红黑树
目录
一、红黑树的特点
一种二叉搜索树
每个节点增加一个存储位表示节点颜色(红色 or 黑色)
通过对任何一条从根到叶子的路径上各个节点的颜色进行约束
确保没有一条路径会比其他路径长出2倍,从而接近平衡
二、红黑树的规则
- 每个节点不是红色就是黑色
- 根节点是黑色
- 一个节点是红色,则它的两个孩子节点必须是黑色(任意一条路径不会有连续的红色节点)
- 对于任意一个节点,从该节点到其所有NULL节点的简单路径上,均包含相同数量的黑色节点
(从根开始每条路径黑色节点的数量都是相等的)




**问:**任何确保最长路径不超过最短路径的2倍?
假设每条路径有x个黑色节点
最短路径:x(全为黑节点)
最长路径:2*x(全为一黑一红)
由此可得:x < h < 2 * x
三、红黑树的效率
假设N是红黑树中节点数量,h是最短路径长度
可得:2^h - 1 <= N < 2^(2*h) - 1
由此推出:h ≈ logN
红黑树增删查改最坏(最长路径):2 * logN
时间复杂度:O(logN)
AVL树通过高度差控制了平衡
红黑树通过4条规则的颜色约束,间接实现了近似平衡
效率属于同一档次,插入相同的节点,红黑树旋转次数更少

四、红黑树的实现
4.1.红黑树的结构
颜色定义
cpp
//枚举值表示颜色
//0-RED 1-BLACK
enum Colour
{
RED,
BLACK
};
树节点结构
cpp
//默认实现方式为K/V
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)
{}
};
树结构
cpp
template < class K, class V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
public:
private:
Node * _root = nullptr;
};
4.2.红黑树的插入
按二叉搜索树规则进行插入
如果是空树插入,新增节点是黑色节点
如果是非空树插入,新增节点必须是红色节点
(非空树插入,新增黑色节点破坏了规则4)
非空树插入后,新增节点必须为红色节点
如果父亲节点是黑色的,则没有违法规则
如果父亲节点是红色的,则违法了规则3
(注:此时c是红色,p是红色,g必须为黑色,三个节点颜色固定,主要看节点u)

注:
新增节点为c(cur)c的父亲节点为p(parent)
p的父亲节点为g(grand)p的兄弟节点为u(uncle)
**情况1:**变色
c为红、p为红、g为黑,u存在且为红
将p和u变黑、g变红,再把g当作新的c,继续向上更新





**情况2:**单旋+变色
c为红、p为红、g为黑,u不存在或者u存在且为黑
u不存在:c一定为新增节点
u存在:c一定不为新增节点



**情况3:**双旋+变色
c为红、p为红、g为黑,u不存在或者u存在且为黑
u不存在:c一定为新增节点
u存在: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;
//遍历二叉树
//循环条件为cur不为空
while (cur)
{
//如果当前节点的key值小于新节点的key值
if (cur->_kv.first < kv.first)
{
//更新父节点
parent = cur;
//更新当前节点(指向右子树)
cur = cur->_right;
}
else if (cur->_kv.first > kv.first)//如果当前节点的key值大于新节点的key值
{
//更新父节点
parent = cur;
//更新当前节点(指向左子树)
cur = cur->_left;
}
else//当前节点的key值等于新节点的key值
{
//存在重复值,返回插入失败
return false;
}
}
//将当前节点(指向空)赋为新节点
cur = new Node(kv);
//将插入节点设置为红色
cur->_col = RED;
//如果父节点的key值小于新节点的key值
if (parent->_kv.first < kv.first)
{
//将新节点赋为父节点的右子树
parent->_right = cur;
}
else//如果父节点的key值大于新节点的key值
{
//将新节点赋为父节点的左子树
parent->_left = cur;
}
//将当前节点与父节点链接
cur->_parent = parent;
//循环条件:1、父亲节点存在 2、父亲节点颜色为红色
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//如果叔父节点不存在,或者为黑色
{
if (cur == parent->_right)//如果新增节点在父亲节点的右边(左单旋)
{
// g
// (u) p
// c
//祖父节点左单旋
RotateL(grandfather);
//父亲节点设为黑色
parent->_col = BLACK;
//祖父节点设为红色
grandfather->_col = RED;
}
else//如果新增节点在父亲节点的右边(右左双旋)
{
// g
// (u) p
// c
//父亲节点右单旋
RotateR(parent);
//祖父节点左单旋
RotateL(grandfather);
//当插入节点设为黑色
cur->_col = BLACK;
//祖父节点设为红色
grandfather->_col = RED;
}
break;
}
}
}
//保证根节点为黑色
_root->_col = BLACK;
//返回插入成功
return true;
}
4.3.红黑树的检查
**规则1:**枚举颜色类型,保证了颜色不是黑色就是红色
**规则2:**直接检查根的颜色
**规则3:**前序遍历检查父亲的颜色
**规则4:**前序遍历,用形参记录从根节点到当前节点的黑色节点数量,走到空就计算出一条路径的
黑色节点再以任意一条路径的黑色节点数量作为参考值,进行比较

4.4.红黑树总代码
cpp
#pragma once
#include <iostream>
using namespace std;
#include "assert.h"
#include "stdbool.h"
//枚举值表示颜色
//0-RED 1-BLACK
enum Colour
{
RED,
BLACK
};
//默认实现方式为K/V
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:
//右单旋
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;
_root->_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 (pParent == nullptr)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parent == pParent->_left)
{
pParent->_left = subR;
}
else
{
pParent->_right = subR;
}
subR->_parent = pParent;
}
}
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;
//遍历二叉树
//循环条件为cur不为空
while (cur)
{
//如果当前节点的key值小于新节点的key值
if (cur->_kv.first < kv.first)
{
//更新父节点
parent = cur;
//更新当前节点(指向右子树)
cur = cur->_right;
}
else if (cur->_kv.first > kv.first)//如果当前节点的key值大于新节点的key值
{
//更新父节点
parent = cur;
//更新当前节点(指向左子树)
cur = cur->_left;
}
else//当前节点的key值等于新节点的key值
{
//存在重复值,返回插入失败
return false;
}
}
//将当前节点(指向空)赋为新节点
cur = new Node(kv);
//将插入节点设置为红色
cur->_col = RED;
//如果父节点的key值小于新节点的key值
if (parent->_kv.first < kv.first)
{
//将新节点赋为父节点的右子树
parent->_right = cur;
}
else//如果父节点的key值大于新节点的key值
{
//将新节点赋为父节点的左子树
parent->_left = cur;
}
//将当前节点与父节点链接
cur->_parent = parent;
//循环条件:1、父亲节点存在 2、父亲节点颜色为红色
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//如果叔父节点不存在,或者为黑色
{
if (cur == parent->_right)//如果新增节点在父亲节点的右边(左单旋)
{
// g
// (u) p
// c
//祖父节点左单旋
RotateL(grandfather);
//父亲节点设为黑色
parent->_col = BLACK;
//祖父节点设为红色
grandfather->_col = RED;
}
else//如果新增节点在父亲节点的右边(右左双旋)
{
// g
// (u) p
// c
//父亲节点右单旋
RotateR(parent);
//祖父节点左单旋
RotateL(grandfather);
//当插入节点设为黑色
cur->_col = BLACK;
//祖父节点设为红色
grandfather->_col = RED;
}
break;
}
}
}
//保证根节点为黑色
_root->_col = BLACK;
//返回插入成功
return true;
}
//中序遍历打印
void InOrder()
{
_InOrder(_root);
cout << endl;
}
//平衡检测
bool IsBalanceTree()
{
return _IsBalanceTree(_root);
}
//高度计算
int Height()
{
return _Height(_root);
}
//节点计算
int Size()
{
return _Size(_root);
}
//查找
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_kv.first < key)
{
cur = cur->_right;
}
else if (cur->_kv.first > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}
//检查
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:
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);
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.first << ":" << root->_kv.second << endl;
_InOrder(root->_right);
}
int _Height(Node* root)
{
if (root == nullptr)
{
return 0;
}
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
int _Size(Node* root)
{
if (root == nullptr)
{
return 0;
}
return _Size(root->_left) + _Size(root->_right) + 1;
}
Node * _root = nullptr;
};
测试代码
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "RBTree.h"
#include <vector>
//测试插入,中序遍历代码
void TestRBTree1()
{
RBTree<int, int> t;
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
for (auto e : a)
{
t.Insert({ e, e });
}
t.InOrder();
cout << t.IsBalance() << endl;
}
//插入一堆随机值,测试平衡,高度和性能
void TestRBTree2()
{
const int N = 1000000;
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;
cout << "Height:" << t.Height() << endl;
cout << "Size:" << t.Size() << endl;
size_t begin1 = clock();
////确定在的值
///*for (auto e : v)
//{
// t.Find(e);
//}*/
// 随机值
for (size_t i = 0; i < N; i++)
{
t.Find((rand() + i));
}
size_t end1 = clock();
cout << "Find:" << end1 - begin1 << endl;
}
int main()
{
TestRBTree1();
TestRBTree2();
return 0;
}
