根據前序和中序遍歷數組,構建出二叉樹邏輯結構,並以後序遍歷方式打印,最後生成此樹的鏡像樹

/**
 * 根據前序和中序遍歷數組,構建出二叉樹邏輯結構,並以後序遍歷方式打印,最後生成此樹的鏡像樹
 * Creted by Dean on 2019-04-11.
 */
public class GenBinaryTree {

    public static void main(String[] args) {
        int[] preOrder = {1, 2, 4, 7, 3, 5, 6, 8};
        int[] middleOrder = {4, 7, 2, 1, 5, 3, 8, 6};

        TreeNode treeNode = genBinaryTree(preOrder, middleOrder);

        System.out.println("後序遍歷結果");
        treeNode.postOrderPrint();//後序打印結果,正確結果應該是{7, 4, 2, 5, 8, 6, 3, 1}

        System.out.println("鏡像前序遍歷結果");
        treeNode.mirrorTreePrint();//鏡像樹打印
        treeNode.preOrderPrint();//再次前序打印觀察是否正確的生成了 鏡像樹

        TreeNode result = treeNode.frontSearch(3);
        System.out.println("查找結果爲" + result);

    }

    private static TreeNode genBinaryTree(int[] preOrder, int[] middleOrder){
        return genBinaryTree(preOrder, 0, preOrder.length - 1, middleOrder, 0, middleOrder.length - 1);
    }


    private static TreeNode genBinaryTree(int[] preOrder, int ps, int pe, int[] middleOrder, int ms, int me){
        if (ps > pe || ms > me){
            //System.out.println("參數越界返回null");
            return null;
        }

        int rootValue = preOrder[ps];//前序的第一個元素肯定是根
        TreeNode rootNode = new TreeNode(rootValue);;

        //找到根節點在中序遍歷數組中的下標
        for (int i = ms; i <= me; i++){
            if (middleOrder[i] == rootValue){//找到索引了
                //參數說明,前序左子樹範圍= ps + 1, ps + i,     中序左子樹範圍= ms, i - 1
                rootNode.left = genBinaryTree(preOrder, ps + 1, ps + i, middleOrder, ms, i - 1);
                //參數說明,前序右子樹範圍= i - ms + ps + 1, pe,中序右子樹範圍= i + 1, me
                rootNode.right = genBinaryTree(preOrder, i - ms + ps + 1, pe, middleOrder, i + 1, me);
                //可以畫個圖出來,拿第一個根來演算一下參數就出來了
                break;
            }
        }
        return rootNode;
    }

}

//二叉樹 節點對象 聲明
class TreeNode {

    public int value;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int value) {
        this.value = value;
    }

    //前序遍歷
    public void preOrderPrint() {

        System.out.println(this.value);

        if (this.left != null) {
            this.left.preOrderPrint();
        }
        if(this.right != null){
            this.right.preOrderPrint();
        }
    }

    //中序遍歷
    public void inOrderPrint() {
        if (this.left != null) {
            this.left.inOrderPrint();
        }

        System.out.println(this.value);

        if(this.right != null){
            this.right.inOrderPrint();
        }
    }

    //後序遍歷
    public void postOrderPrint() {
        if (this.left != null) {
            this.left.postOrderPrint();
        }
        if(this.right != null){
            this.right.postOrderPrint();
        }

        System.out.println(this.value);
    }

    //生成鏡像樹
    public void mirrorTreePrint(){
        TreeNode temp = this.left;
        this.left = this.right;
        this.right  = temp;

        if (this.left != null) {
            this.left.mirrorTreePrint();
        }
        if (this.right != null) {
            this.right.mirrorTreePrint();
        }
    }

    //前序查找,中序及後序查找類似
    public TreeNode frontSearch(int i){
        if (i == this.value){
            return this;
        }
        TreeNode result = null;
        if (this.left != null){
            result = this.left.frontSearch(i);
            if (result != null){
                return result;
            }
        }
        if (this.right != null){
            result = this.right.frontSearch(i);
            if (result != null){
                return result;
            }
        }
        return result;
    }


    @Override
    public String toString() {
        String leftValue = this.left == null ? "null" : String.valueOf(this.left.value);
        String righttValue = this.right == null ? "null" : String.valueOf(this.right.value);
        return "value=" + this.value + ", leftValue=" + leftValue + ", rightValue=" + righttValue;
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章