树的遍历

  • 前序遍历:中左右
  • 中序编列:左中右
  • 后续编列:左右中
//前序遍历
public static void preOrder(Node node) {
    if (node == null) {
        return;
    }
    System.out.print(node.value);
    preOrder(node.left);
    preOrder(node.right);
}

//中序遍历
public static void inOrder(Node node) {
    if (node == null) {
        return;
    }
    inOrder(node.left);
    System.out.print(node.value);
    inOrder(node.right);
}

//后序遍历
public static void postOrder(Node node) {
    if (node == null) {
        return;
    }
    postOrder(node.left);
    postOrder(node.right);
    System.out.print(node.value);

}

/**
 * 非递归实现
 */
//前序遍历
public static void preOrder02(Node node) {
    Stack<Node> stack = new Stack<>();
    while (node != null || !stack.isEmpty()) {
        while (node != null) {
            System.out.print(node.value);
            stack.push(node);
            node = node.left;
        }
        node = stack.pop().right;

    }
}

//中序遍历
public static void inOrder02(Node node) {
    Stack<Node> stack = new Stack<>();
    while (node != null || !stack.isEmpty()) {
        while (node != null) {
            stack.push(node);
            node = node.left;
        }
        node = stack.pop();
        System.out.print(node.value);
        node = node.right;
    }
}

//后序遍历
public static void postOrder2(Node node) {
    Stack<Node> stack = new Stack<Node>();
    Stack<Integer> tag = new Stack<Integer>();
    while (node != null || !stack.isEmpty()) {
        if (node != null) {
            stack.push(node);
            tag.push(1);  //第一次访问
            node = node.left;
        } else {
            if (tag.peek() == 2) {
                System.out.print(stack.pop().value);
                tag.pop();
            } else {
                tag.pop();
                tag.push(2); //第二次访问
                node = stack.peek().right;
            }
        }

    }
//层序遍历
public static void levelOrder(Node node) {
    LinkedList<Node> list = new LinkedList<>();
    list.add(node);
    while (!list.isEmpty()) {
        node = list.poll();
        System.out.print(node.value);
        if (node.left != null) {
            list.offer(node.left);
        }
        if (node.right != null) {
            list.offer(node.right);
        }
    }
}

测试用例

//测试用例
public static void test(Node node) {
    System.out.print("递归_前序排序:");
    preOrder(node);
    System.out.println();
    System.out.print("前序排序:");
    preOrder02(node);
    System.out.println();
    System.out.print("递归_中序遍历:");
    inOrder(node);
    System.out.println();
    System.out.print("中序遍历:");
    inOrder02(node);
    System.out.println();
    System.out.print("递归_后序排序:");
    postOrder(node);
    System.out.println();
    System.out.print("后序排序:");
    postOrder2(node);
    System.out.println();
    System.out.print("层序遍历:");
    levelOrder(node);
    System.out.println();
}
public static void main(String[] args) {
    BST<Integer, Integer> bst = new BST<>();
    bst.put(3, 3);
    bst.put(2, 2);
    bst.put(4, 4);
    TreeOrder.test(bst.root);
}

输出

递归_前序排序:324
前序排序:324
递归_中序遍历:234
中序遍历:234
递归_后序排序:243
后序排序:243
层序遍历:324

转载于:https://www.cnblogs.com/aiguozou/p/11440207.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值