先來說說紅黑樹是什麼吧
紅黑樹也是一種二叉搜索樹,它給每個節點上存儲了一個來表示節點的顏色,可以是BLACK,也可以是RED。通過對任何一個從根節點到葉子節點路徑上的顏色的約束來達到一個近似平衡的一棵樹。紅黑樹要求最長路徑不超過最短路徑的兩倍。
紅黑樹滿足以下性質:
1、每個節點的顏色不是紅色的就是黑色的。
2、根節點的顏色爲黑色的。
3、不能有連續的紅節點(有父子關係的節點的顏色不能同時爲紅色)。
4、對每條路徑而言,黑色節點的個數必須是相等的。
5、每個葉子節點的顏色是黑色的(這裏指的葉子節點爲NULL節點)。
接下來,我們來討論一下插入的時候會有哪幾種情況。
特別聲明:以下我們插入的節點的顏色默認是紅色的。
1、根節點爲NULL時,直接插入即可。
2、當父節點的顏色爲黑色時,直接插入即可。
3、當父節點的顏色爲紅色時:
當p節點爲紅色時,如果u節點存在且爲紅色,將pp節點的顏色更改爲紅色,p節點和u節點的顏色更改爲黑色。如下圖所示
當cu節點爲p的左節點,p節點爲紅色時,u的顏色爲黑色或者是不存在時,右旋,如下圖所示:
當cu節點爲p的右節點,p節點爲紅色時,u的顏色爲黑色或者是不存在時,先左旋再右旋,如下圖所示:
接下來,我們舉個實例來看看到底如何來實現的
將a[] = { 5,3,4,1,7,8,2,6,0,9 };依次插入。
插入5:
插入3:
插入4(先左旋在右旋):
插入1:
插入7:
插入8(左旋):
插入2(先左旋再右旋):
插入6(p節點和u節點同時爲紅色)
插入0(p節點和u節點同時爲紅色):
插入9:
代碼實現:
#pragma once
enum Color
{
BLACK,
RED
};
template<class V>
struct RBTreeNode
{
RBTreeNode<V>* _left;
RBTreeNode<V>* _right;
RBTreeNode<V>* _parent;
// K _key;
V _v;
Color _col;
RBTreeNode(const V& v, Color col = RED)
:_left(NULL)
,_right(NULL)
,_parent(NULL)
,_v(v)
,_col(col)
{}
};
template<class V, class Ref, class Ptr>
struct RBTreeIterator
{
typedef RBTreeNode<V> Node;
typedef RBTreeIterator<V, Ref, Ptr> Iterator;
typedef RBTreeIterator<V, const Ref, const Ptr> ConstIterator;
typedef RBTreeIterator<V, Ref, Ptr> Self;
Node* _node;
RBTreeIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_v;
}
Ptr operator->()
{
return &(operaotr*());
}
Self& operator++()
{
if (_node->_right)
{
Node* subright = _node->_right;
while (subright->_left)
{
subright = subright->_left;
}
_node = subright;
}
else
{
}
}
};
template<class V>
class RBTree
{
public:
typedef RBTreeNode<V> Node;
RBTree()
:_root(NULL)
{}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
subR->_left = parent;
Node* ppNode = parent->_parent;
parent->_parent = subR;
if (_root == parent)
{
_root = subR;
subR->_parent = NULL;
}
else
{
if (ppNode->_right == parent)
ppNode->_right = subR;
else
ppNode->_left = subR;
subR->_parent = ppNode;
}
// _root->_parent = NULL;
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
Node* ppNode = parent->_parent;
parent->_parent = subL;
if (_root == parent)
{
_root = subL;
subL->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
ppNode->_left = subL;
else
ppNode->_right = subL;
subL->_parent = ppNode;
}
_root->_parent = NULL;
}
bool Insert(const V& v)
{
if (_root == NULL)
{
_root = new Node(v);
_root->_col = BLACK;
return true;
}
Node* cur = _root;
Node* parent = NULL;
while (cur)
{
if (cur->_v > v)
{
parent = cur;
cur = cur->_left;
}
else
{
parent = cur;
cur = cur->_right;
}
}
cur = new Node(v);
if (parent->_v > v)
{
parent->_left = cur;
cur->_parent = parent;
}
else
{
parent->_right = cur;
cur->_parent = parent;
}
//判斷顏色
while (parent && parent->_col == RED)
{
Node* grandparent = parent->_parent;
if (grandparent && grandparent->_col == BLACK)
{
if (parent == grandparent->_left)
{
Node* uncle = grandparent->_right;
if (uncle && uncle->_col == RED)
{
uncle->_col = BLACK;
parent->_col = BLACK;
if(grandparent->_parent)
grandparent->_col = RED;
cur = grandparent;
parent = cur->_parent;
}
else
{
if (cur == parent->_right)
{
RotateL(parent);
swap(cur, parent);
}
RotateR(grandparent);
grandparent->_col = RED;
parent->_col = BLACK;
}
}
else
{
Node* uncle = grandparent->_left;
if (uncle && uncle->_col == RED)
{
parent->_col == uncle->_col == RED;
if(grandparent->_parent)
grandparent->_col = BLACK;
cur = grandparent;
parent = cur->_parent;
}
else
{
if (cur == parent->_left)
{
RotateR(parent);
swap(cur, parent);
}
RotateL(grandparent);
parent->_col = BLACK;
grandparent->_col = RED;
}
}
}
else
{
grandparent->_col == BLACK;
break;
}
}
}
bool IsBalance()
{
if (_root && _root->_col == RED)
return false;
int k = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLACK)
++k;
cur = cur->_left;
}
int blacknum = 0;
return _IsBalance(_root, k, blacknum);
}
bool _IsBalance(Node* root, int k, int blacknum)
{
if (root == NULL)
{
if (k != blacknum)
{
cout << "黑色節點的數量不相同" << " " << endl;
return false;
}
return true;
}
if (root->_col == RED && root->_parent->_col == RED)
{
cout << "存在連續的紅節點" << root->_v << " " << endl;
return false;
}
if (root->_col != RED && root->_col != BLACK)
{
cout << "節點的顏色既不是黑色的也不是紅色的" << root->_v << " " << endl;
return false;
}
if (root->_col == BLACK)
{
++blacknum;
}
return _IsBalance(root->_left, k, blacknum)
&& _IsBalance(root->_right, k, blacknum);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
void _InOrder(Node* root)
{
if (root == NULL)
return;
_InOrder(root->_left);
cout << root->_v << " ";
_InOrder(root->_right);
}
protected:
Node* _root;
};
void TestRBTree()
{
int a[] = { 5,3,4,1,7,8,2,6,0,9 };
RBTree<int> t;
for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
t.Insert(a[i]);
cout << a[i] << t.IsBalance() << " ";
}
cout << endl;
t.InOrder();
//t.IsBalance();
}