<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    waysun一路陽光

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

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
      167 隨筆 :: 1 文章 :: 64 評論 :: 0 Trackbacks
    轉自:http://m.tkk7.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一米陽光 閱讀(272) 評論(0)  編輯  收藏 所屬分類: JAVA源碼 、總結備用
    主站蜘蛛池模板: 亚洲第一页中文字幕| 成人亚洲性情网站WWW在线观看| 亚洲人成电影在在线观看网色| 一级黄色毛片免费看| 亚洲av无码成人精品区| 偷自拍亚洲视频在线观看| 日本特黄特色aa大片免费| 亚洲人成色99999在线观看| 成人免费毛片观看| 国产亚洲中文日本不卡二区| 成熟女人牲交片免费观看视频| 亚洲精品456人成在线| 在线观看免费成人| 国产成人高清亚洲一区久久| 免费一级肉体全黄毛片| 毛片基地看看成人免费| 亚洲av永久无码制服河南实里| 久久国产高潮流白浆免费观看 | 亚洲A∨精品一区二区三区| 又粗又长又爽又长黄免费视频| 久久精品国产亚洲Aⅴ香蕉| a毛片在线免费观看| 亚洲成在人线中文字幕| 国产大片线上免费观看| 久久亚洲精品无码网站| 亚洲自偷自偷偷色无码中文| 亚洲综合久久夜AV | a国产成人免费视频| 91午夜精品亚洲一区二区三区| 四虎影视免费在线| 久久er国产精品免费观看8| 亚洲人成电影福利在线播放| 免费看美女被靠到爽的视频| 一二三四在线观看免费中文在线观看| 亚洲乱色熟女一区二区三区丝袜 | 中国国产高清免费av片| 亚洲精品在线免费观看视频| 韩国二级毛片免费播放| 中文在线免费观看| 亚洲人成未满十八禁网站| 亚洲精品和日本精品|