转载自:http://www.cnblogs.com/skywang12345/p/3624291.html
红黑树(Red-Black Tree,简称R-B Tree),它一种特殊的二叉查找树。
红黑树是特殊的二叉查找树,意味着它满足二叉查找树的特征:任意一个节点所包含的键值,大于等于左孩子的键值,小于等于右孩子的键值。
除了具备该特性之外,红黑树还包括许多额外的信息。
红黑树的每个节点上都有存储位表示节点的颜色,颜色是红(Red)或黑(Black)。
红黑树的特性:
(1) 每个节点或者是黑色,或者是红色。
(2) 根节点是黑色。
(3) 每个叶子节点是黑色。 [注意:这里叶子节点,是指为空的叶子节点!]
(4) 如果一个节点是红色的,则它的子节点必须是黑色的。
(5) 从一个节点到该节点的子孙节点的所有路径上包含相同数目的黑节点。
代码如下:
rbtree.h
#ifndef RBTREE_H
#define RBTREE_H
#include <iostream>
using namespace std;
enum RBTColor {RED, BLACK}; // test enum: RED = 0, BLACK = 1
template <class T>
class RBTree
{
private:
struct RBTNode
{
// 相比于AVLTree, 结点没有height值,但多加了parent指针
RBTColor color;
T key;
RBTNode* left;
RBTNode* right;
RBTNode* parent;
RBTNode(T value, RBTColor c, RBTNode* p, RBTNode* l, RBTNode* r):
key(value), color(c), parent(p), left(l), right(r) {}
RBTNode (T value):
key(value), color(BLACK), parent(nullptr), left(nullptr), right(nullptr) {}
};
RBTNode* mRoot;
public:
RBTree();
~RBTree();
void preOrder() const;
void inOrder() const;
void postOrder() const;
RBTNode* search(T key) const;
RBTNode* iterativeSearch(T key) const;
T* minimum() const;
T* maximum() const;
// 找结点x的后继结点,即查找红黑树中数据大于该结点的“最小结点”
RBTNode* successor(RBTNode* x) const;
// 找结点x的前驱结点,即.............小于.......“最大结点”
RBTNode* predecessor(RBTNode* x) const;
void insert(T key);
void remove(T key);
void destroy();
void print();
private:
void preOrder(RBTNode* tree) const;
void inOrder(RBTNode* tree) const;
void postOrder(RBTNode* tree) const;
RBTNode* search(RBTNode* x, T key) const;
RBTNode* iterativeSearch(RBTNode* x, T key) const;
RBTNode* minimum(RBTNode* tree) const;
RBTNode* maximum(RBTNode* tree) const;
void leftRotate(RBTNode* &root, RBTNode* x);
void rightRotate(RBTNode* &root, RBTNode* y);
void insert(RBTNode* &root, RBTNode* node);
void insertFixUp(RBTNode* &root, RBTNode* node);
void remove(RBTNode* &root, RBTNode* node);
void removeFixUp(RBTNode* &root, RBTNode* node, RBTNode* parent);
void destroy(RBTNode* &tree);
void print(RBTNode* tree, T key, int direction) const;
};
#endif // RBTREE_H
rbtree.cpp
#include "rbtree.h"
#include <iomanip>
#define rb_parent(r) ((r)->parent)
#define rb_color(r) ((r)->color)
#define rb_is_red(r) ((r)->color == RED)
#define rb_is_black(r) ((r)->color == BLACK)
#define rb_set_red(r) do {(r)->color = RED;} while(0) //C语言中define的赋值格式
#define rb_set_black(r) do {(r)->color = BLACK;} while(0)
#define rb_set_parent(r, p) do {(r)->parent = (p);} while(0)
#define rb_set_color(r, c) do {(r)->color = (c);} while(0)
template <class T>
RBTree<T>::RBTree()
{
mRoot = nullptr;
}
template <class T>
RBTree<T>::~RBTree()
{
destroy();
}
template <class T>
void RBTree<T>::preOrder(RBTNode* tree) const
{
if (nullptr != tree)
{
cout << tree->key << " ";
preOrder(tree->left);
preOrder(tree->right);
}
}
template <class T>
void RBTree<T>::preOrder() const
{
preOrder(mRoot);
}
template <class T>
void RBTree<T>::inOrder(RBTNode* tree) const
{
if (nullptr != tree)
{
inOrder(tree->left);
cout << tree->key << " ";
inOrder(tree->right);
}
}
template <class T>
void RBTree<T>::inOrder() const
{
inOrder(mRoot);
}
template <class T>
void RBTree<T>::postOrder(RBTNode* tree) const
{
if (nullptr != tree)
{
postOrder(tree->left);
postOrder(tree->right);
cout << tree->key << " ";
}
}
template <class T>
void RBTree<T>::postOrder() const
{
postOrder(mRoot);
}
template <class T>
typename RBTree<T>::RBTNode* RBTree<T>::search(RBTNode* x, T key) const
{
if (nullptr == x || x->key == key)
{
return x;
}
if (x->key < key)
{
return search(x->left, key);
}
else // x->key > key
{
return search(x->right, key);
}
}
template <class T>
typename RBTree<T>::RBTNode* RBTree<T>::search(T key) const
{
return search(mRoot);
}
template <class T>
typename RBTree<T>::RBTNode* RBTree<T>::iterativeSearch(RBTNode* x, T key) const
{
while (nullptr != x && x->key != key)
{
if (key < x->key)
{
x = x->left;
}
else // key > x->key
{
x = x->right;
}
}
return x;
}
template <class T>
typename RBTree<T>::RBTNode* RBTree<T>::iterativeSearch(T key) const
{
return iterativeSearch(mRoot);
}
template <class T>
typename RBTree<T>::RBTNode* RBTree<T>::minimum(RBTNode* tree) const
{
if (nullptr == tree)
{
return nullptr;
}
while (nullptr != tree->left)
{
tree = tree->left;
}
return tree;
}
template <class T>
T* RBTree<T>::minimum() const
{
RBTNode* p = minimum(mRoot);
if (nullptr == p)
{
return nullptr;
}
else
{
return &p->key;
}
}
template <class T>
typename RBTree<T>::RBTNode* RBTree<T>::maximum(RBTNode* tree) const
{
if (nullptr == tree)
{
return nullptr;
}
while (nullptr != tree->right)
{
tree = tree->right;
}
return tree;
}
template <class T>
T* RBTree<T>::maximum() const
{
RBTNode* p = maximum(mRoot);
if (nullptr == p)
{
return nullptr;
}
else
{
return &p->key;
}
}
//找结点(x)的后继结点。即,查找"红黑树中数据值大于该结点"的"最小结点"
template <class T>
typename RBTree<T>::RBTNode* RBTree<T>::successor(RBTNode* x) const
{
// 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"
if (nullptr != x->right)
{
return minimum(x->right);
}
// 如果x没有右孩子。则x有以下两种可能:
// (01) x是"一个左孩子",则"x的后继结点"为 "它的父结点"
// (02) x是"一个右孩子",则查找"x的最低的父结点,并且该结点在父节点的左分支中",
// 找到的这个"最低的父结点"就是"x的后继结点"
RBTNode* y = x->parent;
while (nullptr != y && y->right == x)
{
x = y;
y = y->parent;
}
return y;
}
// 找结点(x)的前驱结点。即,查找"红黑树中数据值小于该结点"的"最大结点"
template <class T>
typename RBTree<T>::RBTNode* RBTree<T>::predecessor(RBTNode* x) const
{
// 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"
if (nullptr != x->left)
{
return maximum(x->left);
}
// 如果x没有左孩子。则x有以下两种可能:
// (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"
// (02) x是"一个左孩子",则查找"x的最低的父结点,并且该结点在父结点的",
// 找到的这个"最低的父结点"就是"x的前驱结点"
RBTNode* y = x->parent;
while (nullptr != y && y->left == x)
{
x = y;
y = y->parent;
}
return y;
}
/*
* 对红黑树的节点(x)进行左旋转
*
* 左旋示意图(对节点x进行左旋):
* px px
* / /
* x y
* / \ --(左旋)--> / \ #
* lx y x ry
* / \ / \
* ly ry lx ly
*
*
*/
template <class T>
void RBTree<T>::leftRotate(RBTNode* &root, RBTNode* x)
{
// 设置x的右孩子为y
RBTNode* y = x->right;
// 将 “y的左孩子” 设为 “x的右孩子”
// 如果y的左孩子非空,将 “x” 设为 “y的左孩子的父亲”
x->right = y->left;
if (nullptr != y->left)
{
y->left->parent = x;
}
// 将 “y的父亲” 设置为 “x的父亲”
y->parent = x->parent;
if (nullptr == x->parent)
{
root = y; // 如果“x的父亲”是空节点,则将y设为根节点
}
else
{
if (x->parent->left == x) // 如果 x是它父节点的左孩子,则将y设为“x的父节点的左孩子”
{
x->parent->left = y;
}
else // x->parent->right == x
{
x->parent->right = y;
}
}
// 将 “x” 设为 “y的左孩子”
y->left = x;
x->parent = y;
}
/*
* 对红黑树的节点(y)进行右旋转
*
* 右旋示意图(对节点y进行右旋):
* py py
* / /
* y x
* / \ --(右旋)--> / \ #
* x ry lx y
* / \ / \ #
* lx rx rx ry
*
*/
template <class T>
void RBTree<T>::rightRotate(RBTNode* &root, RBTNode* y)
{
// 设置x是当前结点的左孩子
RBTNode* x = y->left;
// 将“x的右孩子”设为“y的左孩子”
// 如果"x的右孩子"不为空的话,将 “y” 设为 “x的右孩子的父亲”
y->left = x->right;
if (nullptr != x->right)
{
x->right->parent = y;
}
// 将 “y的父亲” 设为 “x的父亲”
x->parent = y->parent;
if (nullptr == y->parent)
{
root = x; // 如果 “y的父亲” 是空节点,则将x设为根节点
}
else
{
if (y == y->parent->left)
{
y->parent->left = x; // 如果y是它父节点的左孩子,将x设为“x的父节点的左孩子”
}
else
{
y->parent->right = x; // 如果y是它父节点的右孩子,则将x设为“y的父节点的右孩子”
}
}
// 将 “y” 设为 “x的右孩子”
x->right = y;
y->parent = x;
}
/*
* 将结点插入到红黑树中
*
* 参数说明:
* root 红黑树的根结点
* node 插入的结点 // 对应《算法导论》中的node
*/
template <class T>
void RBTree<T>::insert(RBTNode* &root, RBTNode* node)
{
RBTNode* y = nullptr;
RBTNode* x = root;
// 将红黑树当做一颗二叉查找树,将结点添加到二叉查找树中
while (nullptr != x)
{
y = x;
if (node->key < x->key)
{
x = x->left;
}
else
{
x = x->right;
}
}
node->parent = y;
if (nullptr != y)
{
if (node->key < y->key)
{
y->left = node;
}
else
{
y->right = node;
}
}
else // node为第一个被添加的结点
{
root = node;
}
// 设置结点的颜色为RED
node->color = RED;
// 将其重新修正为一颗满足所有条件的红黑树
insertFixUp(root, node);
}
/*
* 将结点(key为节点键值)插入到红黑树中
*
* 参数说明:
* tree 红黑树的根结点
* key 插入结点的键值
*/
template <class T>
void RBTree<T>::insert(T key)
{
RBTNode* z = nullptr;
// 如果创建结点失败,则返回
z = new RBTNode(key, BLACK, nullptr, nullptr, nullptr);
if (z == nullptr)
{
return;
}
insert(mRoot, z);
}
/*
* 红黑树插入修正函数
*
* 在向红黑树中插入节点之后(失去平衡),再调用该函数;
* 目的是将它重新塑造成一颗红黑树。
*
* 参数说明:
* root 红黑树的根
* node 插入的结点 // 对应《算法导论》中的z
*/
template <class T>
void RBTree<T>::insertFixUp(RBTNode* &root, RBTNode* node)
{
RBTNode *parent, *gparent;
// 若“父结点存在,并且父结点的颜色是红色”
while ((parent = rb_parent(node)) && rb_is_red(parent))
{
gparent = rb_parent(parent);
//若“父节点”是“祖父节点的左孩子”
if (parent == gparent->left)
{
// Case 1条件:叔叔节点是红色
{
RBTNode* uncle = gparent->right;
if (uncle && rb_is_red(uncle))
{
rb_set_black(uncle);
rb_set_black(parent);
rb_set_red(gparent);
node = gparent;
continue;
}
}
// Case 2条件:叔叔是黑色,且当前节点是右孩子
if (node == parent->right)
{
RBTNode* tmp;
leftRotate(root, parent); //将红结点node移动到parent的左孩子结点
tmp = parent;
parent = node;
node = tmp;
}
// Case 3条件:叔叔是黑色,且当前节点是左孩子。
rb_set_black(parent);
rb_set_red(gparent);
rightRotate(root, gparent);
}
else //若“父节点”是“祖父节点的右孩子”
{
// Case 1条件:叔叔节点是红色
{
RBTNode* uncle = gparent->left;
if (uncle && rb_is_red(uncle))
{
rb_set_black(uncle);
rb_set_black(parent);
rb_set_red(gparent);
node = gparent;
continue;
}
}
// Case 2条件:叔叔是黑色,且当前节点是左孩子
if (node == parent->left)
{
RBTNode* tmp;
rightRotate(root, parent); //将红结点node移动到parent的右孩子结点
tmp = parent;
parent = node;
node = tmp;
}
// Case 3条件:叔叔是黑色,且当前节点是右孩子
rb_set_black(parent);
rb_set_red(gparent);
leftRotate(root, gparent);
}
}
// 将根节点设为黑色
if (rb_is_red(root))
{
rb_set_black(root);
}
}
/*
* 删除红黑树中键值为key的节点
*
* 参数说明:
* tree 红黑树的根结点
*/
template <class T>
void RBTree<T>::remove(T key)
{
RBTNode* node;
node = search(mRoot, key);
if (nullptr != node)
{
remove(mRoot, node);
}
}
/*
* 删除结点(node),并返回被删除的结点
*
* 参数说明:
* root 红黑树的根结点
* node 删除的结点
*/
template <class T>
void RBTree<T>::remove(RBTNode *&root, RBTNode *node)
{
RBTNode *child, *parent;
RBTColor color;
// 被删除节点的"左右孩子都不为空"的情况
if (nullptr != node->left && nullptr != node->right)
{
// 被删节点的后继节点。(称为"取代节点")
// 用它来取代"被删节点"的位置,然后再将"被删节点"去掉
RBTNode* replace = node;
// 获取后继结点
replace = replace->right;
while (replace->left != nullptr)
{
replace = replace->left;
}
// "node节点"不是根节点(只有根节点不存在父节点)
if (rb_parent(node))
{
if (rb_parent(node)->left == node)
{
rb_parent(node)->left = replace;
}
else
{
rb_parent(node)->right = replace;
}
}
else
{
// "node节点"是根节点,更新根节点
root = replace;
}
// child是"取代节点"的右孩子,也是需要"调整的节点"
// "取代节点"肯定不存在左孩子!因为它是一个后继节点
child = replace->right;
parent = rb_parent(replace);
// 保存"取代节点"的颜色
color = rb_color(replace);
// "被删除节点"是"它的后继节点的父节点"
if (parent == node)
{
parent = replace;
}
else
{
// child不为空
if (child)
{
rb_set_parent(child, parent);
}
parent->left = child;
replace->right = node->right;
rb_set_parent(node->right, replace);
}
replace->parent = node->parent;
replace->color = node->color;
replace->left = node->left;
node->left->parent = replace;
if (color == BLACK) // 从根到叶子,黑色结点之和要保持相等
{
removeFixUp(root, child, parent);
}
delete node;
return;
}
if (node->left != nullptr)
{
child = node->left;
}
else
{
child = node->right;
}
parent = node->parent;
color = node->color;
// node有一个子结点
if (child)
{
child->parent = parent;
}
// "node节点"不是根节点
if (parent)
{
if (parent->left == node)
{
parent->left = child;
}
else
{
parent->right = child;
}
}
else
{
root = child;
}
if (color == BLACK)
{
removeFixUp(root, child, parent);
}
delete node;
}
/*
* 红黑树删除修正函数
*
* 在从红黑树中删除插入节点之后(红黑树失去平衡),再调用该函数;
* 目的是将它重新塑造成一颗红黑树。
*
* 参数说明:
* root 红黑树的根
* node 待修正的节点
*/
template <class T>
void RBTree<T>::removeFixUp(RBTNode *&root, RBTNode *node, RBTNode *parent)
{
RBTNode* other;
while ((!node || rb_is_black(node)) && node != root)
{
if (parent->left == node)
{
other = parent->right;
if (rb_is_red(other))
{
// Case 1: x的兄弟w是红色的,将x的兄弟w转变为黑色
rb_set_black(other);
rb_set_red(parent);
leftRotate(root, parent);
other = parent->right;
}
if ((!other->left || rb_is_black(other->left)) &&
(!other->right || rb_is_black(other->right)))
{
// Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的,将w置为红色即可
rb_set_red(other);
node = parent;
parent = rb_parent(node);
}
else
{
if (!other->right || rb_is_black(other->right))
{
// Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
rb_set_black(other->left);
rb_set_red(other);
rightRotate(root, other);
other = parent->right;
}
// Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
rb_set_color(other, rb_color(parent));
rb_set_black(parent);
rb_set_black(other->right);
leftRotate(root, parent);
node = root;
break;
}
}
else
{
other = parent->left;
if (rb_is_red(other))
{
// Case 1: x的兄弟w是红色的
rb_set_black(other);
rb_set_red(parent);
rightRotate(root, parent);
other = parent->left;
}
if ((!other->left || rb_is_black(other->left)) &&
(!other->right || rb_is_black(other->right)))
{
// Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的
rb_set_red(other);
node = parent;
parent = rb_parent(node);
}
else
{
if (!other->left || rb_is_black(other->left))
{
// Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
rb_set_black(other->right);
rb_set_red(other);
leftRotate(root, other);
other = parent->left;
}
// Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
rb_set_color(other, rb_color(parent));
rb_set_black(parent);
rb_set_black(other->left);
rightRotate(root, parent);
node = root;
break;
}
}
}
if (node)
{
rb_set_black(node);
}
}
template <class T>
void RBTree<T>::destroy(RBTNode *&tree)
{
if (nullptr == tree)
{
return;
}
if (nullptr != tree->left)
{
destroy(tree->left);
}
if (nullptr != tree->right)
{
destroy(tree->right);
}
delete tree;
tree = nullptr;
}
template <class T>
void RBTree<T>::destroy()
{
destroy(mRoot);
}
/*
* 打印"二叉查找树"
*
* key -- 节点的键值
* direction -- 0,表示该节点是根节点;
* -1,表示该节点是它的父结点的左孩子;
* 1,表示该节点是它的父结点的右孩子。
*/
template <class T>
void RBTree<T>::print(RBTNode* tree, T key, int direction) const
{
if(tree != NULL)
{
if(direction==0) // tree是根节点
cout << setw(2) << tree->key << "(b) is root" << endl;
else // tree是分支节点
cout << setw(2) << tree->key << (rb_is_red(tree)?"(r)":"(b)") << " is " << setw(2) << key << "'s " << setw(12) << (direction==1?"right child" : "left child") << endl;
print(tree->left, tree->key, -1);
print(tree->right,tree->key, 1);
}
}
template <class T>
void RBTree<T>::print()
{
if (mRoot != NULL)
print(mRoot, mRoot->key, 0);
}
main.cpp
#include "rbtree.cpp"
using namespace std;
int main()
{
int a[]= {10, 40, 30, 60, 90, 70, 20, 50, 80};
int check_insert=1; // "插入"动作的检测开关(0,关闭;1,打开)
int check_remove=1; // "删除"动作的检测开关(0,关闭;1,打开)
int i;
int ilen = (sizeof(a)) / (sizeof(a[0])) ;
RBTree<int>* tree = new RBTree<int>();
cout << "== 原始数据: ";
for(i=0; i<ilen; i++)
cout << a[i] <<" ";
cout << endl;
for(i=0; i<ilen; i++)
{
tree->insert(a[i]);
// 设置check_insert=1,测试"添加函数"
if(check_insert)
{
cout << "== 添加节点: " << a[i] << endl;
cout << "== 树的详细信息: " << endl;
tree->print();
cout << endl;
}
}
cout << "== 前序遍历: ";
tree->preOrder();
cout << "\n== 中序遍历: ";
tree->inOrder();
cout << "\n== 后序遍历: ";
tree->postOrder();
cout << endl;
cout << "== 最小值: " << *tree->minimum() << endl;
cout << "== 最大值: " << *tree->maximum() << endl;
cout << "== 树的详细信息: " << endl;
tree->print();
// 设置check_remove=1,测试"删除函数"
if(check_remove)
{
for(i=0; i<ilen; i++)
{
tree->remove(a[i]);
cout << "== 删除节点: " << a[i] << endl;
cout << "== 树的详细信息: " << endl;
tree->print();
cout << endl;
}
}
// 销毁红黑树
tree->destroy();
return 0;
}
运行结果如下:




实验结果证明:
红黑树的insert函数实现正确,且较好理解;但remove函数仍存在错误,且代码过程复杂,能力有限暂时还无法debug修复...

本文深入探讨红黑树的数据结构特点与实现细节,包括其性质、插入与删除操作的算法逻辑,通过代码示例展示如何构建红黑树,以及在实际应用中如何维护红黑树的平衡。
1166

被折叠的 条评论
为什么被折叠?



