zoukankan      html  css  js  c++  java
  • 二叉树三种遍历的非递归实现

    下面利用一个栈(Stack)分别实现了二叉树的非递归式的前序、中序、后续遍历。代码最后,自定义一个People类,并存储在二叉树中,并依此利用三种遍历方法输出每个People。

    import java.util.Stack;
    
    class TreeNode<K> {
        TreeNode<K> left, right;
        K key;
        public TreeNode(K k) {
            left = null; right = null; key = k;
        }
    }
    
    class BinaryTree<K extends Comparable<K> > { //添加 extends Comparable<K>后,K对象可以进行比较。
        TreeNode<K> root;
        public BinaryTree() {
            root = null;
        }
        public void createTree(TreeNode<K> node, K data) { //建立二叉树
            if(root == null) {
                root = new TreeNode<K>(data);
            } else {
                if(data.compareTo(node.key) < 0) {
                    if(node.left == null)
                        node.left = new TreeNode<K>(data);
                    else
                        createTree(node.left, data);
                } else {
                    if(node.right == null)
                        node.right = new TreeNode<K>(data);
                    else
                        createTree(node.right, data);
                }
            }
        }
        public void preOrder(TreeNode<K> root) { // 前序遍历,非递归实现。
            if(root != null) {
                Stack<TreeNode<K>> stack = new Stack<TreeNode<K>>();
                stack.push(root);
                while (!stack.isEmpty()) {
                    TreeNode<K> tmp = stack.pop();
                    System.out.println(tmp.key);
                    if(tmp.right != null) stack.push(tmp.right);
                    if(tmp.left != null) stack.push(tmp.left);
                }
            }
        }
        public void inOrder(TreeNode<K> root) { //中序遍历,非递归实现
            Stack<TreeNode<K>> stack = new Stack<TreeNode<K>>();
            while (root != null || !stack.isEmpty()) {
                while(root != null) {
                    stack.push(root);
                    root = root.left;
                }
                if (!stack.isEmpty()) {
                    TreeNode<K> tmp = stack.pop();
                    System.out.println(tmp.key);
                    root = tmp.right;
                }
            }
        }
        public void postOrder(TreeNode<K> root) { //后序遍历,非递归实现
            Stack<TreeNode<K>> stack = new Stack<TreeNode<K>>();
            TreeNode<K> pre = root;                         //记录上一个输出的节点。
            while (root != null || !stack.isEmpty()) {
                while (root != null) {
                    stack.push(root);
                    root = root.left;
                }
                if (!stack.isEmpty()) {
                    TreeNode<K> tmp = stack.peek().right;
                    if(tmp == null || tmp == pre) {
                        TreeNode<K> tmp2 = stack.pop();
                        System.out.println(tmp2.key);
                        pre = tmp2;
                        root = null;
                    } else {
                        root = tmp;
                    }
                }
            }
        }
    }
    class People implements Comparable<People> { // 自定义People类,并覆盖了equals,hashCode,toString方法,实现了Comparable接口。
        String name;
        int age;
        public People(String n, int a) {
            name = n; age = a;
        }
        @Override
        public boolean equals(Object obj) {
            if(this == obj)
                return true;
            if(!(obj instanceof People))
                return false;
            return this.name.equals(((People)obj).name) && this.age == ((People)obj).age;
        }
        @Override
        public int hashCode() {
            return name.hashCode()*37 + age;
        }
        @Override
        public String toString() {
            return name + "," + age;
        }
        @Override
        public int compareTo(People o) {
            return this.age - o.age;
        }
    }
    public class TestClass {
        public static void main(String[] args) {//6, 8, 7, 5, 2, 5
            People[] people = new People[] { new People("xue", 6),  
                    new People("hong", 8), new People("jun", 7), new People("shang", 5),
                    new People("hai", 2), new People("bei", 5)};
            BinaryTree<People> bt = new BinaryTree<People>();
            for (int i = 0; i < people.length; i++) {
                bt.createTree(bt.root, people[i]);
            }
    //        bt.preOrder(bt.root);
    //        bt.inOrder(bt.root);
            bt.postOrder(bt.root);
        }
    }
  • 相关阅读:
    关于高等代数的计算题III(线性方程组)
    关于高等代数的计算题I(矩阵,标准形,二次型)
    日志打印太多导致cpu 100%问题解决
    xxl-job串行任务挂死问题
    apache http调用设置超时时间
    动态修改logback日志级别
    prom2click批量参数设置
    logback日志使用
    跨线程池传递线程变量,使用阿里的transmittable-thread-local
    ES操作整理
  • 原文地址:https://www.cnblogs.com/lasclocker/p/4859198.html
Copyright © 2011-2022 走看看