【算法】-- 【二叉樹的實現、層序遍歷二叉樹、已知先序中序遍歷二叉樹,求後序遍歷二叉樹、求二叉樹中結點的最大距離】

01 如何實現二叉樹

首先定義樹的結點

public class Node {
    public int data;
    public Node left;
    public Node right;
    public Node (int data){
        this.data=data;
        this.left=null;
        this.right=null;
    }

    public int leftMaxDistance;//左子樹距根結點的最大距離
    public int rightMaxDistance;//右子數距根結點的最大距離


}

二叉樹的實現

public class BinaryTree {
    private Node root;
    public BinaryTree(){
        root=null;
    }
    public Node getRoot(){
        return root;
    }

    //將data插入到排序二叉樹中
    public void insert(int data){
        Node newNode = new Node(data);
        if (root==null){
            root=newNode;
        }else {
            Node current=root;
            Node parent;
            while (true){//尋找插入的位置
                parent =current;
                if (data<current.data){
                    current=current.left;
                    if (current==null){
                        parent.left=newNode;
                        return;
                    }
                }else{
                    current=current.right;
                    if (current==null){
                        parent.right=newNode;
                        return;
                    }
                }

            }
        }
    }

    //將數值輸入構建二叉樹
    public void buildTree(int[]data){

        for (int i=0;i<data.length;i++){
            insert(data[i]);
        }
    }


    //中序遍歷方法遞歸實現
    public void inOrder(Node localRoot){
        if (localRoot!=null){
            inOrder(localRoot.left);
            System.out.print(localRoot.data+" ");
            inOrder(localRoot.right);
        }
    }

    public void inOrder(){
        this.inOrder(this.root);
    }


    //先序遍歷方法遞歸實現
    public void preOrder(Node localRoot){
        if (localRoot!=null){
            System.out.print(localRoot.data+" ");
            preOrder(localRoot.left);
            preOrder(localRoot.right);
        }
    }

    public void preOrder(){
        this.preOrder(this.root);
    }

    //後續遍歷方法遞歸實現
    public void postOrder(Node localRoot){
        if (localRoot!=null){
            postOrder(localRoot.left);
            postOrder(localRoot.right);
            System.out.print(localRoot.data+" ");
        }
    }

    public void postOrder(){
        this.postOrder(this.root);
    }

    public static void main(String[] args) {
        BinaryTree biTree=new BinaryTree();
        int[] data={2,8,7,4,9,3,1,6,7,5};
        biTree.buildTree(data);
        System.out.println("二叉樹的中序遍歷");

        biTree.inOrder();
        System.out.println();
        System.out.println("二叉樹的先序遍歷");
        biTree.preOrder();
        System.out.println();
        System.out.println("二叉樹的後序遍歷");
        biTree.postOrder();
    }

}

02 如何層序遍歷二叉樹

可以用隊列實現二叉樹的層序遍歷。主要思路如下:先將根結點放入隊列中,然後每次都從隊列中取出一個結點打印該結點的值,若這個結點有子結點,則將它的子結點放入隊列,直到隊列爲空.

public static void layerTranverse(Node root){
        if(root==null){
            return;
        }
        Queue<Node> q = new LinkedList<>();

        ((LinkedList<Node>) q).add(root);
        while (!q.isEmpty()){
            Node n=q.poll();
            System.out.print(n.data+" ");
            if (n.left!=null)
                ((LinkedList<Node>) q).add(n.left);
            if (n.right!=null)
                ((LinkedList<Node>) q).add(n.right);
        }
    }

    public static void main(String[] args) {
        BinaryTree biTree=new BinaryTree();
        int[] data={2,8,7,4,9,3,1,6,7,5};
        biTree.buildTree(data);
        layerTranverse(biTree.getRoot());
    }

03 已知先序遍歷和中序遍歷,如何求後序遍歷

public class BinaryTree1 {
    private Node root;
    public BinaryTree1(){
        root=null;
    }

    //後序遍歷方法,遞歸實現
    public void postOrder(Node localRoot){
        if (localRoot!=null){
            postOrder(localRoot.left);
            postOrder(localRoot.right);
            System.out.print(localRoot.data+" ");
        }
    }

    public void postOrder(){
        this.postOrder(this.root);
    }

    public void initTree(int[]preOrder,int[]inOrder){
        this.root=this.initTree(preOrder,0,preOrder.length-1,inOrder,0,inOrder.length-1);

    }

    private Node initTree(int[] preOrder, int start1, int end1, int[] inOrder, int start2, int end2) {
        if (start1>end1||start2>end2){
            return null;
        }
        int rootData=preOrder[start1];

        Node head = new Node(rootData);
        //找到根結點所在的位置
        int rootIndex=findIndexInArray(inOrder,rootData,start2,end2);
        int offSet=rootIndex-start2-1;
        //構建左子樹
        Node left=initTree(preOrder,start1+1,start1+1+offSet,inOrder,start2,start2+offSet);
        //構建右子數
        Node right=initTree(preOrder,start1+offSet+2,end1,inOrder,rootIndex+1,end2);

        head.left=left;
        head.right=right;
        return head;
    }

    private int findIndexInArray(int[] a, int x, int begin, int end) {
        for (int i=begin;i<=end;i++){
            if (a[i]==x){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        BinaryTree1 biTree=new BinaryTree1();
        int[] preOrder={1,2,4,8,9,5,10,3,6,7};
        int[] inOrder={8,4,9,2,10,5,1,6,3,7};
        biTree.initTree(preOrder,inOrder);
        System.out.println("二叉樹的後序遍歷:");
        biTree.postOrder();
    }
}

04 如何求二叉樹中結點的最大距離

public class BinaryTree2 {
    private int maxLen=0;
    private int max(int a,int b){
        return a>b?a:b;
    }

    public void FindMaxDistance(Node root){
        if (root==null)
            return;
        if (root.left==null)
            root.leftMaxDistance=0;
        if (root.right==null)
            root.rightMaxDistance=0;
        if (root.left!=null)
            FindMaxDistance(root.left);
        if (root.right!=null)
            FindMaxDistance(root.right);
        //計算左子樹中距離更節點的最大距離
        root.leftMaxDistance=max(root.left.leftMaxDistance,root.left.rightMaxDistance)+1;
        //計算右子數中距離根結點的最大距離
        root.rightMaxDistance=max(root.right.leftMaxDistance,root.right.rightMaxDistance)+1;
        //獲取二叉樹所有節點的最大距離
        if (root.leftMaxDistance+root.rightMaxDistance>maxLen)
            maxLen=root.leftMaxDistance+root.rightMaxDistance;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章