數據結構之二叉排序樹

二叉排序數

1.二叉排序樹介紹

二叉排序樹:BST: (Binary Sort(Search) Tree), 對於二叉排序樹的任何一個非葉子節點,要求左子節點的值比當前節點的值小,右子節點的值比當前節點的值大。

特別說明:如果有相同的值,可以將該節點放在左子節點或右子節點

比如針對前面的數據 (7, 3, 10, 12, 5, 1, 9) ,對應的二叉排序樹爲:

代碼實現:

package cn.smallmartial.binarySortTree;

/**
 * @Author smallmartial
 * @Date 2019/6/21
 * @Email [email protected]
 */
public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        System.out.println("中序遍歷二叉樹");
        binarySortTree.infixOrder();
    }
}
//創建二叉排序樹
class BinarySortTree{
    private Node root;
    //添加節點的方法
    public void add(Node node){
        if (root == null){
            root = node;
        }else {
            root.add(node);
        }
    }
    //中序遍歷
    public void infixOrder(){
        if (root != null){
            root.infixOrder();
        }else {
            System.out.println("二叉樹爲空,不能遍歷");
        }
    }
}

//創建Node結點
class Node{

    int value;
    Node left;
    Node right;

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

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    //添加節點方法
    public void add(Node node){
        if (node == null){
            return;
        }

        //判斷傳入結點的值,和當前子樹的根節點的關係
        if (node.value < this.value){
            //如果當前左子節點爲null
            if (this.left == null){
                this.left = node;
            }else {
                this.left.add(node);
            }
        }else {
            if (this.right == null){
                this.right = node;
            }else {
                this.right.add(node);
            }
        }
    }

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

        System.out.println(this);

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

    }

}

2.二叉排序樹的刪除

二叉排序樹的刪除情況比較複雜,有下面三種情況需要考慮

1)刪除葉子節點 (比如:2, 5, 9, 12)

2)刪除只有一顆子樹的節點 (比如:1)

3)刪除有兩顆子樹的節點. (比如:7, 3,10 )

2.1思路分析

  • 第一種情況:
    刪除葉子節點 (比如:2, 5, 9, 12)
    思路
    (1) 需求先去找到要刪除的結點 targetNode
    (2) 找到targetNode 的 父結點 parent
    (3) 確定 targetNode 是 parent的左子結點 還是右子結點
    (4) 根據前面的情況來對應刪除
    左子結點 parent.left = null
    右子結點 parent.right = null;

  • 第二種情況: 刪除只有一顆子樹的節點 比如 1
    思路
    (1) 需求先去找到要刪除的結點 targetNode
    (2) 找到targetNode 的 父結點 parent
    (3) 確定targetNode 的子結點是左子結點還是右子結點
    (4) targetNode 是 parent 的左子結點還是右子結點
    (5) 如果targetNode 有左子結點

    5.1 如果 targetNode 是 parent 的左子結點
    parent.left = targetNode.left;
    5.2 如果 targetNode 是 parent 的右子結點
    parent.right = targetNode.left;
    (6) 如果targetNode 有右子結點
    6.1 如果 targetNode 是 parent 的左子結點
    parent.left = targetNode.right;
    6.2 如果 targetNode 是 parent 的右子結點
    parent.right = targetNode.right

  • 情況三 : 刪除有兩顆子樹的節點. (比如:7, 3,10 )
    思路
    (1) 需求先去找到要刪除的結點 targetNode
    (2) 找到targetNode 的 父結點 parent
    (3) 從targetNode 的右子樹找到最小的結點
    (4) 用一個臨時變量,將 最小結點的值保存 temp = 11
    (5) 刪除該最小結點
    (6) targetNode.value = temp

2.2代碼實現

package cn.smallmartial.binarySortTree;

/**
 * @Author smallmartial
 * @Date 2019/6/21
 * @Email [email protected]
 */
public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9,2};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        System.out.println("中序遍歷二叉樹");
        binarySortTree.infixOrder();

       // binarySortTree.delNode(2);
        //binarySortTree.delNode(5);
      //  binarySortTree.delNode(1);
        binarySortTree.delNode(7);

        System.out.println("刪除鍵節點後");
        binarySortTree.infixOrder();
    }
}
//創建二叉排序樹
class BinarySortTree{
    private Node root;

    //查找要刪除的節點
    public Node search(int value){
        if (root == null){
            return null;
        }else {
            return root.search(value);
        }
    }

    //查找父節點
    public Node searchParent(int value){
        if (root == null){
            return null;
        }else {
            return root.searchParent(value);
        }
    }

    /**
     *
     * @param node 傳入節點 作爲二叉排序樹的根節點
     * @return
     */
    public int delRightTreeMin(Node node){
        Node target = node;
        while (target.left != null){
            target = target.left;
        }
        //刪除最小節點
        delNode(target.value);
        return target.value;
    }

    //刪除節點
    public void delNode(int value){
        if (root == null){
            return;
        }else {
            Node targetNode = search(value);
            if (targetNode == null){
                return;
            }

            if (root.left == null && root.right == null){
                root = null;
                return;
            }

            //去找到targetNode的父節點
            Node parent = searchParent(value);

            if (targetNode.left == null && targetNode.right == null){
                //判斷targetNode是父節點的左子節點還是右子節點
                if (parent.left != null && parent.left.value == value){
                    parent.left  = null;
                }else if (parent.right != null && parent.right.value == value){
                    parent.right = null;
                }
            }else if (targetNode.left!= null && targetNode.right !=null){//刪除有2顆子樹的節點
                int minVal = delRightTreeMin(targetNode.right);
                targetNode.value = minVal;
            }else {//刪除只有一顆子樹的節點
            if (targetNode.left !=null){
                if (parent.left.value == value){
                    parent.left = targetNode.left;
                }else {
                    parent.right = targetNode.left;
                }
            }else {
                if (parent.left.value == value){
                    parent.left = targetNode.right;
                }else {
                    parent.right = targetNode.right;
                }
            }

            }
        }


    }

    //添加節點的方法
    public void add(Node node){
        if (root == null){
            root = node;
        }else {
            root.add(node);
        }
    }
    //中序遍歷
    public void infixOrder(){
        if (root != null){
            root.infixOrder();
        }else {
            System.out.println("二叉樹爲空,不能遍歷");
        }
    }
}

//創建Node結點
class Node{

    int value;
    Node left;
    Node right;

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

    /**
     *查找刪除的節點
     * @param value
     * @return
     */
    //查找刪除節點
    public Node search(int value){
        if (value == this.value){
            return this;
        }else  if(value <this.value){//如果查找當前的值小於當前節點,向左遞歸查找
            //如果左子節點爲空
            if (this.left == null){
                return null;
            }
            return this.left.search(value);
        }else {//如果查找當前的值不小於當前節點,向小遞歸查找
            if (this.right == null){
                return null;
            }
            return this.right.search(value);

        }
    }
    //查找要刪除節點的父節點
    public Node searchParent(int value){
        if (this.left != null && this.left.value == value ||(this.right != null && this.right.value == value)){
            return this;
        }else {
            //如果查找的值小於當前結點的值,並且當前節點的左子節點不爲空
            if (value < this.value && this.left != null){
                return this.left.searchParent(value);
            }else if (value >= this.value && this.right != null){
                return this.right.searchParent(value);
            }else {
                return null;
            }
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    //添加節點方法
    public void add(Node node){
        if (node == null){
            return;
        }

        //判斷傳入結點的值,和當前子樹的根節點的關係
        if (node.value < this.value){
            //如果當前左子節點爲null
            if (this.left == null){
                this.left = node;
            }else {
                this.left.add(node);
            }
        }else {
            if (this.right == null){
                this.right = node;
            }else {
                this.right.add(node);
            }
        }
    }

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

        System.out.println(this);

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

    }

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