【转】二叉树三种遍历的递归/非递归实现

文章转自:https://www.cnblogs.com/gaopeng527/p/5451176.html

import java.util.Stack;  
import java.util.HashMap;  
  
public class BinTree {  
    private char date;  
    private BinTree lchild;  
    private BinTree rchild;  
  
    public BinTree(char c) {  
        date = c;  
    }  
  
    // 先序遍历递归   
    public static void preOrder(BinTree t) {  
        if (t == null) {  
            return;  
        }  
        System.out.print(t.date);  
        preOrder(t.lchild);  
        preOrder(t.rchild);  
    }  
  
    // 中序遍历递归   
    public static void InOrder(BinTree t) {  
        if (t == null) {  
            return;  
        }  
        InOrder(t.lchild);  
        System.out.print(t.date);  
        InOrder(t.rchild);  
    }  
  
    // 后序遍历递归   
    public static void PostOrder(BinTree t) {  
        if (t == null) {  
            return;  
        }  
        PostOrder(t.lchild);  
        PostOrder(t.rchild);  
        System.out.print(t.date);  
    }  
  
    // 先序遍历非递归   
    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 static void main(String[] args) {  
        BinTree b1 = new BinTree('a');  
        BinTree b2 = new BinTree('b');  
        BinTree b3 = new BinTree('c');  
        BinTree b4 = new BinTree('d');  
        BinTree b5 = new BinTree('e');  
  
        /** 
         *      a  
         *     / / 
         *    b   c 
         *   / / 
         *  d   e 
         */  
        b1.lchild = b2;  
        b1.rchild = b3;  
        b2.lchild = b4;  
        b2.rchild = b5;  
  
        BinTree.preOrder(b1);  
        System.out.println();  
        BinTree.preOrder2(b1);  
        System.out.println();  
        BinTree.InOrder(b1);  
        System.out.println();  
        BinTree.InOrder2(b1);  
        System.out.println();  
        BinTree.PostOrder(b1);  
        System.out.println();  
        BinTree.PostOrder2(b1);  
    }  
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前端知识体系http://www.cnblogs.com/sb19871023/p/3894452.html 前端...
    秋风喵阅读 14,303评论 7 163
  • 每个人都会害怕生离死别,却又不得不坦然面对。 当你告诉我的时候,意料中的意外。很神奇,昨晚我梦到了爷爷和你,梦中的...
    张喵猫阅读 3,233评论 0 0
  • 这次看的书是为了丰富思维数学班的小朋友,特别是幼升小的班级的小朋友,让他们的课堂能更丰富一些。 ...
    卡卡_a24f阅读 4,021评论 0 0
  • 我用一块七,治好了我自己 文|狐小白 “您好,我买点儿药。” “什么药?” “桂枝9克,白芍9克,甘草6克,就这么...
    狐小白阅读 5,928评论 81 48
  • 就业竞争压力好大 随着社会经济水平的发展,我国教育普及率提高,人们对于教育的重要性的认知水平提升,越来越多的家长让...
    Janets阅读 1,105评论 0 0