美文网首页
大话数据结构—二叉树(十)

大话数据结构—二叉树(十)

作者: 浅浅星空 | 来源:发表于2019-02-20 15:28 被阅读2次

    1.二叉树3种递归遍历

    public class Tree {
        // 前序遍历
        public static void preOrderSearch(Node root) {
            if (root == null) {
                return;
            }
            System.out.print(root.data);
            preOrderSearch(root.left);
            preOrderSearch(root.right);
        }
    
        // 中序遍历
        static public void midOrderSearch(Node root) {
            if (root == null) {
                return;
            }
            midOrderSearch(root.left);
            System.out.print(root.data);
            midOrderSearch(root.right);
        }
    
        // 后序遍历
        public static void postOrderSearch(Node root) {
            if (root == null) {
                return;
            }
            postOrderSearch(root.left);
            postOrderSearch(root.right);
            System.out.print(root.data);
        }
    
        static class Node {
            Object data;
            Node left;
            Node right;
    
            public Node(Object data) {
                this.data = data;
            }
        }
    
        public static void main(String[] args) {
            Node node = new Node(0);
            Node node1 = new Node(1);
            Node node2 = new Node(2);
            Node node3 = new Node(3);
            Node node4 = new Node(4);
    
            node.left = node1;
            node.right = node2;
            node1.left = node3;
            node1.right = node4;
    
            Tree tree = new Tree();
            tree.preOrderSearch(node);
            System.out.println("-----------");
            tree.midOrderSearch(node);
            System.out.println("-----------");
            tree.postOrderSearch(node);
    
        }
    }
    

    2.层级遍历

    public class LevelTravel {
    
        public static void main(String[] args) {
            Node root = new Node(10);
            Node left = new Node(4);
            Node right = new Node(7);
            Node node1 = new Node(3);
            Node node2 = new Node(6);
            root.left = left;
            root.right = right;
            left.left = node1;
            left.right = node2;
    
            search(root);
    
        }
    
        public static void search(Node root) {
            if (root == null) {
                return;
            }
            LinkedList<Node> queue = new LinkedList<>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                Node temp = queue.poll();
                System.out.println(temp.data);
                if (temp.left != null) {
                    queue.offer(temp.left);
                }
                if (temp.right != null) {
                    queue.offer(temp.right);
                }
            }
        }
    
    
        static class Node {
            Node left;
            Node right;
            Object data;
    
            public Node(Object data) {
                this.data = data;
            }
        }
    
    }
    

    引用原文:https://www.cnblogs.com/llguanli/p/7363657.html

    相关文章

      网友评论

          本文标题:大话数据结构—二叉树(十)

          本文链接:https://www.haomeiwen.com/subject/hxyhyqtx.html