紅黑樹本質上是一棵二叉搜索樹,差別在於每個節點多了一個儲存位來記錄顏色,取值為 Red 或 Black。它透過約束「從根到葉的簡單路徑上的顏色分佈」來控制樹高,保證最長路徑不超過最短路徑的兩倍,因而近似平衡。
最短路徑上的節點全為黑色;最長路徑則是黑紅交錯,其中黑節點數目與最短路徑相同,再加上數量相當的紅節點。因此最長路徑的長度自然不會超過最短路徑的兩倍。
插入的新節點預設為紅色。原因是:若插入黑節點,必然影響「所有路徑含相同數目黑節點」這條原則,維護成本高得多。
enum Color { RED, BLACK };
template<class K, class V>
struct RBTreeNode
{
K _key;
V _value;
Color _color; // 顏色
RBTreeNode<K, V>* _left; // 左孩子
RBTreeNode<K, V>* _right; // 右孩子
RBTreeNode<K, V>* _parent; // 父節點
RBTreeNode(const K& key = K(), const V& value = V())
: _key(key)
, _value(value)
, _color(RED)
, _left(NULL)
, _right(NULL)
, _parent(NULL)
{}
};
以下 g 代表祖父節點、p 代表父節點、u 代表叔叔節點、cur 代表當前節點。
一、父節點是祖父的左孩子
二、父節點是祖父的右孩子
template<class K, class V>
bool RBTree<K, V>::Insert(const K& key, const V& value)
{
if (_root == NULL)
{
_root = new RBTreeNode<K, V>(key, value);
_root->_color = BLACK;
return true;
}
// 找位置
RBTreeNode<K, V>* cur = _root;
RBTreeNode<K, V>* parent = NULL;
while (cur)
{
if (cur->_key > key) { parent = cur; cur = cur->_left; }
else if (cur->_key < key) { parent = cur; cur = cur->_right; }
else return false;
}
// 插入(新節點預設紅色)
cur = new RBTreeNode<K, V>(key, value);
cur->_parent = parent;
if (parent->_key > key) parent->_left = cur;
else parent->_right = cur;
// 檢查顏色是否合法
while (parent && parent->_color == RED)
{
RBTreeNode<K, V>* grandfather = parent->_parent;
if (parent == grandfather->_left)
{
RBTreeNode<K, V>* uncle = grandfather->_right;
if (uncle && uncle->_color == RED)
{
// 情況一:變色
grandfather->_color = RED;
parent->_color = BLACK;
uncle->_color = BLACK;
cur = grandfather;
parent = grandfather->_parent;
}
else
{
if (cur == parent->_left)
{
// 情況二:右單旋(cur 必有黑色孩子)
parent->_color = BLACK;
grandfather->_color = RED;
RotateR(grandfather);
}
else
{
// 情況三:左右雙旋
RotateL(parent);
parent->_color = BLACK;
grandfather->_color = RED;
RotateR(grandfather);
}
break;
}
}
else if (parent == grandfather->_right)
{
RBTreeNode<K, V>* uncle = grandfather->_left;
if (uncle && uncle->_color == RED)
{
// 情況一:變色
grandfather->_color = RED;
parent->_color = BLACK;
uncle->_color = BLACK;
cur = grandfather;
parent = cur->_parent;
}
else
{
if (cur == parent->_right)
{
// 情況二:左單旋
parent->_color = BLACK;
grandfather->_color = RED;
RotateL(grandfather);
}
else
{
// 情況三:右左雙旋
RotateR(parent);
parent->_color = BLACK;
grandfather->_color = RED;
RotateL(grandfather);
}
break;
}
}
}
_root->_color = BLACK; // 根節點永遠為黑
return true;
}
插入完成後,很難用肉眼判斷顏色是否合法、各條路徑的黑節點數是否一致。因此實作兩個檢驗函式:
// 檢驗紅黑樹的合法性
template<class K, class V>
bool RBTree<K, V>::Check()
{
// 以最左路徑的黑節點數作為標竿
int blackNum = 0;
RBTreeNode<K, V>* cur = _root;
while (cur)
{
if (cur->_color == BLACK)
blackNum++;
cur = cur->_left;
}
int CBNum = 0;
return _Check(_root, blackNum, CBNum);
}
// 遞歸輔助
template<class K, class V>
bool RBTree<K, V>::_Check(RBTreeNode<K, V>* root, int blackNum, int CBNum)
{
if (root == NULL)
return true;
if (root->_color == BLACK)
{
CBNum++;
if (root->_left == NULL && root->_right == NULL)
{
// 走到葉節點,與標竿比較
if (blackNum == CBNum)
return true;
else
{
cout << "葉子節點為" << root->_key
<< "路徑的黑色節點數目與最左側支路的黑色節點數目不相等!" << endl;
return false;
}
}
}
else if (root->_parent && root->_parent->_color == RED)
{
// 存在連續的兩個紅色節點
cout << root->_parent->_key << " 和 " << root->_key
<< " 為兩個連續的紅色節點" << endl;
return false;
}
return _Check(root->_left, blackNum, CBNum)
&& _Check(root->_right, blackNum, CBNum);
}
兩者都是高效的平衡二叉樹,增刪查改的時間複雜度同為 O(lg N)。差別在於 AVL 追求嚴格平衡,紅黑樹只要求最長路徑不超過最短路徑的兩倍,因此旋轉與調整的頻率更低。整體而言紅黑樹的綜合效能略勝一籌,這也是 STL 的 map、set 以及許多語言標準庫選擇它的原因。