java递归,非递归遍历二叉树

import java.util.Stack;     

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(Node p) {    
        Stack<Node> stack = new Stack<Node>();    
        Stack<Node> temp = new Stack<Node>();    
        Node node = p;    
        while (node != null || stack.size() > 0) {    
            while (node != null) {    
                temp.push(node);    
                stack.push(node);    
                node = node.getRight();    
            }    
            if (stack.size() > 0) {    
                node = stack.pop();    
                node = node.getLeft();    
            }    
        }    
        while (temp.size() > 0) {//把插入序列都插入到了temp。  
            node = temp.pop();    
            System.out.print(node);    
        }    
    }

public static void iterativePostorder3(Node p) {    
        Stack<Node> stack = new Stack<Node>();    
        Node node = p, prev = p;    
        while (node != null || stack.size() > 0) {    
            while (node != null) {    
                stack.push(node);    
                node = node.getLeft();    
            }    
            if (stack.size() > 0) {    
                Node temp = stack.peek().getRight();    
                if (temp == null || temp == prev) {    
                    node = stack.pop();    
                    System.out.print(node);      
                    prev = node;    
                    node = null;    
                } else {    
                    node = temp;    
                }    
            }   
        }    
    }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值