1. 程式人生 > >結合java.util.TreeMap源碼理解紅黑樹

結合java.util.TreeMap源碼理解紅黑樹

pen leaf tails 變化 col 般的 參考 some 解決

前言

本篇將結合JDK1.6的TreeMap源碼,來一起探索紅-黑樹的奧秘。紅黑樹是解決二叉搜索樹的非平衡問題。

當插入(或者刪除)一個新節點時,為了使樹保持平衡,必須遵循一定的規則,這個規則就是紅-黑規則:
1) 每個節點不是紅色的就是黑色的
2) 根總是黑色的
3) 如果節點是紅色的,則它的子節點必須是黑色的(反之倒不一定必須為真)
4) 從跟到葉節點或者空子節點的每條路徑,必須包含相同數目的黑色節點

插入一個新節點

紅-黑樹的插入過程和普通的二叉搜索樹基本一致:從跟朝插入點位置走,在每個節點處通過比較節點的關鍵字相對大小來決定向左走還是向右走。

public V put(K key, V value) {
    Entry<K,V> t = root;
    int cmp;
    Entry<K,V> parent;
    Comparable<? super K> k = (Comparable<? super K>) key;
    do {
        parent = t;
        cmp = k.compareTo(t.key);
        if (cmp < 0) {
            t = t.left;
        } else if (cmp > 0) {
            t = t.right; 
        } else {
            // 註意,return退出方法   
            return t.setValue(value);  
        }
    } while (t != null);
    Entry<K,V> e = new Entry<K,V>(key, value, parent);
    if (cmp < 0) {
        parent.left = e;
    } else {
        parent.right = e;
    }
    fixAfterInsertion(e);
    size++;
    modCount++;
    return null;
}

但是,在紅-黑樹種,找到插入點更復雜,因為有顏色變換和旋轉。fixAfterInsertion()方法就是處理顏色變換和旋轉,需重點掌握它是如何保持樹的平衡(use rotations and the color rules to maintain the tree’s balance)。

下面的討論中,使用X、P、G表示關聯的節點。X表示一個特殊的節點, P是X的父,G是P的父。

X is a node that has caused a rule violation. (Sometimes X refers to a newly inserted node, and sometimes to the child node when a parent and child have a redred conflict.)

On the way down the tree to find the insertion point, you perform a color flip whenever you find a black node with two red children (a violation of Rule 2). Sometimes the flip causes a red-red conflict (a violation of Rule 3). Call the red child X and the red parent P. The conflict can be fixed with a single rotation or a double rotation, depending on whether X is an outside or inside grandchild of G. Following color flips and rotations, you continue down to the insertion point and insert the new node.

After you’ve inserted the new node X, if P is black, you simply attach the new red node. If P is red, there are two possibilities: X can be an outside or inside grandchild of G. If X is an outside grandchild, you perform one rotation, and if it’s an inside grandchild, you perform two. This restores the tree to a balanced state.

按照上面的解釋,討論可分為3個部分,按復雜程度排列,分別是:
1) 在下行路途中的顏色變換(Color flips on the way down)
2) 插入節點之後的旋轉(Rotations after the node is inserted)
3) 在向下路途上的旋轉(Rotations on the way down)

在下行路途中的顏色變換(Color flips on the way down)

Here’s the rule: Every time the insertion routine encounters a black node that has two red children, it must change the children to black and the parent to red (unless the parent is the root, which always remains black)

The flip leaves unchanged the number of black nodes on the path from the root on down through P to the leaf or null nodes.

盡管顏色變換不會違背規則4,但是可能會違背規則3。如果P的父是黑色的,則P由黑色變成紅色時不會有任何問題,但是,如果P的父是紅色的,那麽在P的顏色變化之後,就有兩個紅色節點相連接了。這個問題需要在繼續向下沿著路徑插入新節點之前解決,可以通過旋轉修正這個問題,下文將會看到。

插入節點之後的旋轉(Rotations after the node is inserted)

新節點在插入之前,樹是符合紅-黑規則,在插入新節點之後,樹就不平衡了,此時需要通過旋轉來調整樹的平衡,使之重新符合紅-黑規則。

可能性1:P是黑色的,就什麽事情也不用做。插入即可。

可能性2:P是紅色,X是G的一個外側子孫節點,則需要一次旋轉和一些顏色的變化。
以插入50,25,75,12,6為例,註意節點6是一個外側子孫節點,它和它的父節點都是紅色。

技術分享圖片

在這個例子中,X是一個外側子孫節點而且是左子節點,X是外側子孫節點且為右子節點,是一種與此對稱的情況。通過用50,25,75,87,93創建樹,同理再畫一畫圖,這裏就省略了。

可能性3:P是紅色,X是G的一個內側子孫節點,則需要兩次旋轉和一些顏色的改變。
以插入50,25,75,12,18為例,註意節點18是一個內側子孫節點,它和它的父節點都是紅色。

技術分享圖片

在向下路途上的旋轉(Rotations on the way down)

在插入新節點之前,實際上樹已經違背了紅-黑規則,所以需要插入新節點之前做調整。所以我們本次討論的主題是“在向下路途準備插入新節點時,上面先進行調整,使上面成為標準的紅黑樹後,再進行新節點插入”。

外側子孫節點

以插入50,25,75,12,37,6,18,3為例,例子中違背規則的節點是一個外側子孫節點。

技術分享圖片

內側子孫節點

以插入50,25,75,12,37,31,43為例,例子中違背規則的節點是一個內側子孫節點。
技術分享圖片

紅-黑樹的效率

和一般的二叉搜索樹類似,紅-黑樹的查找、插入和刪除的時間復雜度為O(log2N)。

紅-黑樹的查找時間和普通的二叉搜索樹的查找時間應該幾乎完全一樣。因為在查找過程中並沒用到紅-黑特征。額外的開銷只是每個節點的存儲空間都稍微增加了一點,來存儲紅黑顏色(一個boolean變量)。

final Entry<K, V> getEntry(Object key) {
    Comparable <? super K > k = (Comparable <? super K > ) key;
    Entry<K, V> p = root;
    while (p != null) {
        int cmp = k.compareTo(p.key);
        if (cmp < 0) {
            p = p.left;
        } else if (cmp > 0) {
            p = p.right;
        } else  {
            return p;
        }
    }
    return null;
}

插入和刪除的時間要增加一個常數因子,因為不得不在下行的路徑上和插入點執行顏色變換和旋轉。平均起來一次插入大約需要一次旋轉。

因為在大多數應用中,查找的次數比插入和刪除的次數多,所以應用紅-黑樹取代普通的二叉搜索樹總體上不會增加太多的時間開銷。

參考資料

  1. eclipse如何debug調試jdk源碼
  2. 淺談算法和數據結構: 九 平衡查找樹之紅黑樹

結合java.util.TreeMap源碼理解紅黑樹