zoukankan      html  css  js  c++  java
  • 二叉树的基本操作-二叉树遍历

    二叉树的基本操作包含:

      判断是否为空,获取节点数,先跟遍历,中跟遍历,后根遍历,层级遍历,查找元素

    二叉树结构

    public class Node {
        Object value; //结点值
        Node leftChild;//左子树的引用
        Node rightChild;//右子树的引用
    
        public Node(Object value) {
            super();
            this.value = value;
        }
    
        public Node(Object value, Node leftChild, Node rightChild) {
            super();
            this.value = value;
            this.leftChild = leftChild;
            this.rightChild = rightChild;
        }
    
        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    ", leftChild=" + leftChild +
                    ", rightChild=" + rightChild +
                    '}';
        }
    }

    判断是否为空树:

       public boolean isEmpty() {
            return root == null;
        }

    获取节点数量:

     public int size(Node root) {
            if (root == null)
                return 0;
            int left = size(root.leftChild);
            int right = size(root.rightChild);
            return left + right + 1;
        }

    获取高度:

        public int getHeight(Node root) {
            if (root == null)
                return 0;
            int left = getHeight(root.leftChild);
            int right = getHeight(root.rightChild);
            return Math.max(left, right) + 1;
        }

    先根遍历递归:

     public void preOrderTraverse(Node root) {
            if (root == null)
                return;
            //打印根节点
            System.out.print(root.value + "  ");
            //创建左子树,进行先跟遍历
            preOrderTraverse(root.leftChild);
            //创建右子树,进行先跟遍历
            preOrderTraverse(root.rightChild);
        }

    先跟非递归

       void preRoot(Node root) {
            Stack<Node> stack = new Stack();
            Node temp = root;
            while (temp != null || !stack.empty()) {
                while (temp != null) {
                    System.out.print(temp.value + "  ");
                    stack.push(temp.rightChild);
                    temp = temp.leftChild;
                }
                if (!stack.empty()) {
                    temp = stack.pop();
                }
            }
        }

    中跟递归:

       public void inOrderTraverse(Node root) {
            //出口
            if (root == null)
                return;
            //遍历左子树
            inOrderTraverse(root.leftChild);
            //遍历根
            System.out.print(root.value + "  ");
            //遍历右子树
            inOrderTraverse(root.rightChild);
        }

    中跟非递归:

      @Override
        public void inOrderByStack() {
            Stack<Node> stack = new Stack<>();
            Node temp = root;
            while (temp != null || !stack.isEmpty()) {
                while (temp != null) {
                    stack.push(temp);
                    temp = temp.leftChild;
                }
                if (!stack.isEmpty()) {
                    temp = stack.pop();
                    System.out.print(temp.value + "  ");
                    temp = temp.rightChild;
                }
            }
        }

    后跟递归:

     public void postOrderTraverse(Node root) {
            //出口
            if (root == null)
                return;
            //先左
            postOrderTraverse(root.leftChild);
            //再右
            postOrderTraverse(root.rightChild);
            //后根
            System.out.print(root.value + "  ");
        }

    后根非递归:

    01:

        void postRoot(Node node) {
            Stack<Node> stack = new Stack<>();
            Node root = node;
            //遍历过的节点,值不为null
            Map<Node, Object> map = new HashMap<>();
            map.put(null, 1);
            while (map.get(root) == null || !stack.empty()) {
                if (map.get(root) == null) {//null 不能进栈,输出过的节点,不能进入
                    stack.push(root);
                    stack.push(root.rightChild);
                    root = root.leftChild;
                } else {
                    root = stack.pop();
                    if (root != null && map.get(root.leftChild) != null && map.get(root.rightChild) != null) {
                        System.out.print(root.value + "	");
                        map.put(root, 1);
                    }
                }
            }
        }

    02:

      void postOrder1(Node root) {
            Stack<Node> stack = new Stack();
            Node cur, pre = null;
            stack.push(root);
            while (!stack.empty()) {
                cur = stack.peek();
                if ((cur.leftChild == null && cur.rightChild == null) || (pre != null && (cur.leftChild == pre || cur.rightChild == pre))) {
                    Node temp = stack.pop();
                    System.out.print(temp.value + "	");
                    pre = temp;
                } else {
                    if (cur.rightChild != null)
                        stack.push(cur.rightChild);
                    if (cur.leftChild != null)
                        stack.push(cur.leftChild);
                }
            }
    
        }

    03:

    /**
         * 后序遍历 非递归
         * 双栈法
         *
         * @param root
         */
        public static void postOrder2(Node root) {
            Stack<Node> stack = new Stack();
            Stack<Node> output = new Stack();
            Node node = root;
            while (node != null || !stack.isEmpty()) {
                while (node != null) {
                    stack.push(node);
                    output.push(node);
                    node = node.rightChild;
                }
                if (!stack.isEmpty()) {
                    node = stack.pop();
                    node = node.leftChild;
                }
            }
    
            while (output.size() > 0) {
                Node n = output.pop();
                System.out.print(n.value + "	");
            }
        }

    层次遍历:

     public void levelOrderByStack() {
            if (root == null)
                return;
            Queue<Node> queue = new LinkedList<>();
            queue.add(root);
            int len;
            while ((len = queue.size()) != 0) {
                for (int i = 0; i < len; i++) {
                    Node temp = queue.poll();
                    System.out.print(temp.value + "  ");
                    if (temp.leftChild != null)
                        queue.add(temp.leftChild);
                    if (temp.rightChild != null)
                        queue.add(temp.rightChild);
                }
            }
        }

    递归查找元素:

     public Node findKey1(int value, Node root) {
            if (root == null)
                return null;
            if (root.value.equals(value))
                return root;
            Node left = findKey1(value, root.leftChild);
            Node right = findKey1(value, root.rightChild);
            if (left != null)
                return left;
            if (right != null)
                return right;
            return null;
        }
  • 相关阅读:
    MSMQ 跨服务器读写队列的“消息队列系统的访问被拒绝”的解决方案
    WCF中的ServiceHost初始化两种方式
    正则表达式规则
    常用正则表达式
    Visual Studio 2017使用Asp.Net Core构建Angular4应用程序
    斑马打印机ZT410中文打印
    CNPM 安装 for angularjs
    MAC OS X&Vmware
    HBase
    SQL 和 NoSQL 比较
  • 原文地址:https://www.cnblogs.com/chenglc/p/11029508.html
Copyright © 2011-2022 走看看