红黑树
红黑树的概念
红黑树是一颗二叉搜索树,他的每个结点增加一个存储位来表示结点的颜色,可以是红色或黑色。通过对任何一条从根到叶子的路径上各个结点的颜色进行约束,红黑树确保没有一条路径会比其他路径长出2倍,因而是接近平衡的。
红黑树的规则
- 每个结点不是红色就是黑色
- 根结点是黑色的
- 如果一个结点是红色,则它的两个结点必须是黑色的,也就是说任意一条路径不会有连续的红色结点。
- 对于任意一个结点,从该结点到其所有NULL结点的简单路径上,均包含相同数量的黑色结点。
红黑树如何确保最长路不超过最短路的2倍
- 由上述规则4可知,从根到NULL结点的每条路径都有相同数量的黑色结点,所以极端场景下,最短路径就是全黑色结点的路径,假设最短路径的长度为bh。
- 由上述鬼规则2和3可知,任意一条路径不会有连续的红色结点,所以极端场景下,最长的路径解释一黑一红间隔组成,那么最长路径的长度为2*bh。
- 综合上述规则而言,理论上的全黑最短路径和一黑一红的最长路径并不是在每颗红黑树都存在的。bh<=h<=2*bh。

红黑树的实现
红黑树的结构
cpp
enum Colour {
RED,
BLACK
};
template<class K,class V>
struct RBTreeNode {
pair<K,V> _kv;
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
Colour _col;
RBTreeNode(const pair<K,V>& kv)
:_kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
{
}
};
template<class K, class V>
class RBTree {
typedef RBTreeNode<K,V> Node;
public:
private:
Node* _root = nullptr;
};
红黑树的插入
c表示当前结点,p表示当前结点的父亲结点,g表示p的父亲结点,u表示p的兄弟结点。
红黑树插入一个值
- 插入一个值按二叉搜索树规则进行插入,插入后我们只需要观察是否符合红黑树的4条规则。
- 如果是空树插入,新增结点是黑色结点。如果是非空树插入,新增结点必须是红色结点,因为非空树插入,新增黑色结点就破坏了规则4.
- 非空树插入后,新增结点必须是红色结点,如果父亲结点时黑色的,则没有违反任何规则,插入结束。
- 非空树插入后,新增结点必须时红色结点,如果父亲是红色的,违反了红色结点不能相邻的规则,则此时c是红色的,p是红色的,g手是黑色的,具体怎么变化要看u的变化。
情况1:变色
c为红,p为红,g为黑,u存在且为红,则将p和u变黑,g变红,把g当作新的c,继续往上更新。

因为p和u都是红色,g是黑色,把p和u变黑,左边子树路径各增加了一个黑色结点,g再变红,相当于保持g所在子树的黑色结点的数量不变,同时解决了c和p连续红色结点的问题,需要继续网上更新的原因,g此时是红色了,如果g的父亲是红色,那么就需要继续处理;如果g的父亲是黑色,则处理结束了;如果g是整棵树的根,再把g变回黑色。
情况2:单旋+变色
c为红,p为红,g为黑,u不存在或者存在且为黑,u不存在,则c一定是新增结点,u存在且为黑,则c一定不是新增,c之前是黑色的,是在c的子树中插入,变色将c从黑色变成红色,更新上来的。

p必须变黑,才能解决连续红色结点的问题,u不存在或者是黑色的,这里单纯的变色无法解决问题,需要旋转+变色。
情况3:双旋+变色
c为红,p为红,g为黑,u不存在或者u存在且为黑,u不存在,则c一定是新增结点,u存在且为黑,则c一定不是新增,c之前是黑色的,是在c的子树中插入的,变色将c从黑色变成红色,更新上来的。

红黑树的插入代码实现
cpp
bool Insert(const pair<K,V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
_root->_col = BLACK;
return true;
}
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(kv);
cur->_col = RED;
if (parent->_kv.first < kv.first)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
// 链接父亲
cur->_parent = parent;
// 父亲是红色,出现连续的红色节点,需要处理
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)
{
// g
// p u
Node* uncle = grandfather->_right;
if (uncle && uncle->_col == RED)
{
// 变色
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
// 继续往上处理
cur = grandfather;
parent = cur->_parent;
}
else
{
if (cur == parent->_left)
{
// g
// p u
// c
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g
// p u
// c
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else
{
// g
// u p
Node* uncle = grandfather->_left;
// 叔叔存在且为红,-》变色即可
if (uncle && uncle->_col == RED)
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
// 继续往上处理
cur = grandfather;
parent = cur->_parent;
}
else // 叔叔不存在,或者存在且为黑
{
// 情况二:叔叔不存在或者存在且为黑
// 旋转+变色
// g
// u p
// c
if (cur == parent->_right)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return true;
}
红黑树的查找
cpp
bool find(const pair<K, V>& kv) {
Node* cur = _root;
while (cur) {
if (cur->_kv.first > kv.first) {
cur = cur->_left;
}
else if (cur->_kv.first < kv.first) {
cur = cur->_right;
}
else if (cur->_kv.first == kv.first) {
return true;
}
}
return false;
}
红黑树的验证
这里获取最长路径和最短路径,检查最长路径不超过最短路径的2倍是不可行的,因为就算满足这个条件,红黑树也不可能满足规则,一定要去检查四条规则。
- 枚举颜色类型,保证了颜色不是黑色就是红色。
- 根结点必须是黑色。
- 前序遍历检查,遇到红色结点查孩子不太方便,因为孩子有两个,且不一定存在,反过来检查父亲的颜色就方便多了。
- 前序遍历,遍历过程中用形参记录根到当前结点的黑色结点数量,前序遍历遇到黑色结点就++,走到空就计算出一条路径的黑色结点数量。在任意一条路径黑色结点数量作为参考值,依次比较即可。
cpp
//前序遍历的思想
//前序遍历的思想
bool IsBalance() {
if (_root == nullptr) {
return true;
}
if (_root->_col == RED) {
return false;
}
int refNum = 0;
Node* cur = _root;
while (cur) {
if (cur->_col == BLACK) {
refNum++;
}
cur = cur->_left;
}
return Check(_root, 0, refNum);
}
bool Check(Node* root, int blackNum, const int refNum) {
if (root == nullptr) {
if (refNum != blackNum) {
cout << "存在黑色结点的数量不相等" << endl;
return false;
}
return true;
}
if (root->_col == RED && root->_parent->_col == RED) {
cout << root->_kv.first << "存在连续的红色结点" << endl;
return false;
}
if (root->_col == BLACK) {
blackNum++;
}
return Check(root->_left, blackNum, refNum) && Check(root->_right, blackNum, refNum);
}
红黑树整体代码
实现代码
cpp
#pragma once
#include<iostream>
#include<vector>
using namespace std;
enum Colour {
RED,
BLACK
};
template<class K,class V>
struct RBTreeNode {
pair<K,V> _kv;
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
Colour _col;
RBTreeNode(const pair<K,V>& kv)
:_kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
{
}
};
template<class K, class V>
class RBTree {
typedef RBTreeNode<K,V> Node;
public:
bool Insert(const pair<K,V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
_root->_col = BLACK;
return true;
}
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(kv);
cur->_col = RED;
if (parent->_kv.first < kv.first)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
// 链接父亲
cur->_parent = parent;
// 父亲是红色,出现连续的红色节点,需要处理
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)
{
// g
// p u
Node* uncle = grandfather->_right;
if (uncle && uncle->_col == RED)
{
// 变色
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
// 继续往上处理
cur = grandfather;
parent = cur->_parent;
}
else
{
if (cur == parent->_left)
{
// g
// p u
// c
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g
// p u
// c
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else
{
// g
// u p
Node* uncle = grandfather->_left;
// 叔叔存在且为红,-》变色即可
if (uncle && uncle->_col == RED)
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
// 继续往上处理
cur = grandfather;
parent = cur->_parent;
}
else // 叔叔不存在,或者存在且为黑
{
// 情况二:叔叔不存在或者存在且为黑
// 旋转+变色
// g
// u p
// c
if (cur == parent->_right)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return true;
}
bool find(const pair<K, V>& kv) {
Node* cur = _root;
while (cur) {
if (cur->_kv.first > kv.first) {
cur = cur->_left;
}
else if (cur->_kv.first < kv.first) {
cur = cur->_right;
}
else if (cur->_kv.first == kv.first) {
return true;
}
}
return false;
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* pParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (pParent->_left == parent)
{
pParent->_left = subL;
}
else
{
pParent->_right = subL;
}
subL->_parent = pParent;
}
}
void RotateL(Node* parent) {
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL) {
subRL->_parent = parent;
}
Node* pParent = parent-> _parent;
subR->_left = parent;
parent->_parent = subR;
if (parent == _root) {
_root = subR;
subR->_parent = nullptr;
}
else {
if (pParent->_left == parent) {
pParent->_left = subR;
subR->_parent = pParent;
}
else {
pParent->_right = subR;
subR->_parent = pParent;
}
}
}
void Inorder() {
_Inorder(_root);
}
//前序遍历的思想
bool IsBalance() {
if (_root == nullptr) {
return true;
}
if (_root->_col == RED) {
return false;
}
int refNum = 0;
Node* cur = _root;
while (cur) {
if (cur->_col == BLACK) {
refNum++;
}
cur = cur->_left;
}
return Check(_root, 0, refNum);
}
private:
void _Inorder(Node* root) {
if (root == nullptr) {
return;
}
_Inorder(root->_left);
cout << root->_kv.first << ":" << root->_kv.second << endl;
_Inorder(root->_right);
}
bool Check(Node* root, int blackNum, const int refNum) {
if (root == nullptr) {
if (refNum != blackNum) {
cout << "存在黑色结点的数量不相等" << endl;
return false;
}
return true;
}
if (root->_col == RED && root->_parent->_col == RED) {
cout << root->_kv.first << "存在连续的红色结点" << endl;
return false;
}
if (root->_col == BLACK) {
blackNum++;
}
return Check(root->_left, blackNum, refNum) && Check(root->_right, blackNum, refNum);
}
private:
Node* _root = nullptr;
};
测试代码
cpp
#include"RBTree.h"
void TestRBTree1() {
RBTree<int, int> t;
int a[] = { 16,3,7,11,9,26,18,14,15 };
//int a[] = { 4,2,6,1,3,5,15,7,16,14 };
for (auto& e : a) {
t.Insert({ e,e });
}
t.Inorder();
cout << t.find(make_pair(26,26)) << endl;
cout << t.IsBalance() << endl;
}
void TestRBTree2() {
const int N = 100000;
vector<int> v;
v.reserve(N);
srand(time(0));
for (size_t i = 0; i < N; ++i) {
v.push_back(rand() + i);
}
size_t begin2 = clock();
RBTree<int, int> t;
for (auto& e : v) {
t.Insert(make_pair(e,e));
}
size_t end2 = clock();
cout << "Insert:" << end2 - begin2 << endl;
cout << t.IsBalance() << endl;
}
int main() {
TestRBTree1();
return 0;
}

觉得我回答有用的话,记得点个关注哟!谢谢支持!