基础二叉树-创建、遍历、深度

public class Tree {
    TreeNode root;
    public static void main(String[] args) {
        String[] array = new String[]{"A", "B", "C", "D", "E", "F","G","H","I"};
        Tree tree = new Tree();
        tree.createTree(array);
        tree.prelist(tree.root);
        System.out.print("\n");
        tree.middlelist(tree.root);
        System.out.print("\n");
        tree.afterlist(tree.root);
        System.out.print("\n");
        System.out.print("deep="+tree.maxDepth(tree.root));
        System.out.print("\n");
        System.out.print("deep="+tree.TreeDepth(tree.root));
        System.out.print("\n");
        tree.gdlist(tree.root);

        int[] pre = {1,2,4,7,3,5,6,8};
        int[] middle = {4,7,2,1,5,3,8,6};
        TreeNode treeByPreAndMiddle = tree.createTreeByPreAndMiddle(pre, middle);
        tree.gdlist(treeByPreAndMiddle);
    }
    public class TreeNode {
        TreeNode left;
        TreeNode right;
        T value;
    }
    public  void createTree(T[] array) {
        TreeNode treeNode = create((String[]) array, 0);
        root = treeNode;
    }

/**
根据前序遍历和中序遍历还原树
*/
    public TreeNode createTreeByPreAndMiddle(int[] pre,int[] middle){

        if (pre==null || middle == null || pre.length==0 || middle.length==0) return null;

        TreeNode treeNode = new TreeNode<>();
        int length = pre.length;
        int leftcount = 0;
        int rightcount = 0;
        for (int i = 0; i < middle.length; i++) {
            if (middle[i] == pre[0]){
                leftcount = i;
                rightcount = length-i-1;
                break;
            }
        }


        treeNode.value = pre[0];
        int[] newPre = Arrays.copyOfRange(pre, 1, 1 + leftcount);
        int[] newMiddle = Arrays.copyOfRange(middle, 0, leftcount);
        treeNode.left = createTreeByPreAndMiddle(newPre,newMiddle);

        int[] newPrer = Arrays.copyOfRange(pre,  leftcount+1, length);
        int[] newMiddler = Arrays.copyOfRange(middle, leftcount+1, length);
        treeNode.right = createTreeByPreAndMiddle(newPrer,newMiddler);
        return treeNode;
    }

    public TreeNode create(String[] array, int n) {
        TreeNode stringTreeNode = new TreeNode<>();
        if (n > array.length - 1) return null;
        stringTreeNode.left = create(array,2*n+1);
        stringTreeNode.right = create(array,2*n+2);
        stringTreeNode.value = array[n];
        return stringTreeNode;
    }
    public void prelist(TreeNode treeNode){
        if (treeNode==null) return;
        System.out.print(treeNode.value);
        prelist(treeNode.left);
        prelist(treeNode.right);

    }
    public void middlelist(TreeNode treeNode){
        if (treeNode==null) return;
        middlelist(treeNode.left);
        System.out.print(treeNode.value);
        middlelist(treeNode.right);
    }
    public void afterlist(TreeNode treeNode){
        if (treeNode==null) return;
        afterlist(treeNode.left);
        afterlist(treeNode.right);
        System.out.print(treeNode.value);

    }

    /**
     * 广度遍历
     * @param treeNode
     */
    public void gdlist(TreeNode treeNode){
        if (treeNode==null) return;
        Queue treeNodeQueue = new LinkedList<>();
        treeNodeQueue.add(treeNode);
        while (!treeNodeQueue.isEmpty()){
            TreeNode poll = treeNodeQueue.poll();
            System.out.printf((String) poll.value);
            if (poll.left!=null)treeNodeQueue.add(poll.left);
            if (poll.right!=null)treeNodeQueue.add(poll.right);
        }
    }


    /**
     * 递归求深度
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right)+1;
    }

    /**
     * 广度遍历求深度
     * @param root
     * @return
     */
    public int TreeDepth(TreeNode root) {
        if (root == null) return 0;
        int depth = 0,count = 0,nextcount;
        Queue treeNodeQueue = new LinkedList<>();
        treeNodeQueue.add(root);
        nextcount = 1;
        while(!treeNodeQueue.isEmpty()){
            TreeNode poll = treeNodeQueue.poll();
            count++;
            if (poll.left!=null) treeNodeQueue.add(poll.left);
            if (poll.right!=null) treeNodeQueue.add(poll.right);
            if (count== nextcount){
                count = 0;
                depth++;
                nextcount = treeNodeQueue.size();
            }
        }
        return depth;
    }

}

你可能感兴趣的:(其他)