二叉树的递归和非递归遍历

参考文章:详解二叉树的非递归遍历
二叉树的非递归遍历的思考
二叉树的遍历

1结构体:节点

class Node{
    int data;
    Node left;
    Node right;
    public Node(int data,Node left,Node right){
        this.data=data;
        this.left=left;
        this.right=right;
    }
}

2递归遍历

    //前序遍历递归
    public static void preOrderRecursion(Node root){
        if(null==root){
            return;
        }else {
            System.out.print(root.data+" ");
            preOrderRecursion(root.left);
            preOrderRecursion(root.right);
        }
    }
    //中序遍历递归
    public static void ioOrderRecursion(Node root){
        if(null==root){
            return;
        }else {
            ioOrderRecursion(root.left);
            System.out.print(root.data+" ");
            ioOrderRecursion(root.right);
        }
    }
    //后序遍历递归
    public static void postOrderRecursion(Node root){
        if(null==root){
            return;
        }else {
            postOrderRecursion(root.left);
            postOrderRecursion(root.right);
            System.out.print(root.data+" ");
        }
    }

2非递归(迭代)遍历

该种非递归遍历,三种方式格式都一样,只是在把节点数据放入栈的顺序不一样,(只有结尾处三行代码不同)

	//前序
    public static void preOrderIteration(Node root){
        Stack<Pair<Node,Boolean>> stack = new Stack<>();
        stack.push(new Pair<>(root,false));
        Pair<Node, Boolean> pop;
        Node node;
        Boolean visit;
        while (!stack.isEmpty()){
            pop = stack.pop();
            node = pop.getKey();
            visit = pop.getValue();
            if(null==node){
                continue;
            }

            if(visit){
                System.out.print(node.data+" ");
            }else {
                //前序遍历,需要把它的镜像放到栈里面
                stack.push(new Pair<>(node.right,false));
                stack.push(new Pair<>(node.left,false));
                stack.push(new Pair<>(node,true));
            }
        }
    }
    //中序
    public static void inOrderIteration(Node root){
        Stack<Pair<Node,Boolean>> stack = new Stack<>();
        stack.push(new Pair<>(root,false));
        Pair<Node, Boolean> pop;
        Node node;
        Boolean visit;
        while (!stack.isEmpty()){
            pop = stack.pop();
            node = pop.getKey();
            visit = pop.getValue();
            if(null==node){
                continue;
            }

            if(visit){
                System.out.print(node.data+" ");
            }else {
                //中序遍历,需要把它的镜像放到栈里面
                stack.push(new Pair<>(node.right,false));
                stack.push(new Pair<>(node,true));
                stack.push(new Pair<>(node.left,false));
            }
        }
    }
    //后序
    public static void postOrderIteration(Node root){
        Stack<Pair<Node,Boolean>> stack = new Stack<>();
        stack.push(new Pair<>(root,false));
        Pair<Node, Boolean> pop;
        Node node;
        Boolean visit;
        while (!stack.isEmpty()){
            pop = stack.pop();
            node = pop.getKey();
            visit = pop.getValue();
            if(null==node){
                continue;
            }

            if(visit){
                System.out.print(node.data+" ");
            }else {
                //后序遍历,需要把它的镜像放到栈里面
                stack.push(new Pair<>(node,true));
                stack.push(new Pair<>(node.right,false));
                stack.push(new Pair<>(node.left,false));
            }
        }
    }

3非递归遍历

3.1前序非递归遍历

   //方式1
    public static void preOrderIteration1(Node root) {
        Node node = root;
        Stack<Node> stack = new Stack<>();
        while (!stack.isEmpty() || null != node) {
            while (null != node) {
                System.out.print(node.data + " ");
                stack.push(node);
                node = node.left;
            }

            if (!stack.isEmpty()) {
                node = stack.pop();
                node = node.right;
            }
        }
    }
	//方式2
    public static void preOrderIteration2(Node root) {
        Node node = root;
        Stack<Node> stack = new Stack<>();
        while (!stack.isEmpty() || null != node) {
            if (null != node) {
                System.out.print(node.data + " ");
                stack.push(node);
                node = node.left;
            } else {
                node = stack.pop();
                node = node.right;
            }
        }
    }

3.2中序非递归遍历

	//方式1
   public static void inOrderIteration1(Node root) {
        Node node = root;
        Stack<Node> stack = new Stack<>();
        while (!stack.isEmpty() || null != node) {
            while (null != node) {
                stack.push(node);
                node = node.left;
            }

            if (!stack.isEmpty()) {
                node = stack.pop();
                System.out.print(node.data + " ");
                node = node.right;
            }
        }
    }
    //方式2
    public static void inOrderIteration2(Node root) {
        Node node = root;
        Stack<Node> stack = new Stack<>();
        while (!stack.isEmpty() || null != node) {
            if (null != node) {
                stack.push(node);
                node = node.left;
            } else {
                node = stack.pop();
                System.out.print(node.data + " ");
                node = node.right;
            }
        }
    }

3.3后序非递归遍历

    public static void postOrderIteration1(Node root) {
        Node node = root;
        Stack<Node> stack = new Stack<>();
        Node lastVisit = root;
        while (!stack.isEmpty() || null != node) {
            while (null != node) {
                stack.push(node);
                node = node.left;
            }

            node = stack.peek();
            if (node.right == null || lastVisit == node.right) {
                node = stack.pop();
                System.out.print(node.data + " ");
                lastVisit = node;
                node = null;
            } else {
                node = node.right;
            }
        }
    }

4层序遍历

使用到的是队列。上面的都是使用到的栈

    public static void levelOrder(Node root){
        Node node = root;
        Queue<Node> queue = new LinkedList<>();
        queue.add(node);
        while (!queue.isEmpty()){
            node = queue.poll();
            System.out.print(node.data + " ");
            if(null!=node.left){
                queue.add(node.left);
            }
            if(null!=node.right){
                queue.add(node.right);
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树是一种常见的数据结构,包含根节点、左子树和右子树。在进行二叉树遍历时,我们需要按照某种顺序访问二叉树中的每个节点,包括先序遍历、中序遍历和后序遍历。为了实现这些遍历方式,我们可以使用递归非递归两种方式。 一、递归遍历 递归遍历是一种简单直观的遍历方式,也是最常用的一种方式。以先序遍历为例,递归遍历的具体实现如下: 1. 如果当前节点不为空,则输出当前节点的值 2. 如果当前节点有左子树,则递归遍历左子树 3. 如果当前节点有右子树,则递归遍历右子树 中序遍历和后序遍历的实现类似,只需要改变输出节点值的位置即可。 递归遍历的优点是实现简单,代码清晰易懂,但是当二叉树的深度很大时,递归调用会导致系统栈溢出,从而影响程序的性能。因此,对于深度较大的二叉树,我们需要使用非递归遍历来实现。 二、非递归遍历 非递归遍历是利用栈来实现的。以先序遍历为例,非递归遍历的具体实现如下: 1. 将根节点入栈 2. 当栈不为空时,取出栈顶元素,输出其值 3. 如果栈顶元素有右子树,则将其右子树入栈 4. 如果栈顶元素有左子树,则将其左子树入栈 5. 重复步骤2-4,直到栈为空 中序遍历和后序遍历的实现类似,只需要改变入栈顺序即可。 非递归遍历的优点是可以避免递归调用导致的系统栈溢出问题,同时也可以提高程序的运行效率。但是需要注意的是,由于使用了栈来存储节点,因此需要额外的存储空间。 总结: 递归遍历非递归遍历都是二叉树遍历的常用方式。递归遍历简单易懂,但对于深度较大的二叉树可能会导致系统栈溢出。非递归遍历需要使用栈来存储节点,可以避免递归调用导致的问题,但需要额外的存储空间。在实际应用中,我们需要根据实际情况选择合适的遍历方式来实现二叉树遍历

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值