目录
一 红黑树的概念
红黑树
,是一种
二叉搜索树
,但
在每个结点上增加一个存储位表示结点的颜色,可以是
Red
或
Black
。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,
红黑树确保没有一条路径会比其他路径长出俩倍
,因而是
接近平衡
的。
二 红黑树的性质
(1)每个结点不是红色就是黑色(
双色
)
(2)
根节点是黑色的
(3)如果一个节点是红色的,则它的两个孩子结点是黑色的 (
红色不能连续)
(4)对于每个结点,从该结点到其所有后代叶结点的简单路径上,
均包含相同数目的黑色结点
(5)每个叶子结点都是黑色的
(
此处的叶子结点指的是空结点
)
三 红黑树节点的定义
enum Colour
{
RED,
BLACK,
};
//红黑树的结点
template<class T>
struct RBTreeNode
{
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
T _data;
Colour _col;
RBTreeNode(const T& x)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _data(x)
, _col(RED) //默认红色
{}
};
四 红黑树的插入操作
红黑树是在二叉搜索树的基础上加上其平衡限制条件,因此红黑树的插入可分为两步:
1 按照二叉搜索的树规则插入新节点
pair<Node*, bool> Insert(const T& data)
{
if (_root == nullptr)
{
_root = new Node(data);
_root->_col = BLACK; //根节点为黑色
return make_pair(_root, true);
}
KeyOfT kot;
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (kot(cur->_data) < kot(data))
{
parent = cur;
cur = cur->_right;
}
else if (kot(cur->_data) > kot(data))
{
parent = cur;
cur = cur->_left;
}
else
{
return make_pair(cur, false);
}
}
Node* newnode = new Node(data);
newnode->_col = RED;
if (kot(parent->_data) < kot(data))
{
parent->_right = newnode;
newnode->_parent = parent;
}
else
{
parent->_left = newnode;
newnode->_parent = parent;
}
cur = newnode;
}
2 检测新节点插入后,红黑树的性质是否造到破坏
新节点的默认颜色是红色
,如果
其
双亲节点的颜色是黑色,则不需要处理
,没有违反红黑树任何性质,如果
新插入节点的
双亲节点颜色为红色时,就出现了连续的红色节点,此时需要分情况处理
,
cur
为当前节点,
p
为父节点,
g
为祖父节点,
u
为叔叔节点
2.1 情况一
(1)cur为红,p为红,g为黑,u存在且为红
(2)解决方式:将
p,u
改为黑,
g
改为红,然后把
g
当成
cur
,继续向上调整。
2.2 情况二
(1)cur为红,p为红,g为黑,u不存在/u为黑
①如果u节点不存在,则cur一定是新插入节点,因为如果cur不是新插入节点,则cur和p一定有一个节点的颜色是黑色,不满足每条路径黑色节点个数相同。
②如果u节点存在,则其一定是黑色的,那么cur节点原来的颜色一定是黑色的,现在看到其是红色的原因是因为cur的子树在调整的过程中将cur节点的颜色由黑色改成红色。
p为g的左孩子,cur为p的左孩子,则进行右单旋转;
p为g的右孩子,cur为p的右孩子,则进行左单旋转;
p、g变色--p变黑,g变红;
2.3 情况三
cur为红,p为红,g为黑,u不存在/u为黑
p为g的左孩子,cur为p的右孩子,则针对p做左单旋转;
p为g的右孩子,cur为p的左孩子,则针对p做右单旋转;
则转换为情况二进行处理
2.4 代码实现
// 如果父亲存在,且颜色为红色就需要处理
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
// 关键是看叔叔
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
// 情况1:uncle存在且为红
if (uncle && uncle->_col == RED)
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
// 继续往上处理
cur = grandfather;
parent = cur->_parent;
}
else // 情况2+3:uncle不存在 uncle存在且为黑
{
// 情况2:单旋
if (cur == parent->_left)
{
RotateR(grandfather);
grandfather->_col = RED;
parent->_col = BLACK;
}
else // 情况3:双旋
{
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else // parent == grandfather->_right
{
Node* uncle = grandfather->_left;
// 情况1:
if (uncle && uncle->_col == RED) //uncle 存在且为红
{
uncle->_col = parent->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else // 情况2:+ 情况3: //uncle不存在或者存在为黑色
{
if (cur == parent->_right) //right right 直线情况单旋
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else // cur == parent->_left //折线情况
{
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
// 插入结束
break;
}
}
}
_root->_col = BLACK; //保证根节点为黑色
return make_pair(newnode, true);
//旋转相关代码
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
subR->_left = parent;
Node* parentParent = parent->_parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
_root->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subR;
}
else
{
parentParent->_right = subR;
}
subR->_parent = parentParent;
}
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
Node* parentParent = parent->_parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
parentParent->_left = subL;
else
parentParent->_right = subL;
subL->_parent = parentParent;
}
}
五 红黑树的验证
1 检测其是否满足二叉搜索树
中序遍历是否为有序序列
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.first << ":"<<root->_kv.second<<endl;
_InOrder(root->_right);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
2 检测其是否满足红黑树的性质
bool _CheckBlance(Node* root, int blackNum, int count)
{
if (root == nullptr)
{
if (count != blackNum)
{
cout << "黑色节点的数量不相等" << endl;
return false;
}
return true;
}
if (root->_col == RED && root->_parent->_col == RED)
{
cout << "存在连续的红色节点" << endl;
return false;
}
if (root->_col == BLACK)
{
count++;
}
return _CheckBlance(root->_left, blackNum, count) && _CheckBlance(root->_right, blackNum, count);
}
bool CheckBlance()
{
if (_root == nullptr)
{
return true;
}
if (_root->_col == RED)
{
cout << "根节点是红色的" << endl;
return false;
}
// 找最左路径做黑色节点数量参考值
int blackNum = 0;
Node* left = _root;
while (left)
{
if (left->_col == BLACK)
{
blackNum++;
}
left = left->_left;
}
int count = 0;
return _CheckBlance(_root, blackNum, count);
}
六 红黑树与AVL树的比较
(1)红黑树和
AVL
树都是高效的平衡二叉树,增删改查的时间复杂度都是
O( log2N
)
(2)
红黑树不追求绝对平衡,其
只需保证最长路径不超过最短路径的
2
倍,相对而言,降低了插入和旋转的次数,所以
在经常进行增删的结构中性能比AVL树更优
,而且
红黑树实现比较简单
,所以实际运用中红黑树更多。