码迷,mamicode.com
首页 > 其他好文 > 详细

Tree-抽象数据类型-二叉树

时间:2015-05-28 17:46:53      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

2015-05-28

 

二叉树的遍历:

逻辑顺序->在class名中构建私有类private class TreeNode();

  1 package cn.itcast.initialize;
  2 
  3 import java.util.Stack;
  4 
  5 /**
  6  * 二叉树的链式存储
  7  * @author WWX
  8  */
  9 public class BinaryTree {
 10 
 11     
 12     private TreeNode root=null;
 13     
 14     public BinaryTree(){
 15         root=new TreeNode(1,"rootNode(A)");
 16     }
 17     
 18     /**
 19      * 创建一棵二叉树
 20      * <pre>
 21      *           A
 22      *     B          C
 23      *  D     E            F
 24      *  </pre>
 25      * @param root
 26      * @author WWX
 27      */
 28     public void createBinTree(TreeNode root){
 29         TreeNode newNodeB = new TreeNode(2,"B");
 30         TreeNode newNodeC = new TreeNode(3,"C");
 31         TreeNode newNodeD = new TreeNode(4,"D");
 32         TreeNode newNodeE = new TreeNode(5,"E");
 33         TreeNode newNodeF = new TreeNode(6,"F");
 34         root.leftChild=newNodeB;
 35         root.rightChild=newNodeC;
 36         root.leftChild.leftChild=newNodeD;
 37         root.leftChild.rightChild=newNodeE;
 38         root.rightChild.rightChild=newNodeF;
 39     }
 40     
 41     
 42     public boolean isEmpty(){
 43         return root==null;
 44     }
 45 
 46     //树的高度
 47     public int height(){
 48         return height(root);
 49     }
 50     
 51     //节点个数
 52     public int size(){
 53         return size(root);
 54     }
 55     
 56     
 57     private int height(TreeNode subTree){
 58         if(subTree==null)
 59             return 0;//递归结束:空树高度为0
 60         else{
 61             int i=height(subTree.leftChild);
 62             int j=height(subTree.rightChild);
 63             return (i<j)?(j+1):(i+1);
 64         }
 65     }
 66     
 67     private int size(TreeNode subTree){
 68         if(subTree==null){
 69             return 0;
 70         }else{
 71             return 1+size(subTree.leftChild)
 72                     +size(subTree.rightChild);
 73         }
 74     }
 75     
 76     //返回双亲结点
 77     public TreeNode parent(TreeNode element){
 78         return (root==null|| root==element)?null:parent(root, element);
 79     }
 80     
 81     public TreeNode parent(TreeNode subTree,TreeNode element){
 82         if(subTree==null)
 83             return null;
 84         if(subTree.leftChild==element||subTree.rightChild==element)
 85             //返回父结点地址
 86             return subTree;
 87         TreeNode p;
 88         //现在左子树中找,如果左子树中没有找到,才到右子树去找
 89         if((p=parent(subTree.leftChild, element))!=null)
 90             //递归在左子树中搜索
 91             return p;
 92         else
 93             //递归在右子树中搜索
 94             return parent(subTree.rightChild, element);
 95     }
 96     
 97     public TreeNode getLeftChildNode(TreeNode element){
 98         return (element!=null)?element.leftChild:null;
 99     }
100     
101     public TreeNode getRightChildNode(TreeNode element){
102         return (element!=null)?element.rightChild:null;
103     }
104     
105     public TreeNode getRoot(){
106         return root;
107     }
108     
109     //在释放某个结点时,该结点的左右子树都已经释放,
110     //所以应该采用后续遍历,当访问某个结点时将该结点的存储空间释放
111     public void destroy(TreeNode subTree){
112         //删除根为subTree的子树
113         if(subTree!=null){
114             //删除左子树
115             destroy(subTree.leftChild);
116             //删除右子树
117             destroy(subTree.rightChild);
118             //删除根结点
119             subTree=null;
120         }
121     }
122     
123     public void traverse(TreeNode subTree){
124         System.out.println("key:"+subTree.key+"--name:"+subTree.data);;
125         traverse(subTree.leftChild);
126         traverse(subTree.rightChild);
127     }
128     
129     //前序遍历
130     public void preOrder(TreeNode subTree){
131         if(subTree!=null){
132             visted(subTree);
133             preOrder(subTree.leftChild);
134             preOrder(subTree.rightChild);
135         }
136     }
137     
138     //中序遍历
139     public void inOrder(TreeNode subTree){
140         if(subTree!=null){
141             inOrder(subTree.leftChild);
142             visted(subTree);
143             inOrder(subTree.rightChild);
144         }
145     }
146     
147     //后续遍历
148     public void postOrder(TreeNode subTree) {
149         if (subTree != null) {
150             postOrder(subTree.leftChild);
151             postOrder(subTree.rightChild);
152             visted(subTree);
153         }
154     }
155     
156     //前序遍历的非递归实现
157     public void nonRecPreOrder(TreeNode p){
158         Stack<TreeNode> stack=new Stack<TreeNode>();
159         TreeNode node=p;
160         while(node!=null||stack.size()>0){
161             while(node!=null){
162                 visted(node);
163                 stack.push(node);
164                 node=node.leftChild;
165             }
166         
167             while(stack.size()>0){
168                 node=stack.pop();
169                 node=node.rightChild;
170             } 
171         }
172     }
173     
174     //中序遍历的非递归实现
175     public void nonRecInOrder(final TreeNode p){
176         Stack<TreeNode> stack =new Stack<BinaryTree.TreeNode>();
177         TreeNode node =p;
178         while(node!=null||stack.size()>0){
179             //存在左子树
180             while(node!=null){
181                 stack.push(node);
182                 node=node.leftChild;
183             }
184             //栈非空
185             if(stack.size()>0){
186                 node=stack.pop();
187                 visted(node);
188                 node=node.rightChild;
189             }
190         }
191     }
192     
193     //后序遍历的非递归实现
194     public void noRecPostOrder(TreeNode p){
195         Stack<TreeNode> stack=new Stack<BinaryTree.TreeNode>();
196         TreeNode node =p;
197         while(p!=null){
198             //左子树入栈
199             for(;p.leftChild!=null;p=p.leftChild){
200                 stack.push(p);
201             }
202             //当前结点无右子树或右子树已经输出
203             while(p!=null&&(p.rightChild==null||p.rightChild==node)){
204                 visted(p);
205                 //纪录上一个已输出结点
206                 node =p;
207                 if(stack.empty())
208                     return;
209                 p=stack.pop();
210             }
211             //处理右子树
212             stack.push(p);
213             p=p.rightChild;
214         }
215     }
216     public void visted(TreeNode subTree){
217         subTree.isVisted=true;
218         System.out.println("key:"+subTree.key+"--name:"+subTree.data);;
219     }
220     
221     
222     /**
223      * 二叉树的节点数据结构
224      * @author WWX
225      */
226     private class  TreeNode{
227         private int key=0;
228         private String data=null;
229         private boolean isVisted=false;
230         private TreeNode leftChild=null;
231         private TreeNode rightChild=null;
232         
233         public TreeNode(){    
234             
235         }
236         
237         /**
238          * @param key  层序编码
239          * @param data 数据域
240          */
241         public TreeNode(int key,String data){
242             this.key=key;
243             this.data=data;
244             this.leftChild=null;
245             this.rightChild=null;
246         }
247 
248 
249     }
250     
251     
252     //测试
253     public static void main(String[] args) {
254         BinaryTree bt = new BinaryTree();
255         bt.createBinTree(bt.root);
256         System.out.println("the size of the tree is " + bt.size());
257         System.out.println("the height of the tree is " + bt.height());
258         
259         System.out.println("*******(前序遍历)[ABDECF]遍历*****************");
260         bt.preOrder(bt.root);
261         
262         System.out.println("*******(中序遍历)[DBEACF]遍历*****************");
263         bt.inOrder(bt.root);
264        
265         System.out.println("*******(后序遍历)[DEBFCA]遍历*****************");
266         bt.postOrder(bt.root);
267         
268         System.out.println("***非递归实现****(前序遍历)[ABDECF]遍历*****************");
269         bt.nonRecPreOrder(bt.root);
270         
271         System.out.println("***非递归实现****(中序遍历)[DBEACF]遍历*****************");
272         bt.nonRecInOrder(bt.root);
273         
274         System.out.println("***非递归实现****(后序遍历)[DEBFCA]遍历*****************");
275         bt.noRecPostOrder(bt.root);
276     }
277 }

 

Tree-抽象数据类型-二叉树

标签:

原文地址:http://www.cnblogs.com/garf/p/4536383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!