淺談算法和數據結構: 二叉查找樹

無序鏈表和有序數組,無序鏈表在插入的時候具有較高的靈活性,而有序數組在查找時具有較高的效率,本文介紹的二叉查找樹(Binary Search Tree,BST)這一數據結構綜合了以上兩種數據結構的優點。

二叉查找樹具有很高的靈活性,對其優化可以生成平衡二叉樹,紅黑樹等高效的查找和插入數據結構,後文會一一介紹。

一 定義

二叉查找樹(Binary Search Tree),也稱有序二叉樹(ordered binary tree),排序二叉樹(sorted binary tree),是指一棵空樹或者具有下列性質的二叉樹:

1. 若任意節點的左子樹不空,則左子樹上所有結點的值均小於它的根結點的值;

2. 若任意節點的右子樹不空,則右子樹上所有結點的值均大於它的根結點的值;

3. 任意節點的左、右子樹也分別爲二叉查找樹。

4. 沒有鍵值相等的節點(no duplicate nodes)。

如下圖,這個是普通的二叉樹:

binary tree

在此基礎上,加上節點之間的大小關係,就是二叉查找樹:

binary search tree

二 實現

在實現中,我們需要定義一個內部類Node,它包含兩個分別指向左右節點的Node,一個用於排序的Key,以及該節點包含的值Value,還有一個記錄該節點及所有子節點個數的值Number。

public class BinarySearchTreeSymbolTable<TKey, TValue> : SymbolTables<TKey, TValue> where TKey : IComparable<TKey>, IEquatable<TValue>
{
    private Node root;
    private class Node
    {
        public Node Left { get; set; }
        public Node Right { get; set; }
        public int Number { get; set; }
        public TKey Key { get; set; }
        public TValue Value { get; set; }

        public Node(TKey key, TValue value, int number)
        {
            this.Key = key;
            this.Value = value;
            this.Number = number;
        }
    }
...
}

查找

查找操作和二分查找類似,將key和節點的key比較,如果小於,那麼就在Left Node節點查找,如果大於,則在Right Node節點查找,如果相等,直接返回Value。

 SearchhitAndSearchMissinBST

該方法實現有迭代和遞歸兩種。

遞歸的方式實現如下:

public override TValue Get(TKey key)
{
    TValue result = default(TValue);
    Node node = root;
    while (node != null)
    {

        if (key.CompareTo(node.Key) > 0)
        {
            node = node.Right;
        }
        else if (key.CompareTo(node.Key) < 0)
        {
            node = node.Left;
        }
        else
        {
            result = node.Value;
            break;
        }
    }
    return result;
}

迭代的如下:

public TValue Get(TKey key)
{
    return GetValue(root, key);
}

private TValue GetValue(Node root, TKey key)
{
    if (root == null) return default(TValue);
    int cmp = key.CompareTo(root.Key);
    if (cmp > 0) return GetValue(root.Right, key);
    else if (cmp < 0) return GetValue(root.Left, key);
    else return root.Value;
}

插入

插入和查找類似,首先查找有沒有和key相同的,如果有,更新;如果沒有找到,那麼創建新的節點。並更新每個節點的Number值,代碼實現如下:

public override void Put(TKey key, TValue value)
{
    root = Put(root, key, value);
}

private Node Put(Node x, TKey key, TValue value)
{
    //如果節點爲空,則創建新的節點,並返回
    //否則比較根據大小判斷是左節點還是右節點,然後繼續查找左子樹還是右子樹
    //同時更新節點的Number的值
    if (x == null) return new Node(key, value, 1);
    int cmp = key.CompareTo(x.Key);
    if (cmp < 0) x.Left = Put(x.Left, key, value);
    else if (cmp > 0) x.Right = Put(x.Right, key, value);
    else x.Value = value;
    x.Number = Size(x.Left) + Size(x.Right) + 1;
    return x;
}

private int Size(Node node)
{
    if (node == null) return 0;
    else return node.Number;
}

  插入操作圖示如下:

insert into BST

下面是插入動畫效果:

insert into BST

隨機插入形成樹的動畫如下,可以看到,插入的時候樹還是能夠保持近似平衡狀態:

Insert keys random order in BST

最大最小值

如下圖可以看出,二叉查找樹的最大最小值是有規律的:

the max and min item in bst

從圖中可以看出,二叉查找樹中,最左和最右節點即爲最小值和最大值,所以我們只需迭代調用即可。

public override TKey GetMax()
{
    TKey maxItem = default(TKey);
    Node s = root;
    while (s.Right != null)
    {
        s = s.Right;
    }
    maxItem = s.Key;
    return maxItem;
}

public override TKey GetMin()
{
    TKey minItem = default(TKey);
    Node s = root;
    while (s.Left != null)
    {
        s = s.Left;
    }
    minItem = s.Key;
    return minItem;
}

以下是遞歸的版本:

public TKey GetMaxRecursive()
{
    return GetMaxRecursive(root);
}

private TKey GetMaxRecursive(Node root)
{
    if (root.Right == null) return root.Key;
    return GetMaxRecursive(root.Right);
}

public TKey GetMinRecursive()
{
    return GetMinRecursive(root);
}

private TKey GetMinRecursive(Node root)
{
    if (root.Left == null) return root.Key;
    return GetMinRecursive(root.Left);
}

Floor和Ceiling

查找Floor(key)的值就是所有<=key的最大值,相反查找Ceiling的值就是所有>=key的最小值,下圖是Floor函數的查找示意圖:

floor  function in BST

以查找Floor爲例,我們首先將key和root元素比較,如果key比root的key小,則floor值一定在左子樹上;如果比root的key大,則有可能在右子樹上,當且僅當其右子樹有一個節點的key值要小於等於該key;如果和root的key相等,則floor值就是key。根據以上分析,Floor方法的代碼如下,Ceiling方法的代碼類似,只需要把符號換一下即可:

public TKey Floor(TKey key)
{
    Node x = Floor(root, key);
    if (x != null) return x.Key;
    else return default(TKey);
}

private Node Floor(Node x, TKey key)
{
    if (x == null) return null;
    int cmp = key.CompareTo(x.Key);
    if (cmp == 0) return x;
    if (cmp < 0) return Floor(x.Left, key);
    else
    {
        Node right = Floor(x.Right, key);
        if (right == null) return x;
        else return right;
    }
}

刪除

刪除元素操作在二叉樹的操作中應該是比較複雜的。首先來看下比較簡單的刪除最大最小值得方法。

以刪除最小值爲例,我們首先找到最小值,及最左邊左子樹爲空的節點,然後返回其右子樹作爲新的左子樹。操作示意圖如下:

delete minimun in BST

代碼實現如下:

public void DelMin()
{
    root = DelMin(root);
}

private Node DelMin(Node root)
{
    if (root.Left == null) return root.Right;
    root.Left = DelMin(root.Left);
    root.Number = Size(root.Left) + Size(root.Right) + 1;
    return root;
}

刪除最大值也是類似。

現在來分析一般情況,假定我們要刪除指定key的某一個節點。這個問題的難點在於:刪除最大最小值的操作,刪除的節點只有1個子節點或者沒有子節點,這樣比較簡單。但是如果刪除任意節點,就有可能出現刪除的節點有0個,1 個,2個子節點的情況,現在來逐一分析。

當刪除的節點沒有子節點時,直接將該父節點指向該節點的link設置爲null。

 delete node which has 0 childrens

當刪除的節點只有1個子節點時,將該自己點替換爲要刪除的節點即可。

delete node which has 1 childrens

當刪除的節點有2個子節點時,問題就變複雜了。

假設我們刪除的節點t具有兩個子節點。因爲t具有右子節點,所以我們需要找到其右子節點中的最小節點,替換t節點的位置。這裏有四個步驟:

1. 保存帶刪除的節點到臨時變量t

2. 將t的右節點的最小節點min(t.right)保存到臨時節點x

3. 將x的右節點設置爲deleteMin(t.right),該右節點是刪除後,所有比x.key最大的節點。

4. 將x的做節點設置爲t的左節點。

整個過程如下圖:

delete node which has 2 childrens

對應代碼如下:

public void Delete(TKey key)
{
    root =Delete(root, key);
        
}

private Node Delete(Node x, TKey key)
{
    int cmp = key.CompareTo(x.Key);
    if (cmp > 0) x.Right = Delete(x.Right, key);
    else if (cmp < 0) x.Left = Delete(x.Left, key);
    else
    {
        if (x.Left == null) return x.Right;
        else if (x.Right == null) return x.Left;
        else
        {
            Node t = x;
            x = GetMinNode(t.Right);
            x.Right = DelMin(t.Right);
            x.Left = t.Left;
        }
    }
    x.Number = Size(x.Left) + Size(x.Right) + 1;
    return x;
}

private Node GetMinNode(Node x)
{
    if (x.Left == null) return x;
    else return GetMinNode(x.Left); 
}

以上二叉查找樹的刪除節點的算法不是完美的,因爲隨着刪除的進行,二叉樹會變得不太平衡,下面是動畫演示。

delete node in BST

三 分析

二叉查找樹的運行時間和樹的形狀有關,樹的形狀又和插入元素的順序有關。在最好的情況下,節點完全平衡,從根節點到最底層葉子節點只有lgN個節點。在最差的情況下,根節點到最底層葉子節點會有N各節點。在一般情況下,樹的形狀和最好的情況接近。

BST Tree shape

在分析二叉查找樹的時候,我們通常會假設插入的元素順序是隨機的。對BST的分析類似與快速排序中的查找:

BST and quick sort partition

BST中位於頂部的元素就是快速排序中的第一個劃分的元素,該元素左邊的元素全部小於該元素,右邊的元素均大於該元素。

對於N個不同元素,隨機插入的二叉查找樹來說,其平均查找/插入的時間複雜度大約爲2lnN,這個和快速排序的分析一樣,具體的證明方法不再贅述,參照快速排序。

 

四 總結

有了前篇文章 二分查找的分析,對二叉查找樹的理解應該比較容易。下面是二叉查找樹的時間複雜度:

analysis of binary search tree

它和二分查找一樣,插入和查找的時間複雜度均爲lgN,但是在最壞的情況下仍然會有N的時間複雜度。原因在於插入和刪除元素的時候,樹沒有保持平衡。我們追求的是在最壞的情況下仍然有較好的時間複雜度,這就是後面要講的平衡查找樹的內容了。下文首先講解平衡查找樹的最簡單的一種:2-3查找樹。

希望本文對您瞭解二叉查找樹有所幫助。

發佈了128 篇原創文章 · 獲贊 36 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章