waysun一路陽光

          不輕易服輸,不輕言放棄.--心是夢的舞臺,心有多大,舞臺有多大。踏踏實實做事,認認真真做人。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評論 :: 0 Trackbacks
          轉自:http://www.aygfsteel.com/javacap/archive/2007/10/11/152073.html
          /**
           * 
           
          */

          import java.util.Stack;
          import java.util.Vector;

          /**
           * 
          @author yovn
           *
           
          */
          public class TreeDemo {
              
              
          static interface NodeVistor
              {
                   
          <T> void visit(BinaryTreeNode<T> node);
              }
              
          static class BinaryTree<T>
              {
                  BinaryTreeNode
          <T> root;
                  
                  
          public BinaryTree(BinaryTreeNode<T> root) {
                      
          this.root=root;
                  }

                  
          //no recursion ,pre-order
                  void NLRVisit(NodeVistor visitor)
                  {
                      BinaryTreeNode
          <T> pointer=root;
                      Stack
          <BinaryTreeNode<T>> stack=new Stack<BinaryTreeNode<T>>();
                      
          while(!stack.isEmpty()||pointer!=null)
                      {
                          
          if(pointer!=null)
                          {
                              visitor.visit(pointer);
                              
          if(pointer.rightChild!=null)
                              {
                                  stack.push(pointer.rightChild);
                              }
                              pointer
          =pointer.leftChild;
                          }
                          
          //go to right child
                          else
                          {
                              pointer
          =stack.pop();
                              
                          }
                      }
                  }
                  
                  
          //no recursion , in-order
                  void LNRVisit(NodeVistor visitor)
                  {
                      BinaryTreeNode
          <T> pointer=root;
                      Stack
          <BinaryTreeNode<T>> stack=new Stack<BinaryTreeNode<T>>();
                      
          while(!stack.isEmpty()||pointer!=null)
                      {
                          
          if(pointer!=null)
                          {
                              stack.push(pointer);
                              
                              pointer
          =pointer.leftChild;
                          }
                          
          //no left child here, then visit root and then go to right child
                          else
                          {
                              pointer
          =stack.pop();
                              visitor.visit(pointer);
                              pointer
          =pointer.rightChild;
                              
                          }
                      }
                  }
                  
                  
                  
          //no recursion ,post-order,this one is the most complex one.
                  
          //we need know from which side ,it back(left or right)
                  void LRNVisit(NodeVistor visitor)
                  {
                      
          if(root==null)return;
                      BinaryTreeNode
          <T> pointer=root;
                      Stack
          <BinaryTreeNode<T>> stack=new Stack<BinaryTreeNode<T>>();
                      
          while(true)
                      {
                          
                          
          //mark left 
                          while(pointer!=null)
                          {
                              stack.push(pointer);
                              pointer
          =pointer.leftChild;
                          }
                          
                          
                          pointer
          =stack.pop();
                          
                          
          while(pointer.visitedRight)//back from right child, so we can visit it now.
                          {
                              visitor.visit(pointer);
                              
          if(stack.isEmpty())return;
                              pointer
          =stack.pop();
                          }
                      
                          pointer.visitedRight
          =true;
                          stack.push(pointer);
                          
                          pointer
          =pointer.rightChild;
                          
                          
                      }
                      
                  }
                  
                  
                  
          void levelOrder(NodeVistor visitor)
                  {
                      
          if(root==null)return;
                      BinaryTreeNode
          <T> pointer=root;
                      Vector
          <BinaryTreeNode<T>> queue=new Vector<BinaryTreeNode<T>>();
                      
                      queue.add(pointer);
                      
          while(!queue.isEmpty())
                      {
                          BinaryTreeNode
          <T> node=queue.remove(0);
                          visitor.visit(node);
                          
          if(node.leftChild!=null)
                          {
                              queue.add(node.leftChild);
                          }
                          
          if(node.rightChild!=null)
                          {
                              queue.add(node.rightChild);
                          }
                          
                      }
                      
                  }
                  
              }
              
          static class BinaryTreeNode<T>
              {
                  
                  BinaryTreeNode(T data)
                  {
                      
          this.data=data;
                  }
                  T data;
                  
          boolean visitedRight;
                  BinaryTreeNode
          <T> leftChild;
                  BinaryTreeNode
          <T> rightChild;
              }

              
          /**
               * 
               
          */
              
          public TreeDemo() {
                  
          // TODO Auto-generated constructor stub
              }

              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  BinaryTreeNode
          <String> root=new BinaryTreeNode<String>("A");
                  root.leftChild
          =new BinaryTreeNode<String>("B");
                  root.rightChild
          =new BinaryTreeNode<String>("C");
                  
                  
                  root.leftChild.leftChild
          =new BinaryTreeNode<String>("D");
                  
                  root.rightChild.leftChild
          =new BinaryTreeNode<String>("E");
                  
                  root.rightChild.rightChild
          =new BinaryTreeNode<String>("F");
                  
                  root.rightChild.leftChild.rightChild
          =new BinaryTreeNode<String>("G");
                  
                  root.rightChild.rightChild.leftChild
          =new BinaryTreeNode<String>("H");
                  root.rightChild.rightChild.rightChild
          =new BinaryTreeNode<String>("I");
                  
                  NodeVistor visitor
          =new NodeVistor()
                  {

                      @Override
                      
          public <T> void visit(BinaryTreeNode<T> node) {
                          System.out.print(
          "'"+node.data+"'");
                          
                      }
                      
                  };
                  
                  BinaryTree
          <String> tree=new BinaryTree<String>(root);

                  
                  System.out.println(
          "pre-order visit");
                  tree.NLRVisit(visitor);
                  System.out.println();
                  System.out.println(
          "in-order visit");
                  
                  tree.LNRVisit(visitor);
                  
                  System.out.println();
                  System.out.println(
          "post-order visit");
                  
                  tree.LRNVisit(visitor);
                  
                  System.out.println();
                  System.out.println(
          "level-order visit");
                  
                  tree.levelOrder(visitor);
              }

          }

          posted on 2009-04-15 22:03 weesun一米陽光 閱讀(278) 評論(0)  編輯  收藏 所屬分類: JAVA源碼總結備用
          主站蜘蛛池模板: 昌江| 志丹县| 瑞昌市| 南川市| 柏乡县| 望谟县| 凤翔县| 关岭| 三河市| 乌拉特后旗| 镇康县| 攀枝花市| 华宁县| 澄迈县| 宜州市| 泰宁县| 乌兰浩特市| 修水县| 安庆市| 镶黄旗| 江达县| 新郑市| 延吉市| 阿坝| 桂阳县| 夏河县| 吉首市| 榆中县| 宁都县| 遵义县| 泽库县| 闽侯县| 苍溪县| 西和县| 海宁市| 黑龙江省| 松阳县| 光山县| 韶山市| 图木舒克市| 庆城县|