目录
1 红黑树的概念
红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或 Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
2 红黑树的性质
1. 每个结点不是红色就是黑色
2. 根节点是黑色的
3. 如果一个节点是红色的,则它的两个孩子结点是黑色的
4. 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点
5. 每个叶子结点都是黑色的(此处的叶子结点指的是空结点)
为什么满足上面条件就能保证最长路径不会超过最短路径的二倍呢!
- 根据第4点我们可以知道:如果最长路径的子树是最短路径子树长度的二倍则说明最短路径子树的节点全为黑色。否则不满足条件4。
- 而如果继续在最长路径子树插入节点则必须要旋转,否则不满足条件3。
3 红黑树节点的定义
enum Color
{
BLACK,
RED,
};
template<class k, class v >
struct RBTNode
{
pair<k, v> _kv;
RBTNode<k, v>* _left;
RBTNode<k, v>* _right;
RBTNode<k, v>* _parent;
Color_col;
RBTNode(const pair<k, v>& key)
:_kv(key)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _col(RED)
{}
};
为什么初始化节点为红色呢?
因为如果初始化黑色节点则必定破坏性质4.导致每个路径黑色节点数量不一样,而插入红色则有可能不会破坏性质3.所以插入为红色节点。
4 红黑树插入操作
1.红黑树是在二叉搜索树的基础上加上其平衡限制条件,因此红黑树的插入可分为两步:
- 按照二叉搜索的树规则插入新节点
bool Insert(const pair<k, v>& ret)
{
if (_root == nullptr)
{
_root = new Node(ret);
_root->_col = BLACK;
return true;
}
Node* prev = nullptr;
Node* root = _root;
while (root != nullptr)
{
if (root->_kv.first > ret.first)
{
prev = root;
root = root->_left;
}
else if (root->_kv.first < ret.first)
{
prev = root;
root = root->_right;
}
else return false;
}
Node* cur = new Node(ret);
if (prev->_kv.first > ret.first)
{
prev->_left = cur;
cur->_parent = prev;
}
else
{
prev->_right = cur;
cur->_parent = prev;
}
while (prev && prev->_col == RED)
{
//颜色调整和旋转
}
_root->_col = BLACK;
return true;
}
2. 检测新节点插入后,红黑树的性质是否造到破坏
因为新节点的默认颜色是红色,因此:如果其双亲节点的颜色是黑色,没有违反红黑树任何 性质,则不需要调整;但当新插入节点的双亲节点颜色为红色时,就违反了性质三不能有连 在一起的红色节点,此时需要对红黑树分情况来讨论:
情况一: cur为红,p为红,g为黑,u存在且为红
解决方式:将p,u改为黑,g改为红,然后把g当成cur,继续向上调整。
情况二: cur为红,p为红,g为黑,u不存在/u存在且为黑
说明:u的情况有两种
1.如果u节点不存在,则cur一定是新插入节点,因为如果cur不是新插入节点则cur和p一定有一个节点的颜色是黑色,就不满足性质4:每条路径黑色节点个数相同。
2.如果u节点存在,则其一定是黑色的,那么cur节点原来的颜色一定是黑色的现在看到其是红色的原因是因为cur的子树在调整的过程中将cur节点的颜色由黑色改成红色。
p为g的左孩子,cur为p的左孩子,则进行右单旋转;
相反, p为g的右孩子,cur为p的右孩子,则进行左单旋转 p、g变色--p变黑,g变红
情况三: cur为红,p为红,g为黑,u不存在/u存在且为黑
p为g的左孩子,cur为p的右孩子,则针对p做左单旋转;
相反, p为g的右孩子,cur为p的左孩子,则针对p做右单旋转 则转换成了情况2
//insert 中while循环的实现
while (prev && prev->_col == RED)
{
Node* grandfather = prev->_parent;
Node* uncle = nullptr;
if (grandfather->_left == prev)
{
uncle = grandfather->_right;
}
else
{
uncle = grandfather->_left;
}
if (uncle && uncle->_col == RED)
{
prev->_col = uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
prev = cur->_parent;
}
else if (uncle == nullptr || uncle->_col == BLACK)
{
if (cur == prev->_left && grandfather->_left == prev)
{
RotetaR(grandfather);
grandfather->_col = RED;
prev->_col = BLACK;
}
else if (cur == prev->_right && grandfather->_right == prev)
{
RotetaL(grandfather);
grandfather->_col = RED;
prev->_col = BLACK;
}
else if (grandfather->_left == prev && prev->_right == cur)
{
RotetaL(prev);
RotetaR(grandfather);
grandfather->_col = RED;
cur->_col = BLACK;
}
else if (grandfather->_right == prev && prev->_left == cur)
{
RotetaR(prev);
RotetaL(grandfather);
grandfather->_col = RED;
cur->_col = BLACK;
}
break;
}
}
void RotetaL(Node* prev)
{
Node* right = prev->_right;
Node* parent = prev->_parent;
prev->_right = right->_left;
if (right->_left)
{
right->_left->_parent = prev;
}
right->_left = prev;
prev->_parent = right;
if (parent == nullptr)
{
_root = right;
}
else
{
if (parent->_left == prev)
{
parent->_left = right;
}
else
{
parent->_right = right;
}
}
right->_parent = parent;
}
void RotetaR(Node* prev)
{
Node* left = prev->_left;
Node* parent = prev->_parent;
prev->_left = left->_right;
if (left->_right)
{
left->_right->_parent = prev;
}
left->_right = prev;
prev->_parent = left;
if (parent == nullptr)
{
_root = left;
}
else
{
if (parent->_left == prev)
{
parent->_left = left;
}
else
{
parent->_right = left;
}
}
left->_parent = parent;
}
5 红黑树的验证
bool IsValidRBTree()
{
//空树为真
if (_root == nullptr)
{
return true;
}
//判断根节点颜色
if (_root->_col == RED)
{
cout << "根节点为红色" << endl;
return false;
}
//统计一条路径黑色节点的个数
int blackcount = 0;
Node* root = _root;
while (root)
{
if (root->_col == BLACK)
{
blackcount++;
}
root = root->_left;
}
//对每一条路径进行判断
return _IsValidRBTree(_root, blackcount, 0);
}
private:
bool _IsValidRBTree(Node* root, int blackcount, int t)
{
//遇到空判断黑色节点个数是否相等
if (root == nullptr)
{
//cout << t << endl;
return t == blackcount;
}
if (root->_col == BLACK)
{
t++;
}
Node* parent = root->_parent;
//判断是否有连续两个红色节点
if (parent && parent->_col == RED && root->_col == RED)
{
cout << "连续两个红色节点" << endl;
return false;
}
//递归检查每一条路径黑色节点数然后返回
return _IsValidRBTree(root->_right, blackcount, t) && _IsValidRBTree(root->_right, blackcount, t);
}
6 红黑树与AVL树的比较
红黑树和AVL树都是高效的平衡二叉树,增删改查的时间复杂度都是O($log_2 N$),红黑树不追 求绝对平衡,其只需保证最长路径不超过最短路径的2倍,相对而言,降低了插入和旋转的次数, 所以在经常进行增删的结构中性能比AVL树更优,而且红黑树实现比较简单,所以实际运用中红黑树更多。
C++ STL库中set 和map 的底层用的就是红黑树。
感谢大家的观看!
#include<iostream>
#include<cassert>
using namespace std;
namespace bit
{
enum Color
{
BLACK,
RED,
};
template<class k, class v >
struct RBTNode
{
pair<k, v> _kv;
RBTNode<k, v>* _left;
RBTNode<k, v>* _right;
RBTNode<k, v>* _parent;
Color _col;
RBTNode(const pair<k, v>& key)
:_kv(key)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _col(RED)
{}
};
template<class k, class v = int>
class RBTree
{
typedef RBTNode<k, v> Node;
public:
RBTree() = default;
RBTree(const RBTree& root)
{
_root = Copy(root._root);
}
~RBTree()
{
Destroy(_root);
_root = nullptr;
}
bool Insert(const pair<k, v>& ret)
{
if (_root == nullptr)
{
_root = new Node(ret);
_root->_col = BLACK;
return true;
}
Node* prev = nullptr;
Node* root = _root;
while (root != nullptr)
{
if (root->_kv.first > ret.first)
{
prev = root;
root = root->_left;
}
else if (root->_kv.first < ret.first)
{
prev = root;
root = root->_right;
}
else return false;
}
Node* cur = new Node(ret);
if (prev->_kv.first > ret.first)
{
prev->_left = cur;
cur->_parent = prev;
}
else
{
prev->_right = cur;
cur->_parent = prev;
}
while (prev && prev->_col == RED)
{
Node* grandfather = prev->_parent;
Node* uncle = nullptr;
if (grandfather->_left == prev)
{
uncle = grandfather->_right;
}
else
{
uncle = grandfather->_left;
}
if (uncle && uncle->_col == RED)
{
prev->_col = uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
prev = cur->_parent;
}
else if (uncle == nullptr || uncle->_col == BLACK)
{
if (cur == prev->_left && grandfather->_left == prev)
{
RotetaR(grandfather);
grandfather->_col = RED;
prev->_col = BLACK;
}
else if (cur == prev->_right && grandfather->_right == prev)
{
RotetaL(grandfather);
grandfather->_col = RED;
prev->_col = BLACK;
}
else if (grandfather->_left == prev && prev->_right == cur)
{
RotetaL(prev);
RotetaR(grandfather);
grandfather->_col = RED;
cur->_col = BLACK;
}
else if (grandfather->_right == prev && prev->_left == cur)
{
RotetaR(prev);
RotetaL(grandfather);
grandfather->_col = RED;
cur->_col = BLACK;
}
break;
}
}
_root->_col = BLACK;
return true;
}
bool Find(const k& ret)
{
Node* root = _root;
while (root != nullptr)
{
if (root->_kv.first > ret)
{
root = root->_left;
}
else if (root->_kv.first < ret)
{
root = root->_right;
}
else return true;
}
return false;
}
void Inorder()
{
inorder(_root);
cout << endl;
}
bool Erase(const k& ret)
{
Node* prev = nullptr;
Node* root = _root;
while (root != nullptr)
{
if (ret > root->_kv.first)
{
prev = root;
root = root->_right;
}
else if (ret < root->_kv.first)
{
prev = root;
root = root->_left;
}
else
{
if (root->_left == nullptr)
{
if (prev == nullptr)
{
_root = root->_right;
if (_root)
_root->_parent = nullptr;
}
else if (prev->_left == root)
{
prev->_left = root->_right;
if (root->_right)
{
root->_right->_parent = prev;
}
}
else if (prev->_right == root)
{
prev->_right = root->_right;
if (root->_right)
{
root->_right->_parent = prev;
}
}
delete root;
return true;
}
else if (root->_right == nullptr)
{
if (prev == nullptr)
{
_root = root->_left;
if (_root)
_root->_parent = nullptr;
}
else if (prev->_left == root)
{
prev->_left = root->_left;
if (root->_left)
{
root->_left->_parent = prev;
}
}
else if (prev->_right == root)
{
prev->_right = root->_left;
if (root->_left)
{
root->_left->_parent = prev;
}
}
delete root;
return true;
}
else//左右节点都不为空找右子树最小节点||左子树最大节点
{
Node* right = root->_right;
prev = root;
while (right->_left != nullptr)
{
prev = right;
right = right->_left;
}
root->_kv.first = right->_kv.first;
root->_kv.second = right->_kv.second;
if (prev == root)
{
prev->_right = right->_right;
}
else
{
prev->_left = right->_right;
}
delete right;
return true;
}
}
}
return false;
}
bool IsValidRBTree()
{
if (_root == nullptr)
{
return true;
}
if (_root->_col == RED)
{
cout << "根节点为红色" << endl;
return false;
}
int blackcount = 0;
Node* root = _root;
while (root)
{
if (root->_col == BLACK)
{
blackcount++;
}
root = root->_left;
}
return _IsValidRBTree(_root, blackcount, 0);
}
private:
bool _IsValidRBTree(Node* root, int blackcount, int t)
{
if (root == nullptr)
{
//cout << t << endl;
return t == blackcount;
}
if (root->_col == BLACK)
{
t++;
}
Node* parent = root->_parent;
if (parent && parent->_col == RED && root->_col == RED)
{
cout << "连续两个红色节点" << endl;
return false;
}
return _IsValidRBTree(root->_right, blackcount, t) && _IsValidRBTree(root->_right, blackcount, t);
}
void RotetaL(Node* prev)
{
Node* right = prev->_right;
Node* parent = prev->_parent;
prev->_right = right->_left;
if (right->_left)
{
right->_left->_parent = prev;
}
right->_left = prev;
prev->_parent = right;
if (parent == nullptr)
{
_root = right;
}
else
{
if (parent->_left == prev)
{
parent->_left = right;
}
else
{
parent->_right = right;
}
}
right->_parent = parent;
}
void RotetaR(Node* prev)
{
Node* left = prev->_left;
Node* parent = prev->_parent;
prev->_left = left->_right;
if (left->_right)
{
left->_right->_parent = prev;
}
left->_right = prev;
prev->_parent = left;
if (parent == nullptr)
{
_root = left;
}
else
{
if (parent->_left == prev)
{
parent->_left = left;
}
else
{
parent->_right = left;
}
}
left->_parent = parent;
}
void inorder(Node* root)
{
if (root == nullptr) return;
inorder(root->_left);
cout << root->_kv.first << ' ';
inorder(root->_right);
}
Node* Copy(const Node* root)
{
if (root == nullptr) return nullptr;
Node* val = new Node(root->_kv);
val->_col = root->_col;
val->_left = Copy(root->_left);
if (val->_left)
val->_left->_parent = val;
val->_right = Copy(root->_right);
if (val->_right)
val->_right->_parent = val;
return val;
}
void Destroy(Node* root)
{
if (root == nullptr) return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
Node* _root = nullptr;
};
}