二叉树三种遍历的6中实现方式

import java.util.LinkedList;
import java.util.Stack;

/**
 * @author bro
 * @date 2020/9/21 9:59
 * @desc
 */
public class Construct {
    /**
     * 先序递归
     *
     * @param biTree
     */
    public static void preOrderRe(TreeNode biTree) {
        System.out.print(biTree.value + " ");
        TreeNode left = biTree.left;
        if (left != null) {
            preOrderRe(left);
        }
        TreeNode right = biTree.right;
        if (right != null) preOrderRe(right);
    }

    /**
     * 先序非递归
     *
     * @param biTree
     */
    public static void preOrder(TreeNode biTree) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while (biTree != null || !stack.isEmpty()) {
            while (biTree != null) {
                System.out.print(biTree.value + " ");
                stack.push(biTree);
                biTree = biTree.left;
            }
            if (!stack.isEmpty()) {
                biTree = stack.pop();
                biTree = biTree.right;
            }
        }
    }

    /**
     * 中序递归
     * @param biTree
     */
    public static void midOrderRe(TreeNode biTree) {
        if (biTree == null) return;
        else {
            midOrderRe(biTree.left);
            System.out.print(biTree.value + " ");
            midOrderRe(biTree.right);
        }
    }

    /**
     * 中序非递归
     * @param biTree
     */
    public static void midOrder(TreeNode biTree) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while (biTree != null || !stack.isEmpty()) {
            while (biTree != null) {
                stack.push(biTree);
                biTree = biTree.left;
            }
            if (!stack.isEmpty()) {
                TreeNode pop = stack.pop();
                System.out.print(pop.value + " ");
                if (pop.right != null) biTree = pop.right;
            }
        }
    }

    /**
     * 后序递归
     * @param biTree
     */
    public static void postOrderRe(TreeNode biTree) {
        if (biTree == null) return;
        else {
            postOrderRe(biTree.left);
            postOrderRe(biTree.right);
            System.out.print(biTree.value + " ");
        }
    }

    /**
     * 后序非递归
     * @param biTree
     */
    public static void postOrder(TreeNode biTree) {
        int left = 1;
        int right = 2;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        Stack<Integer> stack2 = new Stack<Integer>();
        while (biTree != null || !stack.isEmpty()) {
            while (biTree != null) {
                //将节点压入栈1,并在栈2将节点标记为左节点
                stack.push(biTree);
                stack2.push(left);
                biTree = biTree.left;
            }
            while (!stack.isEmpty() && stack2.peek() == right) {
                //如果是从右子节点返回父节点,则任务完成,将两个栈的栈顶弹出
                stack2.pop();
                System.out.print(stack.pop().value + " ");
            }
            if (!stack.isEmpty() && stack2.peek() == left) {
                //如果是从左子节点返回父节点,则将标记改为右子节点
                stack2.pop();
                stack2.push(right);
                biTree = stack.peek().right;
            }
        }
    }

    /**
     * 层次遍历
     * @param biTree
     */
    public static void levelOrder(TreeNode biTree) {
        if (biTree == null) return;
        LinkedList<TreeNode> list = new LinkedList<TreeNode>();
        list.add(biTree);
        TreeNode currentNode;
        while (!list.isEmpty()) {
            currentNode = list.poll();
            System.out.print(currentNode.value + " ");
            if (currentNode.left != null) list.add(currentNode.left);
            if (currentNode.right != null) list.add(currentNode.right);
        }
    }

    public static void main(String[] args) {
        TreeNode[] node = new TreeNode[10];
        for (int i = 0; i < 10; i++) {
            node[i] = new TreeNode(i);
        }
        for (int i = 0; i < 10; i++) {
            if (i * 2 + 1 < 10) node[i].left = node[i * 2 + 1];
            if (i * 2 + 2 < 10) node[i].right = node[i * 2 + 2];
        }
        System.out.print("二叉树递归先序排列为:");
        preOrderRe(node[0]);
        System.out.println();
        System.out.print("二叉树非递归先序排列为:");
        preOrder(node[0]);
        System.out.println();
        System.out.print("二叉树递归中序排列为:");
        midOrderRe(node[0]);
        System.out.println();
        System.out.print("二叉树非递归中序排列为:");
        midOrder(node[0]);
        System.out.println();
        System.out.print("二叉树递归后序排列为:");
        postOrderRe(node[0]);
        System.out.println();
        System.out.print("二叉树非递归后序排列为:");
        postOrder(node[0]);
        System.out.println();
        System.out.print("二叉树层次遍历为:");
        levelOrder(node[0]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值