二叉树的三种遍历方式的递归实现和迭代实现

数据与算法的课程中,给出了二叉树三种遍历方式的实现代码但是没有给出其递归实现。本文给出迭代实现。


测试结果如下:

前序遍历递归实现:
96487
前序遍历迭代实现:
96487
中序遍历递归实现:
46789
中序遍历迭代实现:
46789
后序遍历递归实现:
47869
后序遍历迭代实现:
47869
Process finished with exit code 0


代码如下:

package AlgorithmTest;

import java.util.Stack;

/**
 * Created by dell on 2016/12/3.
 */
public class IterationGoThrough {
    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insertNode(9);
        binarySearchTree.insertNode(6);
        binarySearchTree.insertNode(4);
        binarySearchTree.insertNode(8);
        binarySearchTree.insertNode(7);
        System.out.println("前序遍历递归实现:");
        preorder(binarySearchTree.getRootNode());
        System.out.println();
        System.out.println("前序遍历迭代实现:");
        preorderIteration(binarySearchTree.getRootNode());
        System.out.println();
        System.out.println("中序遍历递归实现:");
        inorder(binarySearchTree.getRootNode());
        System.out.println();
        System.out.println("中序遍历迭代实现:");
        inorderIteration(binarySearchTree.getRootNode());
        System.out.println();
        System.out.println("后序遍历递归实现:");
        postOrder(binarySearchTree.getRootNode());
        System.out.println();
        System.out.println("后序遍历迭代实现:");
        postOrderIteration(binarySearchTree.getRootNode());

    }

    //前序遍历 递归实现
    public static void preorder(Node root){
        if (root != null){
            System.out.print(root.val);
            preorder(root.nodeLeft);
            preorder(root.nodeRight);
        }
    }

    //前序遍历 迭代实现
    public static void preorderIteration(Node root){
        Stack<Node> nodes = new Stack<>();
        nodes.push(root);
        while (!nodes.isEmpty()){
            root = nodes.pop();
            if (root != null){
                System.out.print(root.val);
                nodes.push(root.nodeRight);
                nodes.push(root.nodeLeft);
            }
        }
    }

    //中序遍历 递归实现
    public static void inorder(Node root){
        if (root != null){
            inorder(root.nodeLeft);
            System.out.print(root.val);
            inorder(root.nodeRight);
        }
    }

    static class Pair{
        Node node;
        int loction;

        public Pair(Node node, int loction) {
            this.node = node;
            this.loction = loction;
        }
    }

    //中序遍历 迭代实现
    public static void inorderIteration(Node root){
        Stack<Pair> stack = new Stack<>();
        stack.push(new Pair(root, 1));
        while (!stack.isEmpty()){
            Pair pair = stack.peek();
            root = pair.node;
            stack.pop();
            if (root != null){
                switch (pair.loction){
                    case 1:
                        stack.push(new Pair(root, 2));
                        stack.push(new Pair(root.nodeLeft, 1));
                        break;
                    case 2:
                        System.out.print(root.val);
                        stack.push(new Pair(root, 3));
                        stack.push(new Pair(root.nodeRight, 1));
                        break;
                    case 3:
                        break;
                }
            }

        }
    }

    //后序遍历 递归实现
    public static void postOrder(Node root){
        if (root != null){
            postOrder(root.nodeLeft);
            postOrder(root.nodeRight);
            System.out.print(root.val);
        }
    }

    //中序遍历 迭代实现
    public static void postOrderIteration(Node root){
        Stack<Pair> stack = new Stack<>();
        stack.push(new Pair(root, 1));
        while (!stack.isEmpty()){
            Pair pair = stack.peek();
            root = pair.node;
            stack.pop();
            if (root != null){
                switch (pair.loction){
                    case 1:
                        stack.push(new Pair(root, 2));
                        stack.push(new Pair(root.nodeLeft, 1));
                        break;
                    case 2:
                        stack.push(new Pair(root, 3));
                        stack.push(new Pair(root.nodeRight, 1));
                        break;
                    case 3:
                        System.out.print(root.val);
                        break;
                }
            }
        }
    }


}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值