二叉树的遍历

结构:

public class BiTree {
    public BiTree lchild;
    public BiTree rchild;
    public int element;
    public boolean isVisited = false;
    public boolean isPolled = false;
    public BiTree(int element){
        this.element = element;
    }
    public void visit(){
        isVisited = true;
        System.out.print(element);
    }
}

1.递归先序遍历

public void preOrder(BiTree b) {
    if (b != null) {
        b.visit();
        preOrder(b.lchild);
        preOrder(b.rchild);
    }
}

2.递归中序遍历

public void inOrder(BiTree b) {
    if (b != null) {
        inOrder(b.lchild);
        b.visit();
        inOrder(b.rchild);
    }
}

3.递归后序遍历

public void postOrder(BiTree b) {
    if (b != null) {
        postOrder(b.lchild);
        postOrder(b.rchild);
        b.visit();
    }
}

4.非递归先序遍历

public void preOrder2(BiTree b) {
    LinkedList<BiTree> stack = new LinkedList<>();
    BiTree p = b;
    while (p != null) {
        if (!p.isVisited) {
            p.visit();
            if (p.lchild != null) {
                stack.push(p);
                p = p.lchild;
                continue;
            } else {
                if (p.rchild != null) {
                    p = p.rchild;
                    continue;
                }
            }
        } else {
            if (p.rchild != null) {
                p = p.rchild;
                continue;
            }
        }
        p = stack.pop();
    }
}

5.非递归中序遍历

public void inOrder2(BiTree b) {
    LinkedList<BiTree> stack = new LinkedList<>();
    BiTree p = b;
    while (p != null) {
        stack.push(p);
        if (p.lchild != null) {
            p = p.lchild;
            continue;
        }
        p.visit();
        while (p.rchild == null) {
            p = stack.pop();
            p.visit();
        }
        p = p.rchild;
    }
}

6.非递归后序遍历

public void postOrder2(BiTree b) {
    LinkedList<BiTree> stack = new LinkedList<>();
    BiTree p = b;
    while (true) {
        if (p.lchild != null) {
            stack.push(p);
            p = p.lchild;
            continue;
        }
        if (p.rchild != null) {
            p.isPolled = true;
            stack.push(p);
            p = p.rchild;
            continue;
        }
        p.visit();
        while (!stack.isEmpty()) {
            p = stack.pop();
            if (p.isPolled) {
                p.visit();
            } else {
                if (p.rchild != null) {
                    p.isPolled = true;
                    stack.push(p);
                    p = p.rchild;
                    break;
                }
                p.visit();
            }
        }
        if (stack.isEmpty()) break;
    }
}

7.层次遍历

public void levelOrder(BiTree b) {
    LinkedList<BiTree> queue = new LinkedList<>();
    BiTree p = b;
    queue.add(p);
    while (!queue.isEmpty()) {
        p = queue.poll();
        p.visit();
        if (p.lchild != null) queue.add(p.lchild);
        if (p.rchild != null) queue.add(p.rchild);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值