package com.pt.data.struct;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

/**
 * 使用队列或者栈进行遍历过程中,入栈和队列的时候必须检查null
 */
public class Tree {
    private Node root;

    private Tree(Node root) {
        this.root = root;
    }

    private static Tree build(Integer[] elements) {
        return new Tree(buildNode(elements, 0));
    }

    /**
     * @param elements 元素
     * @param index 索引
     * @return Node
     */
    private static Node buildNode(Integer[] elements, int index) {
        if (elements.length > index) {
            Node node = new Node(elements[index]);
            node.left = buildNode(elements, 2 * index + 1);
            node.right = buildNode(elements, 2 * index + 2);
            return node;
        } else {
            return null;
        }
    }

    private static Node buildNodeCycle(Integer[] elements, int index) {
        int currentIndex = 0;
        Queue<Node> currentNodes = new LinkedList<>();
        Node root = null;
        if (elements.length > 0) {
            currentNodes.offer(new Node(elements[0]));
        }
        while (elements.length > currentIndex) {

        }
        return null;
    }

    private static void breadthTraversal(Tree tree) {
        Queue<Node> queue = new LinkedList<>();
        queue.offer(tree.root);
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            System.out.println(node.value);
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
    }

    private static void preorderTraversal(Tree tree) {
        Stack<Node> stack = new Stack<>();
        stack.push(tree.root);
        while (!stack.empty()) {
            Node node = stack.pop();
            System.out.println(node.value);
            if (node.right != null) {
                stack.push(node.right);
            }

            if (node.left != null) {
                stack.push(node.left);
            }
        }
    }

    private static void inorderTraversal(Tree tree) {
        Stack<Node> stack = new Stack<>();
        Node node = tree.root;
        stack.push(node);
        while (node != null || !stack.empty()) {
            //左子树入栈
            while (node != null && node.left != null) {
                stack.push(node.left);
                node = node.left;
            }
            //弹出左子树,并入栈其右子树;其后将右子树作为根节点,继续
            if (!stack.empty()) {
                node = stack.pop();
                System.out.println(node.value);
                node = node.right;
                if (node != null) {
                    stack.push(node);
                }
            }
        }

    }

    private static void preorderRecursive(Node node) {
        if (node != null) {
            System.out.println(node.value);
            preorderRecursive(node.left);
            preorderRecursive(node.right);
        }
    }

    private static void inorderRecursive(Node node) {
        if (node != null) {
            inorderRecursive(node.left);
            System.out.println(node.value);
            inorderRecursive(node.right);
        }
    }

    private static void postorderRecursive(Node node) {
        if (node != null) {
            postorderRecursive(node.left);
            postorderRecursive(node.right);
            System.out.println(node.value);
        }
    }

    public static void main(String[] arges) {
        Integer[] elements = new Integer[14];
        for (int i = 0; i < 14; i++) {
            elements[i] = i;
        }
        Tree tree = Tree.build(elements);
        System.out.println("-----广度优先-----");
        Tree.breadthTraversal(tree);
        System.out.println("-----深度优先(前序)-----");
        Tree.preorderTraversal(tree);
        System.out.println("------中序遍历--------");
        Tree.inorderTraversal(tree);

        System.out.println("------前序遍历(递归)----");
        Tree.preorderRecursive(tree.root);
        System.out.println("------中序遍历(递归)----");
        Tree.inorderRecursive(tree.root);
        System.out.println("------后序遍历(递归)----");
        Tree.postorderRecursive(tree.root);
    }

    static class Node {
        Node left;
        Node right;
        Integer value;

        Node(Integer value) {
            this.value = value;
        }

        public Integer getValue() {
            return value;
        }

        public void setValue(Integer value) {
            this.value = value;
        }
    }
}
View Code

 

posted @ 2018-05-03 11:33  沙中世界  阅读(312)  评论(0编辑  收藏  举报