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

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

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);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值