1 红黑树的性质
2 红黑树的旋转
红黑树的旋转与AVL树的旋转相同。
3 红黑树的插入
4 红黑树的删除
4.1 红黑树删除的一个例子
5 红黑树的实现
5.1 RBTree.hpp
红黑树的声明和定义均在这个头文件中。
//file RBTree.h
#ifndef _RB_TREE_H_
#define _RB_TREE_H_
#include<iostream>
#include<string>
#include<sstream>
#include<fstream>
using namespace std;
//原来的july的C++代码里面有一些问题,已经进行了修改,建议参考
//C语言版本: https://blog.csdn.net/v_JULY_v/article/details/6114226
template<class KEY, class U>
class RB_Tree
{
private:
RB_Tree(const RB_Tree& input) {}
const RB_Tree& operator=(const RB_Tree& input) {}
private:
enum COLOR { RED, BLACK };
class RB_Node
{
public:
RB_Node()
{
//RB_COLOR = BLACK;
right = NULL;
left = NULL;
parent = NULL;
}
COLOR RB_COLOR;
RB_Node* right;
RB_Node* left;
RB_Node* parent;
KEY key;
U data;
};
public:
RB_Tree()
{
this->m_nullNode = new RB_Node();
this->m_root = m_nullNode;
this->m_nullNode->right = NULL;
this->m_nullNode->left = NULL;
this->m_nullNode->parent = NULL;
this->m_nullNode->RB_COLOR = BLACK;
}
bool Empty()
{
if (this->m_root == this->m_nullNode)
{
return true;
}
else
{
return false;
}
}
//查找key
RB_Node* find(KEY key)
{
RB_Node* index = m_root;
while (index != m_nullNode)
{
if (key < index->key)
{
index = index->left; //比当前的小,往左
}
else if (key > index->key)
{
index = index->right; //比当前的大,往右
}
else
{
break;
}
}
return index;
}
//--------------------------插入结点总操作----------------------------------
//全部的工作,都在下述伪代码中:
/*RB-INSERT(T, z)
1 y ← nil[T] // y 始终指向 x 的父结点。
2 x ← root[T] // x 指向当前树的根结点,
3 while x ≠ nil[T]
4 do y ← x
5 if key[z] < key[x] //向左,向右..
6 then x ← left[x]
7 else x ← right[x] //为了找到合适的插入点,x探路跟踪路径,直到x成为NIL 为止。
8 p[z] ← y //y置为 插入结点z 的父结点。
9 if y = nil[T]
10 then root[T] ← z
11 else if key[z] < key[y]
12 then left[y] ← z
13 else right[y] ← z //此 8-13行,置z 相关的指针。
14 left[z] ← nil[T]
15 right[z] ← nil[T] //设为空,
16 color[z] ← RED //将新插入的结点z作为红色
17 RB-INSERT-FIXUP(T, z)
*/
//因为将z着为红色,可能会违反某一红黑性质,
//所以需要调用下面的RB-INSERT-FIXUP(T, z)来保持红黑性质。
bool Insert(KEY key, U data)
{
RB_Node* insert_point = m_nullNode;
RB_Node* index = m_root;
while (index != m_nullNode)
{
insert_point = index;
if (key < index->key)
{
index = index->left;
}
else if (key > index->key)
{
index = index->right;
}
else
{
return false;
}
}
RB_Node* insert_node = new RB_Node();
insert_node->key = key;
insert_node->data = data;
insert_node->RB_COLOR = RED;
insert_node->right = m_nullNode;
insert_node->left = m_nullNode;
insert_node->parent = insert_point;
if (insert_point == m_nullNode) //如果插入的是一颗空树
{
m_root = insert_node;
}
else
{
if (key < insert_point->key)
{
insert_point->left = insert_node;
}
else
{
insert_point->right = insert_node;
}
}
InsertFixUp(insert_node); //调用InsertFixUp修复红黑树性质。
}
//---------------------插入结点性质修复--------------------------------
//3种插入情况,都在下面的伪代码中(未涉及到所有全部的插入情况)。
/*
RB-INSERT-FIXUP(T, z)
1 while color[p[z]] = RED
2 if p[z] = left[p[p[z]]]
3 then y ← right[p[p[z]]]
4 if color[y] = RED
5 then color[p[z]] ← BLACK Case 1
6 color[y] ← BLACK Case 1
7 color[p[p[z]]] ← RED Case 1
8 z ← p[p[z]] Case 1
9 else
10 if z = right[p[z]]
11 then z ← p[z] Case 2
12 LEFT-ROTATE(T, z) Case 2
13 color[p[z]] ← BLACK Case 3
14 color[p[p[z]]] ← RED Case 3
15 RIGHT-ROTATE(T, p[p[z]]) Case 3
16 else (same as then clause with "right" and "left" exchanged)
17 color[root[T]] ← BLACK
*/
//然后的工作,就非常简单了,即把上述伪代码改写为下述的c++代码:
void InsertFixUp(RB_Node* node)
{
while (node->parent->RB_COLOR == RED)
{
if (node->parent == node->parent->parent->left) //
{
RB_Node* uncle = node->parent->parent->right;
if (uncle->RB_COLOR == RED) //插入情况1,z的叔叔y是红色的。
{
node->parent->RB_COLOR = BLACK;
uncle->RB_COLOR = BLACK;
node->parent->parent->RB_COLOR = RED;
node = node->parent->parent;
}
else //if (uncle->RB_COLOR == BLACK) //此时叔叔肯定是黑色,不用再判断。插入情况2:z的叔叔y是黑色的,。
{
if (node == node->parent->right) //且z是右孩子
{
node = node->parent;
RotateLeft(node);
}
//插入情况3:z的叔叔y是黑色的,但z是左孩子。
node->parent->RB_COLOR = BLACK;
node->parent->parent->RB_COLOR = RED;
RotateRight(node->parent->parent);
}
}
else //这部分是针对为插入情况1中,z的父亲现在作为祖父的右孩子了的情况,而写的。
//15 else (same as then clause with "right" and "left" exchanged)
{
RB_Node* uncle = node->parent->parent->left;
if (uncle->RB_COLOR == RED)
{
node->parent->RB_COLOR = BLACK;
uncle->RB_COLOR = BLACK;
uncle->parent->RB_COLOR = RED;
node = node->parent->parent;
}
else //if (uncle->RB_COLOR == BLACK)//此时叔叔肯定是黑色,不用再判断。
{
if (node == node->parent->left)
{
node = node->parent;
RotateRight(node); //与上述代码相比,左旋改为右旋
}
node->parent->RB_COLOR = BLACK;
node->parent->parent->RB_COLOR = RED;
RotateLeft(node->parent->parent); //右旋改为左旋,即可。
}
}
}
m_root->RB_COLOR = BLACK;
}
//左旋代码实现
bool RotateLeft(RB_Node* node)
{
if (node == m_nullNode || node->right == m_nullNode)
{
return false;//can't rotate
}
RB_Node* lower_right = node->right;
lower_right->parent = node->parent;
node->right = lower_right->left;
if (lower_right->left != m_nullNode)
{
lower_right->left->parent = node;
}
if (node->parent == m_nullNode) //rotate node is root
{
m_root = lower_right;
}
else
{
if (node == node->parent->left)
{
node->parent->left = lower_right;
}
else
{
node->parent->right = lower_right;
}
}
node->parent = lower_right;
lower_right->left = node;
}
//右旋代码实现
bool RotateRight(RB_Node* node)
{
if (node == m_nullNode || node->left == m_nullNode)
{
return false;//can't rotate
}
RB_Node* lower_left = node->left;
node->left = lower_left->right;
lower_left->parent = node->parent;
if (lower_left->right != m_nullNode)
{
lower_left->right->parent = node;
}
if (node->parent == m_nullNode) //node is root
{
m_root = lower_left;
}
else
{
if (node == node->parent->right)
{
node->parent->right = lower_left;
}
else
{
node->parent->left = lower_left;
}
}
node->parent = lower_left;
lower_left->right = node;
}
//--------------------------删除结点总操作----------------------------------
//全部的工作,都在下述伪代码中:
/*RB-DELETE(T, z)
1 if left[z] = nil[T] or right[z] = nil[T]
2 then y ← z
3 else y ← TREE-SUCCESSOR(z)
4 if left[y] ≠ nil[T]
5 then x ← left[y]
6 else x ← right[y]
7 p[x] ← p[y]
8 if p[y] = nil[T]
9 then root[T] ← x
10 else if y = left[p[y]]
11 then left[p[y]] ← x
12 else right[p[y]] ← x
13 if y ≠ z
14 then key[z] ← key[y]
15 copy y's satellite data into z
16 if color[y] = BLACK
17 then RB-DELETE-FIXUP(T, x)
18 return y
*/
bool Delete(KEY key)
{
RB_Node* delete_point = find(key);
if (delete_point == m_nullNode)
{
return false;
}
if (delete_point->left != m_nullNode && delete_point->right != m_nullNode)
{
RB_Node* successor = InOrderSuccessor(delete_point);
delete_point->data = successor->data;
delete_point->key = successor->key;
delete_point = successor;
}
RB_Node* delete_point_child;
if (delete_point->right != m_nullNode)
{
delete_point_child = delete_point->right;
}
else if (delete_point->left != m_nullNode)
{
delete_point_child = delete_point->left;
}
else
{
delete_point_child = m_nullNode;
}
delete_point_child->parent = delete_point->parent;
if (delete_point->parent == m_nullNode)//delete root node
{
m_root = delete_point_child;
}
else if (delete_point == delete_point->parent->right)
{
delete_point->parent->right = delete_point_child;
}
else
{
delete_point->parent->left = delete_point_child;
}
//if (delete_point->RB_COLOR == BLACK && !(delete_point_child == m_nullNode && delete_point_child->parent == m_nullNode))
if (delete_point->RB_COLOR == BLACK)
{
DeleteFixUp(delete_point_child);
}
delete delete_point;
return true;
}
//---------------------删除结点性质修复-----------------------------------
//所有的工作,都在下述23行伪代码中:
/*
RB-DELETE-FIXUP(T, x)
1 while x ≠ root[T] and color[x] = BLACK
2 if x = left[p[x]]
3 then w ← right[p[x]]
4 if color[w] = RED
5 then color[w] ← BLACK Case 1
6 color[p[x]] ← RED Case 1
7 LEFT-ROTATE(T, p[x]) Case 1
8 w ← right[p[x]] Case 1
9 if color[left[w]] = BLACK and color[right[w]] = BLACK
10 then color[w] ← RED Case 2
11 x ← p[x] Case 2
12 else
13 if color[right[w]] = BLACK
14 then color[left[w]] ← BLACK Case 3
15 color[w] ← RED Case 3
16 RIGHT-ROTATE(T, w) Case 3
17 w ← right[p[x]] Case 3
18 color[w] ← color[p[x]] Case 4
19 color[p[x]] ← BLACK Case 4
20 color[right[w]] ← BLACK Case 4
21 LEFT-ROTATE(T, p[x]) Case 4
22 x ← root[T] Case 4
23 else (same as then clause with "right" and "left" exchanged)
24 color[x] ← BLACK
*/
//接下来的工作,很简单,即把上述伪代码改写成c++代码即可。
void DeleteFixUp(RB_Node* node)
{
while (node != m_root && node->RB_COLOR == BLACK)
{
if (node == node->parent->left)
{
RB_Node* brother = node->parent->right;
if (brother->RB_COLOR == RED) //情况1:x的兄弟w是红色的。
{
brother->RB_COLOR = BLACK;
node->parent->RB_COLOR = RED;
RotateLeft(node->parent);
/************************************************************************/
/* 原来的代码中少了下面这一行,虽然上面的伪代码中有这一行
* 不要认为在上面执行了 RB_Node* brother = node->parent->right;
* 这里就不需要再次赋值了,因为经过了RotateLeft(node->parent)这就会导致node->parent->right
* 进行了修改,所以这里要再次赋值
*/
/************************************************************************/
brother = node->parent->right;
}
if (brother->left->RB_COLOR == BLACK && brother->right->RB_COLOR == BLACK)//情况2:x的兄弟w是黑色的,且w的俩个孩子都是黑色的。
{
brother->RB_COLOR = RED;
node = node->parent;
}
else
{
if (brother->right->RB_COLOR == BLACK)//情况3:x的兄弟w是黑色的,w的左孩子是红色且w的右孩子是黑色
{
brother->RB_COLOR = RED;
brother->left->RB_COLOR = BLACK;
RotateRight(brother);
/************************************************************************/
/* 原来的代码中少了下面这一行,虽然上面的伪代码中有这一行
* 不要认为在上面执行了 RB_Node* brother = node->parent->right;
* 这里就不需要再次赋值了,因为经过了RotateRight(brother)这就会导致node->parent->right
* 进行了修改,所以这里要再次赋值
*/
/************************************************************************/
brother = node->parent->right;
}
//情况4:x的兄弟w是黑色的,且w的右孩子是红色的(不关心w的左孩子的颜色)
brother->RB_COLOR = node->parent->RB_COLOR;
node->parent->RB_COLOR = BLACK;
brother->right->RB_COLOR = BLACK;
RotateLeft(node->parent);
node = m_root;
}
}
else //下述情况针对上面的情况1中,node作为右孩子而阐述的。
//22 else (same as then clause with "right" and "left" exchanged)
//同样,原理一致,只是遇到左旋改为右旋,遇到右旋改为左旋,即可。其它代码不变。
{
RB_Node* brother = node->parent->left;
if (brother->RB_COLOR == RED)
{
brother->RB_COLOR = BLACK;
node->parent->RB_COLOR = RED;
RotateRight(node->parent);
/************************************************************************/
/* 原来的代码中少了下面这一行,虽然上面的伪代码中有这一行
* 不要认为在上面执行了 RB_Node* brother = node->parent->left;
* 这里就不需要再次赋值了,因为经过了RotateRight(node->parent)这就会导致node->parent->left
* 进行了修改,所以这里要再次赋值
*/
/************************************************************************/
brother = node->parent->left;
}
if (brother->left->RB_COLOR == BLACK && brother->right->RB_COLOR == BLACK)
{
brother->RB_COLOR = RED;
node = node->parent;
}
else
{
if (brother->left->RB_COLOR == BLACK)
{
brother->RB_COLOR = RED;
brother->right->RB_COLOR = BLACK;
RotateLeft(brother);
/************************************************************************/
/* 原来的代码中少了下面这一行,虽然上面的伪代码中有这一行
* 不要认为在上面执行了 RB_Node* brother = node->parent->left;
* 这里就不需要再次赋值了,因为经过了RotateLeft(brother)这就会导致node->parent->left
* 进行了修改,所以这里要再次赋值
*/
/************************************************************************/
brother = node->parent->left;
}
brother->RB_COLOR = node->parent->RB_COLOR;
node->parent->RB_COLOR = BLACK;
brother->left->RB_COLOR = BLACK;
RotateRight(node->parent);
node = m_root;
}
}
}
node->RB_COLOR = BLACK; //并改为黑色。
}
//
inline RB_Node* InOrderPredecessor(RB_Node* node)
{
if (node == m_nullNode) //null node has no predecessor
{
return m_nullNode;
}
RB_Node* result = node->left; //get node's left child
while (result != m_nullNode) //try to find node's left subtree's right most node
{
if (result->right != m_nullNode)
{
result = result->right;
}
else
{
break;
}
} //after while loop result==null or result's right child is null
if (result == m_nullNode)
{
RB_Node* index = node->parent;
result = node;
while (index != m_nullNode && result == index->left)
{
result = index;
index = index->parent;
}
result = index; // first right parent or null
}
return result;
}
//
inline RB_Node* InOrderSuccessor(RB_Node* node)
{
if (node == m_nullNode) //null node has no successor
{
return m_nullNode;
}
RB_Node* result = node->right; //get node's right node
while (result != m_nullNode) //try to find node's right subtree's left most node
{
if (result->left != m_nullNode)
{
result = result->left;
}
else
{
break;
}
} //after while loop result==null or result's left child is null
if (result == m_nullNode)
{
RB_Node* index = node->parent;
result = node;
while (index != m_nullNode && result == index->right)
{
result = index;
index = index->parent;
}
result = index; //first parent's left or null
}
return result;
}
~RB_Tree()
{
clear(m_root);
delete m_nullNode;
}
void showRBTree()
{
RB_Node *now = m_root;
showRBTree(now, 0);
}
private:
// utility function for destructor to destruct object;
void clear(RB_Node* node)
{
if (node == m_nullNode)
{
return;
}
else
{
clear(node->left);
clear(node->right);
delete node;
}
}
void showRBTree(RB_Node* now, int d, std::string flag = "")
{
if (now == m_nullNode)
return;
for (int i = 0; i < d; i++)
cout << " ";
cout << now->key << "(";
cout << (flag.empty() ? "root" : flag) << ": ";
cout << (now->RB_COLOR == BLACK ? "BLACK" : "RED") << ")";
cout << endl;
showRBTree(now->left, d + 1, "left");
showRBTree(now->right, d + 1, "right");
}
private:
RB_Node *m_nullNode;
RB_Node *m_root;
};
#endif /*_RB_TREE_H_*/
5.2 测试
//file test.cpp
#include<iostream>
#include<algorithm>
#include<iterator>
#include<vector>
#include<sstream>
#include"RBTree.hpp"
using namespace std;
int main()
{
RB_Tree<int, int> tree;
vector<int> v;
int i;
for (i = 0; i < 5; ++i)
{
v.push_back(i);
}
random_shuffle(v.begin(), v.end());
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
stringstream sstr;
cout << "========================== test insert ==========================" << endl;
for (i = 0; i < v.size(); ++i)
{
tree.Insert(v[i], i);
cout << "insert:" << v[i] << endl; //添加结点
tree.showRBTree();
cout << "====================================================" << endl;
}
//tree.showRBTree();
cout << "========================== test delete ==========================" << endl;
for (i = 0; i < v.size(); ++i)
{
cout << "Delete:" << v[i] << endl;
tree.Delete(v[i]); //删除结点
tree.showRBTree();
cout << "====================================================" << endl;
}
cout << endl;
return 0;
}