zoukankan      html  css  js  c++  java
  • 二叉树的非递归遍历

     // 先序遍历非递归     
        public static void preOrder2(BinTree t) {    
            Stack<BinTree> s = new Stack<BinTree>();    
            while (t != null || !s.empty()) {    
                while (t != null) {    
                    System.out.print(t.date);    
                    s.push(t);    
                    t = t.lchild;    
                }    
                if (!s.empty()) {    
                    t = s.pop();    
                    t = t.rchild;    
                }    
            }    
        }    
        
        // 中序遍历非递归     
        public static void InOrder2(BinTree t) {    
            Stack<BinTree> s = new Stack<BinTree>();    
            while (t != null || !s.empty()) {    
                while (t != null) {    
                    s.push(t);    
                    t = t.lchild;    
                }    
                if (!s.empty()) {    
                    t = s.pop();    
                    System.out.print(t.date);    
                    t = t.rchild;    
                }    
            }    
        }    
        
        // 后序遍历非递归     
        public static void PostOrder2(BinTree t) {    
            Stack<BinTree> s = new Stack<BinTree>();    
            Stack<Integer> s2 = new Stack<Integer>();    
            Integer i = new Integer(1);    
            while (t != null || !s.empty()) {    
                while (t != null) {    
                    s.push(t);    
                    s2.push(new Integer(0));    
                    t = t.lchild;    
                }    
                while (!s.empty() && s2.peek().equals(i)) {    
                    s2.pop();    
                    System.out.print(s.pop().date);    
                }    
        
                if (!s.empty()) {    
                    s2.pop();    
                    s2.push(new Integer(1));    
                    t = s.peek();    
                    t = t.rchild;    
                }    
            }    
        }    
        
     public void thePostOrderTraversal_Stack(Node root) {   //后序遍历  
            Stack<Node> stack = new Stack<Node>();  
            Stack<Node> output = new Stack<Node>();//构造一个中间栈来存储逆后序遍历的结果  
            Node node = root;  
            while (node != null || stack.size() > 0) {  
                if (node != null) {  
                    output.push(node);  
                    stack.push(node);                 
                    node = node.getRightNode();  
                } else {  
                    node = stack.pop();               
                    node = node.getLeftNode();
                }  
            }  
            System.out.println(output.size());
            while (output.size() > 0) {
                
                printNode(output.pop());  
            }  
        }
  • 相关阅读:
    java中取两位小数 但不要四舍五入
    从字符串中提取数字 java正则表达式
    SQL实现 列转行(MySQL中)
    sql如何根据时间取出最新的数据记录
    动画 很精辟的
    week 与 strong区别 精辟的解释
    The executable was signed with invalid entitlements新设备run出现这个问题
    在iOS中创建静态库
    网址
    nginx单机1w并发设置
  • 原文地址:https://www.cnblogs.com/blythe/p/7486280.html
Copyright © 2011-2022 走看看