一个用于实现初始化指定个数的完全二叉树,以及两个非递归的深度优先遍历,和广度优先遍历

 
  
  1. package fifth; 
  2.  
  3. import java.util.Random; 
  4.  
  5. public class Tool{ 
  6.     public static Random rand= new Random(); 
  7.  
  8. ------------------------------------------------------------- 
  9.  
  10. package fifth; 
  11.  
  12. import java.util.EmptyStackException; 
  13. import java.util.LinkedList; 
  14. import java.util.Queue; 
  15. import java.util.Stack; 
  16.  
  17.  
  18. public class Tree { 
  19.     //用来实现非递归的广度优先查询与构造需要的队列 
  20.     public static Queue<Tree> que= new LinkedList<Tree>(); 
  21.     //用来实现深度优先查询需要的栈 
  22.     public static Stack<Tree> stack=new Stack<Tree>(); 
  23.     //存储的节点信息 
  24.     public int data;                                     
  25.     public Tree leftNode=null
  26.     public Tree rightNode=null
  27.      
  28.     //用来实现遍历的临时节点 
  29.     public Tree cTree=null
  30.      
  31.      
  32.     public Tree() { 
  33.         data=Tool.rand.nextInt(200); 
  34.         display(); 
  35.     } 
  36.      
  37.     public void display(){ 
  38.         System.out.print(data+","); 
  39.     } 
  40.      
  41.     /** 
  42.      * 创建一个以这个节点为定点的完全二叉树 
  43.      * 这个中初始化方法类似于广度优先遍历 
  44.      * @param count 要创建的完全二叉树的节点个数 
  45.      */ 
  46.     public void initTree(int count){ 
  47.         que.add(this); 
  48.         while((cTree=que.poll())!=null
  49.         { 
  50.             //当树中需要创建节点数小于两个时,那么就应该创造完叶子节点然后返回 
  51.             //当树中需要创建节点个数大于两个时,那么就应该把创造的子节点放入队列中,等待创建它的子节点 
  52.             if(count-2>0
  53.             { 
  54.                  
  55.                 cTree.leftNode=new Tree(); 
  56.                 que.add(cTree.leftNode); 
  57.                 count--; 
  58.                 cTree.rightNode=new Tree(); 
  59.                 que.add(cTree.rightNode); 
  60.                 count--; 
  61.             } 
  62.             else if(count==0
  63.             { 
  64.                 break
  65.             } 
  66.             else if(count==1
  67.             { 
  68.                 cTree.leftNode=new Tree(); 
  69.                 count--; 
  70.                 break
  71.             } 
  72.             else 
  73.             { 
  74.                 cTree.leftNode=new Tree(); 
  75.                 cTree.rightNode=new Tree(); 
  76.                 count-=2
  77.             } 
  78.                  
  79.         } 
  80.     } 
  81.     /** 
  82.      * 非递归的广度优先遍历,用的队列来实现 
  83.      */ 
  84.     public void breadthSearch(){ 
  85.         System.out.print('\n'+"非递归广度优先遍历:"); 
  86.         //清空队列,同时把自己放在队列的第一个元素 
  87.         que.clear();que.add(this); 
  88.         while((cTree=que.poll())!=null
  89.         { 
  90.             //访问队列中的数据 
  91.             cTree.display(); 
  92.             if(cTree.leftNode!=null
  93.             { 
  94.                 que.add(cTree.leftNode); 
  95.             } 
  96.             if(cTree.rightNode!=null
  97.             { 
  98.                 que.add(cTree.rightNode); 
  99.             } 
  100.         } 
  101.          
  102.     } 
  103.      
  104.     /** 
  105.      * 用于栈来实现的非递归深度优先遍历 前序遍历 
  106.      */ 
  107.     public void deepFirstSearch(){ 
  108.         System.out.print('\n'+"非递归深度优先遍历,前序遍历:"); 
  109.         //首先把自己压入栈底 
  110.         this.display(); 
  111.         cTree=this
  112.         while(cTree!=null
  113.         { 
  114.             if(cTree.leftNode!=null
  115.             { 
  116.                 stack.add(cTree); 
  117.                 cTree.leftNode.display(); 
  118.                 cTree=cTree.leftNode; 
  119.             } 
  120.             else 
  121.             { 
  122.                 while(cTree.rightNode==null
  123.                 { 
  124.                     try 
  125.                     { 
  126.                         cTree=stack.pop(); 
  127.                     } 
  128.                     catch(EmptyStackException e) 
  129.                     { 
  130.                         return
  131.                     } 
  132.                      
  133.                 } 
  134.                 cTree.rightNode.display(); 
  135.                 cTree=cTree.rightNode; 
  136.             } 
  137.         } 
  138.          
  139.     } 
  140.  
  141.     public static void main(String[] args) { 
  142.         Tree tree= new Tree(); 
  143.         tree.initTree(10); 
  144.         System.out.println(); 
  145.         tree.leftNode.display(); 
  146.         tree.rightNode.display(); 
  147.         tree.breadthSearch(); 
  148.         tree.deepFirstSearch(); 
  149.     }