1. 程式人生 > 實用技巧 >二叉排序樹(二叉搜尋樹、二叉查詢樹)BST

二叉排序樹(二叉搜尋樹、二叉查詢樹)BST


概述

對於一組元素 [7, 3, 10, 12, 5, 1, 9] 可以有很多種儲存方式,但無論使用哪種資料結構,都或多或少有缺陷。比如使用線性結構儲存,排序方便,但查詢效率低。二叉排序樹的特點就是能在保證元素有序的同時,提高查詢的效率。


二叉排序樹的定義

二叉排序樹,也叫二叉查詢樹,二叉搜尋樹,英文名 Binary Sort Tree(BST)。對於二叉樹中的任何一個非葉子結點,要求左子結點比當前結點值小,右子結點比當前結點值大。空樹也可以認為是一個二叉排序樹。

序列 [7, 3, 10, 12, 5, 1, 9] 以二叉排序樹儲存的結構如圖:


建立二叉排序樹 & 新增 & 查詢 & 遍歷

值得注意的是,對二叉排序樹作中序遍歷,結果正好是一個有序序列,這也是排序一詞的根據。

public class Node {

    private int value;
    private Node left;
    private Node right;

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

    /**
     * 向子樹新增結點
     * @param node 要新增的結點
     */
    public void add(Node node) {
        if (node != null) {
            // 新增的結點比當前結點的值小
            if (node.value < this.value) {
                // 左結點為空
                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 midShow() {
        // 輸出左結點內容
        if (left != null) {
            left.midShow();
        }
        // 輸出當前結點內容
        System.out.println(value);
        // 輸出右結點內容
        if (right != null) {
            right.midShow();
        }
    }

    /**
     * 查詢結點
     * @param value 目標結點的值
     * @return  目標結點
     */
    public Node search(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value) {
            if (left == null) {
                return null;
            }
            return left.search(value);
        } else {
            if (right == null) {
                return null;
            }
            return right.search(value);
        }
    }
}
public class BinarySortTree {

    private Node root;

    /**
     * 向二叉排序樹新增結點
     * @param node
     */
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    /**
     * 中序遍歷
     */
    public void midShow() {
        if (root != null) {
            root.midShow();
        }
    }

    /**
     * 查詢結點
     * @param value 目標結點的值
     * @return  目標結點
     */
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }
}

刪除結點

二叉排序樹的刪除操作相對麻煩些,我們不能像以前那樣直接刪除結點對應的整個子樹,而是要把子結點保留下來,並重新拼接成新的排序二叉樹。針對不同的情況,也有不同的應對策略:

  • 刪除葉子結點。直接砍掉就好了,不會對其他結點有影響。
  • 刪除只有一個子結點的結點。子結點代替原結點的位置。
  • 刪除有兩個子結點的結點。被刪除結點同時也是對應二叉排序子樹的根結點,根據二叉排序樹的性質,根結點就是序列的中間值,所以要補上中間值的位置,要用中間值的後一位的元素(對應右子樹的最小結點)或前一位元素(對應左子樹的最大結點)
public class BinarySortTree {

    private Node root;

	......

    /**
     * 刪除結點
     * @param value 要刪除結點的值
     */
    public void delete(int value) {
        if (root != null) {
            // 找到目標結點
            Node target = search(value);
            if (target != null) {
                // 找到目標結點的父結點
                Node parent = searchParent(value);
                // 要刪除的結點是葉子結點
                if (target.getLeft() == null && target.getRight() == null) {
                    // 要刪除的結點是父結點的左子結點
                    if (parent.getLeft().getValue() == value) {
                        parent.setLeft(null);
                    // 要刪除的結點是父結點的右子結點
                    } else {
                        parent.setRight(null);
                    }
                // 要刪除的結點有兩個子結點
                } else if (target.getLeft() != null && target.getRight() != null) {
                    // 刪除右子樹中值最小的結點,並獲取該結點的值
                    int min = deleteMin(target.getRight());
                    // 替換目標結點的值
                    target.setValue(min);
                // 要刪除的結點只有一個子結點
                } else {
                    // 有左子結點
                    if (target.getLeft() != null) {
                        // 要刪除的結點是父結點的左子結點
                        if (parent.getLeft().getValue() == value) {
                            // 父結點的左子結點指向目標結點的左子結點
                            parent.setLeft(target.getLeft());
                        // 要刪除的結點是父結點的右子結點
                        } else {
                            // 父結點的右子結點指向目標結點的左子結點
                            parent.setRight(target.getLeft());
                        }
                    // 有右子結點
                    } else {
                        // 要刪除的結點是父結點的左子結點
                        if (parent.getLeft().getValue() == value) {
                            // 父結點的左子結點指向目標結點的左子結點
                            parent.setLeft(target.getRight());
                        // 要刪除的結點是父結點的右子結點
                        } else {
                            parent.setRight(target.getRight());
                        }
                    }
                }
            }
        }
    }

    /**
     * 刪除最小值結點
     * @param node  目標二叉樹的根結點
     * @return
     */
    public int deleteMin(Node node) {

        Node target = node;
        while (target.getLeft() != null) {
            target = target.getLeft();
        }
        delete(target.getValue());
        return target.getValue();
    }

    /**
     * 查詢父結點
     * @param value 目標父結點的子結點的值
     * @return 目標父結點
     */
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }
}
public class Node {

    private int value;
    private Node left;
    private Node right;

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

	......

    /**
     * 查詢結點
     * @param value 目標結點的值
     * @return  目標結點
     */
    public Node search(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value) {
            if (left == null) {
                return null;
            }
            return left.search(value);
        } else {
            if (right == null) {
                return null;
            }
            return right.search(value);
        }
    }

    /**
     * 查詢父結點
     * @param value 目標父結點的子結點的值
     * @return 目標父結點
     */
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (this.left != null && this.value > value) {
                return this.left.searchParent(value);
            } else if (this.right != null && this.value < value) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }
}