waysun一路陽光

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

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評論 :: 0 Trackbacks
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          公告

          開啟一扇窗,給自己一個舞臺!

          QQ:251218333,82424805
          MSN:CF1504@Hotmail.com
          E-mail:yyk1504@163.com

          �ڷQ���a�K�O�p�ƾ��ڷQ���a�K�O�p�ƾ��ڷQ���a�K�O�p�ƾ��ڷQ���a�K�O�p�ƾ��ڷQ���a�K�O�p�ƾ��ڷQ���a�K�O�p�ƾ�位來訪者

          常用鏈接

          隨筆分類(189)

          隨筆檔案(160)

          文章分類(1)

          AJAX

          搜索

          積分與排名

          最新隨筆

          最新評論

          轉自: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一米陽光 閱讀(277) 評論(0)  編輯  收藏 所屬分類: JAVA源碼總結備用
          主站蜘蛛池模板: 平遥县| 宝应县| 若羌县| 饶河县| 乐安县| 河曲县| 滁州市| 疏勒县| 湘乡市| 舟曲县| 怀柔区| 霍林郭勒市| 丰原市| 青冈县| 四子王旗| 吴江市| 上杭县| 石首市| 茂名市| 桑植县| 德州市| 青川县| 醴陵市| 庆云县| 呼玛县| 循化| 互助| 蒲江县| 漳州市| 离岛区| 桃江县| 嘉义市| 梧州市| 城步| 桐梓县| 宜兰市| 永康市| 从江县| 涿鹿县| 昌黎县| 新余市|