二叉树遍历

/**
 * 树结构,包括节点值,左子树节点指针,右子树节点指针
 */
public class TreeNode<T extends Comparable> {

    private T value;
    private TreeNode left;
    private TreeNode right;

    public TreeNode() {
    }

    public TreeNode(T value, TreeNode left, TreeNode right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }
}
import java.util.Arrays;
import java.util.Stack;

public class TreeTest {

    public static void main(String[] args) throws Exception {
        int[] input = {4, 2, 6, 1, 3, 5, 7, 8, 10};
        System.out.println("======构建树:" + Arrays.toString(input) + "======");
        TreeNode<Integer> rootNode = null;
        for (int i = 0; i < input.length; i++) {
            rootNode = insert(input[i], rootNode);
        }

        preOrder(rootNode);
        System.out.println();

        preOrder1(rootNode);
        System.out.println();
        System.out.println("==============================================");


        inOrder(rootNode);
        System.out.println();

        inOrder1(rootNode);
        System.out.println();
        System.out.println("==============================================");

        postOrder(rootNode);
        System.out.println();

        postOrder1(rootNode);
        System.out.println();
    }

    //前序(先根):根、左、右
    public static void preOrder(TreeNode node) {
        if (null != node) {
            System.out.print(node.getValue() + ", ");
            preOrder(node.getLeft());
            preOrder(node.getRight());
        }
    }

    //中序(根中间):根、左、右
    public static void inOrder(TreeNode node) {
        if (null != node) {
            inOrder(node.getLeft());
            System.out.print(node.getValue() + ", ");
            inOrder(node.getRight());
        }
    }

    //后序(根最后):左、右、根
    public static void postOrder(TreeNode node) {
        if (null != node) {
            inOrder(node.getLeft());
            inOrder(node.getRight());
            System.out.print(node.getValue() + ", ");
        }
    }

    /**
     * 前序遍历
     * 非递归
     */
    public static void preOrder1(TreeNode node) {
        Stack<TreeNode> stack = new Stack<>();
        stack.push(node);
        while (!stack.empty()) {
            node = stack.pop();
            System.out.print(node.getValue() + ", ");

            if (null != node.getRight()) {
                stack.push(node.getRight());
            }

            if (null != node.getLeft()) {
                stack.push(node.getLeft());
            }
        }
    }


    /**
     * 中序遍历
     * 非递归
     */
    public static void inOrder1(TreeNode node) {
        Stack<TreeNode> stack = new Stack<>();
        while (node != null || !stack.empty()) {
            while (null != node) {
                stack.push(node);
                node = node.getLeft();
            }

            if (!stack.empty()) {
                node = stack.pop();
                System.out.print(node.getValue() + ", ");
                node = node.getRight();
            }
        }
    }

    /**
     * 后序遍历
     * 非递归
     */
    public static void postOrder1(TreeNode node) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode visiteNode = node;
        TreeNode currentNode = node;
        while (currentNode != null || !stack.empty()) {
            while (null != currentNode) {
                stack.push(currentNode);
                currentNode = currentNode.getLeft();
            }

            currentNode = stack.peek();
            if (null != currentNode.getRight() && visiteNode != currentNode.getRight()) {
                currentNode = currentNode.getRight();
            } else {
                currentNode = stack.pop();
                System.out.print(currentNode.getValue() + ", ");
                visiteNode = currentNode;
                currentNode = null;
            }
        }
    }

    private static <E extends Comparable> TreeNode<E> insert(E value, TreeNode<E> t) {
        if (t == null) {
            return new TreeNode<>(value, null, null);
        }

        int compareResult = value.compareTo(t.getValue());

        if (compareResult < 0) {
            t.setLeft(insert(value, t.getLeft()));
        } else if (compareResult > 0) {
            t.setRight(insert(value, t.getRight()));
        } else {
            ;
        }
        return t;
    }

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值