本文將主要講述平衡二叉樹中的紅黑樹,紅黑樹是一種我們經常使用的樹,相較於 AVL 樹他無論是增加還是刪除節點,其結構的變化都能控制在常樹次;在 JDK 中的 TreeMap 同樣也是使用紅黑樹實現的;
一、結構概述
紅黑樹是在AVL 樹平衡條件的基礎上,進一步放寬條件,從而使得紅黑樹在動態變化的時候,其結構的變化在常數次;其標准大致可以表示為; 任一節點左、右子樹的高度,相差不得超過兩倍。
同他的名字,紅黑樹的節點是有顏色的,如圖所示:

其性質如下:
- 樹根始終為黑色
- 外部節點均為黑色(圖中的 leaf 節點,通常在表述的時候會省略)
- 紅色節點的孩子節點必為黑色(通常插入的節點為紅色)
- 從任一外部節點到根節點的沿途,黑節點的數目相等
(2,4)B 樹,如果將紅黑樹的紅色節點和其父節點合並為一個超級節點,則其結構和(2,4)B 樹 的結構完全一樣,所以在學習紅黑樹的時候,可以對照 B 的轉換方法,幫助理解;
public class RBTree<T extends Comparable<T>> {
private static final boolean RED = false;
private static final boolean BLACK = true;
private RBTNode<T> root; // 根結點
public class RBTNode<T extends Comparable<T>> {
boolean color; // 顏色
T key; // 關鍵字(鍵值)
RBTNode<T> left; // 左孩子
RBTNode<T> right; // 右孩子
RBTNode<T> parent; // 父結點
}
}
二、紅重平衡
因為通常情況下插入的節點會標記為紅色,那么就有可能導致兩個紅色的節點練成父子,所以需要通過一下方法修復;
1. RR-1

如圖所示,如果插入的紅色節點和父節點一起組成了3個關鍵碼的超級節點,在 B 樹的角度上則只需要重新標記顏色,使黑色節點位於中間即可;表現在紅黑樹中就需要進行旋轉操作,如圖:
雙紅節點同邊:

- 如圖中,兩個紅色節點都是左孩子或者都是右孩子時
- 只需要旋轉其祖父節點
- 然后祖父節點和其父節點反轉顏色即可
雙紅節點異邊:

- 如圖中, 兩個紅色節點是異邊的時候
- 首先需要旋轉父節點,轉為上面同邊的情況,在旋轉其祖父節點;
- 然后祖父節點和其父節點反轉顏色即可
其實在這里如果忽略顏色,其旋轉操作就可 AVL 樹是一樣的;那么在實現的時候同樣可以使用之前講過的 3+4 重構
;
1. RR-2

如圖所示,如果紅色節點上移后,同其父節點組成的超級節點是4個關鍵碼,則發生了上溢,需要將其分裂為兩個節點;但此時表現在紅黑樹上其結構並未發生變化,所以只需要重新染色即可;
- 如圖所示,如果父親是紅色節點,同時叔父也是紅色節點,此時就構成了4個關鍵碼的超級節點
- 這個時候只需要將父親節點和叔父節點變成黑色,祖父節點變紅即可;
如圖所示:

三、黑重平衡
當刪除黑節點的時候,會使得該分支的黑高度降低,從而不滿足每個分支的黑高度相等,所以下面將刪除黑節點分成幾種情況進行修復;
1. BB-1

當刪除的節點是黑色節點,且其兄弟節點是黑色,同時有紅孩子的時候;如果轉化為 (2,4)B 樹
:

如圖所示:
- 圖中的綠色節點,表示顏色任意;
- 如果將以情況看作是 B 樹,則相當於刪除 x 節點后,使得該節點關鍵碼不足,發生下溢;於是通過旋轉父節點向其兄弟節點借一個關鍵碼;
- 對於紅黑樹則是,旋轉父節點,同時相同位置的顏色保持不變;
2. BB-2-R
如果父節點是紅色,有黑色兄弟節點,並且沒有紅色孩子:

轉化為 (2,4)B 樹
:

如圖所示,
- 此時相當於刪除黑色節點,使得該節點的關鍵碼不足,發生下溢
- 同時兄弟節點沒有紅色孩子,沒辦法借出,所以只能從父節點以一個關鍵碼合並兩個孩子節點;
- 同時父節點為紅色,借出一個關鍵碼后,其黑高度不變;
- 在紅黑樹中則為刪除的位置由父節點代替,並且兄弟姐弟節點變紅;整體結構不變;
3. BB-2-B
如果父節點是黑色,有黑色兄弟節點,並且沒有紅色孩子:

轉化為 (2,4)B 樹
:

如圖所示:
- 整體情況和
bb-2-r
一樣,但是其父節點為黑色; - 也就是在父節點借出一個節點后,父節點會繼續發生下溢;並根據情況再次判斷調整;但是下溢整體不會超過
O(logn)
次;
4. BB-3
如果父節點是黑色,有紅色兄弟節點:

轉化為 (2,4)B 樹
:

如圖所示:
- 如果有黑色父節點,且兄弟節點為紅色;
- 則相當於可以從兄弟節點借一個節點,同時結構不會改變;
- 對於紅黑樹而言,相當於旋轉父節點,同時父節點和兄弟節點變色;
## 三、實現
1. 查找
private RBTNode<T> search(RBTNode<T> x, T key) {
if (x == null) return x;
int cmp = key.compareTo(x.key);
if (cmp < 0)
return search(x.left, key);
else if (cmp > 0)
return search(x.right, key);
else
return x;
}
2. 插入
public void insert(T key) {
insert(new RBTNode<T>(key, BLACK, null, null, null));
}
private void insert(RBTNode<T> node) {
int cmp;
RBTNode<T> y = null;
RBTNode<T> x = this.root;
// 1. 將紅黑樹當作一顆二叉查找樹,將節點添加到二叉查找樹中。
while (x != null) {
y = x;
cmp = node.key.compareTo(x.key);
if (cmp < 0)
x = x.left;
else
x = x.right;
}
node.parent = y;
if (y != null) {
cmp = node.key.compareTo(y.key);
if (cmp < 0)
y.left = node;
else
y.right = node;
} else {
this.root = node;
}
// 2. 設置節點的顏色為紅色
node.color = RED;
// 3. 將它重新修正為一顆二叉查找樹
insertFixUp(node);
}
private void insertFixUp(RBTNode<T> node) {
RBTNode<T> parent, gparent;
// 若“父節點存在,並且父節點的顏色是紅色”
while (((parent = parentOf(node)) != null) && isRed(parent)) {
gparent = parentOf(parent);
//若“父節點”是“祖父節點的左孩子”
if (parent == gparent.left) {
// Case 1條件:叔叔節點是紅色
RBTNode<T> uncle = gparent.right;
if ((uncle != null) && isRed(uncle)) {
setBlack(uncle);
setBlack(parent);
setRed(gparent);
node = gparent;
continue;
}
// Case 2條件:叔叔是黑色,且當前節點是右孩子
if (parent.right == node) {
RBTNode<T> tmp;
leftRotate(parent);
tmp = parent;
parent = node;
node = tmp;
}
// Case 3條件:叔叔是黑色,且當前節點是左孩子。
setBlack(parent);
setRed(gparent);
rightRotate(gparent);
} else { //若“z的父節點”是“z的祖父節點的右孩子”
// Case 1條件:叔叔節點是紅色
RBTNode<T> uncle = gparent.left;
if ((uncle != null) && isRed(uncle)) {
setBlack(uncle);
setBlack(parent);
setRed(gparent);
node = gparent;
continue;
}
// Case 2條件:叔叔是黑色,且當前節點是左孩子
if (parent.left == node) {
RBTNode<T> tmp;
rightRotate(parent);
tmp = parent;
parent = node;
node = tmp;
}
// Case 3條件:叔叔是黑色,且當前節點是右孩子。
setBlack(parent);
setRed(gparent);
leftRotate(gparent);
}
}
}
/*
* 對紅黑樹的節點(x)進行左旋轉
*
* 左旋示意圖(對節點x進行左旋):
* px px
* / /
* x y
* / \ --(左旋)-. / \ #
* lx y x ry
* / \ / \
* ly ry lx ly
*
*
*/
private void leftRotate(RBTNode<T> x) {
// 設置x的右孩子為y
RBTNode<T> y = x.right;
// 將 “y的左孩子” 設為 “x的右孩子”;
// 如果y的左孩子非空,將 “x” 設為 “y的左孩子的父親”
x.right = y.left;
if (y.left != null)
y.left.parent = x;
// 將 “x的父親” 設為 “y的父親”
y.parent = x.parent;
if (x.parent == null) {
this.root = y; // 如果 “x的父親” 是空節點,則將y設為根節點
} else {
if (x.parent.left == x)
x.parent.left = y; // 如果 x是它父節點的左孩子,則將y設為“x的父節點的左孩子”
else
x.parent.right = y; // 如果 x是它父節點的左孩子,則將y設為“x的父節點的左孩子”
}
// 將 “x” 設為 “y的左孩子”
y.left = x;
// 將 “x的父節點” 設為 “y”
x.parent = y;
}
/*
* 對紅黑樹的節點(y)進行右旋轉
*
* 右旋示意圖(對節點y進行左旋):
* py py
* / /
* y x
* / \ --(右旋)-. / \ #
* x ry lx y
* / \ / \ #
* lx rx rx ry
*
*/
private void rightRotate(RBTNode<T> y) {
// 設置x是當前節點的左孩子。
RBTNode<T> x = y.left;
// 將 “x的右孩子” 設為 “y的左孩子”;
// 如果"x的右孩子"不為空的話,將 “y” 設為 “x的右孩子的父親”
y.left = x.right;
if (x.right != null)
x.right.parent = y;
// 將 “y的父親” 設為 “x的父親”
x.parent = y.parent;
if (y.parent == null) {
this.root = x; // 如果 “y的父親” 是空節點,則將x設為根節點
} else {
if (y == y.parent.right)
y.parent.right = x; // 如果 y是它父節點的右孩子,則將x設為“y的父節點的右孩子”
else
y.parent.left = x; // (y是它父節點的左孩子) 將x設為“x的父節點的左孩子”
}
// 將 “y” 設為 “x的右孩子”
x.right = y;
// 將 “y的父節點” 設為 “x”
y.parent = x;
}
3. 刪除
public void remove(T key) {
RBTNode<T> node;
if ((node = search(root, key)) != null)
remove(node);
}
private void remove(RBTNode<T> node) {
RBTNode<T> child, parent;
boolean color;
// 被刪除節點的"左右孩子都不為空"的情況。
if ((node.left != null) && (node.right != null)) {
// 被刪節點的后繼節點。(稱為"取代節點")
// 用它來取代"被刪節點"的位置,然后再將"被刪節點"去掉。
RBTNode<T> replace = node;
// 獲取后繼節點
replace = replace.right;
while (replace.left != null)
replace = replace.left;
// "node節點"不是根節點(只有根節點不存在父節點)
if (parentOf(node) != null) {
if (parentOf(node).left == node)
parentOf(node).left = replace;
else
parentOf(node).right = replace;
} else {
// "node節點"是根節點,更新根節點。
this.root = replace;
}
// child是"取代節點"的右孩子,也是需要"調整的節點"。
// "取代節點"肯定不存在左孩子!因為它是一個后繼節點。
child = replace.right;
parent = parentOf(replace);
// 保存"取代節點"的顏色
color = colorOf(replace);
// "被刪除節點"是"它的后繼節點的父節點"
if (parent == node) {
parent = replace;
} else {
// child不為空
if (child != null)
setParent(child, parent);
parent.left = child;
replace.right = node.right;
setParent(node.right, replace);
}
replace.parent = node.parent;
replace.color = node.color;
replace.left = node.left;
node.left.parent = replace;
if (color == BLACK)
removeFixUp(child, parent);
node = null;
return;
}
if (node.left != null) {
child = node.left;
} else {
child = node.right;
}
parent = node.parent;
// 保存"取代節點"的顏色
color = node.color;
if (child != null)
child.parent = parent;
// "node節點"不是根節點
if (parent != null) {
if (parent.left == node)
parent.left = child;
else
parent.right = child;
} else {
this.root = child;
}
if (color == BLACK)
removeFixUp(child, parent);
node = null;
}
private void removeFixUp(RBTNode<T> node, RBTNode<T> parent) {
RBTNode<T> other;
while ((node == null || isBlack(node)) && (node != this.root)) {
if (parent.left == node) {
other = parent.right;
if (isRed(other)) {
// Case 1: x的兄弟w是紅色的
setBlack(other);
setRed(parent);
leftRotate(parent);
other = parent.right;
}
if ((other.left == null || isBlack(other.left)) &&
(other.right == null || isBlack(other.right))) {
// Case 2: x的兄弟w是黑色,且w的倆個孩子也都是黑色的
setRed(other);
node = parent;
parent = parentOf(node);
} else {
if (other.right == null || isBlack(other.right)) {
// Case 3: x的兄弟w是黑色的,並且w的左孩子是紅色,右孩子為黑色。
setBlack(other.left);
setRed(other);
rightRotate(other);
other = parent.right;
}
// Case 4: x的兄弟w是黑色的;並且w的右孩子是紅色的,左孩子任意顏色。
setColor(other, colorOf(parent));
setBlack(parent);
setBlack(other.right);
leftRotate(parent);
node = this.root;
break;
}
} else {
other = parent.left;
if (isRed(other)) {
// Case 1: x的兄弟w是紅色的
setBlack(other);
setRed(parent);
rightRotate(parent);
other = parent.left;
}
if ((other.left == null || isBlack(other.left)) &&
(other.right == null || isBlack(other.right))) {
// Case 2: x的兄弟w是黑色,且w的倆個孩子也都是黑色的
setRed(other);
node = parent;
parent = parentOf(node);
} else {
if (other.left == null || isBlack(other.left)) {
// Case 3: x的兄弟w是黑色的,並且w的左孩子是紅色,右孩子為黑色。
setBlack(other.right);
setRed(other);
leftRotate(other);
other = parent.left;
}
// Case 4: x的兄弟w是黑色的;並且w的右孩子是紅色的,左孩子任意顏色。
setColor(other, colorOf(parent));
setBlack(parent);
setBlack(other.left);
rightRotate(parent);
node = this.root;
break;
}
}
}
if (node != null) setBlack(node);
}
總結
- 對於紅黑樹增加和刪除的情況特別的多,不是特別好理解,所以這一部分最好對應 B 樹,上溢和下溢的修復