概念
红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍(即最长路径不超过最短路径的两倍,近似平衡),因而是接近平衡的。
注意:红黑树叶子节点必须为空节点,即NIL节点
性质
- 每个结点不是红色就是黑色
- 根节点是黑色的
- 如果一个节点是红色的,则它的两个孩子结点必须是黑色的
(即任何路径没有连续的红色结点)
- 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点
(每条路径上的黑色结点个数相同,这里的路径包括空结点)
- 每个叶子结点都是黑色的
(此处的叶子结点指的是空结点,已经不在是传统的叶子结点了,图中的NIL结点就是空结点)
补:按照性质5,概念中的例图有11条路径。
小测试:下面的书是平衡树吗?
答案:不是,请自己根据性质判断为什么不是。
AVL树与红黑树比较
查找
插入
红黑树插入时不需要旋转
插入
enum Colour
{
RED,
BLACK
};
template <class K, class V>
struct RBTreeNode
{
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
pair<K, V> _kv;
Colour _col;
RBTreeNode(const pair<K, V>& kv)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _kv(kv)
//插入红色结点,违反规则3,只影响一条路径,甚至可能不影响。
//如果插入黑色结点,违反规则4,影响所有路径
,_col(RED)
{}
};
template <class K, class V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
private:
Node* _root = nullptr;
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)
{
parent = cur;
if (cur->_kv.first < kv.first)
{
cur = cur->_right;
}
else if (cur->_kv.first > kv.first)
{
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(kv);
if (parent->_kv.first > cur->_kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
//颜色处理
}
};
颜色处理
不需要处理型
新节点的父节点为黑色
完整代码
#pragma once
#include <utility>
#include <assert.h>
#include <algorithm>
using namespace std;
/*>1. 每个结点不是红色就是黑色
>2. 根节点是黑色的
>3. 如果一个节点是红色的,则它的两个孩子结点必须是黑色的
>4. 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点`(每条路径上的黑色结点个数相同,这里的路径包括空结点)`
>5. 每个叶子结点都是黑色的
*/
enum Colour
{
RED,
BLACK
};
template <class K, class V>
struct RBTreeNode
{
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
pair<K, V> _kv;
Colour _col;
RBTreeNode(const pair<K, V>& kv)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _kv(kv)
//插入红色结点,违反规则3,只影响一条路径,甚至可能不影响。
//如果插入黑色结点,违反规则4,影响所有路径
,_col(RED)
{}
};
template <class K, class V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
private:
Node* _root = nullptr;
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)
{
parent = cur;
if (cur->_kv.first < kv.first)
{
cur = cur->_right;
}
else if (cur->_kv.first > kv.first)
{
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(kv);//默认红色
if (parent->_kv.first > cur->_kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
//颜色处理
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
assert(grandfather);
assert(grandfather->_col == BLACK);
//看叔叔
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
//情况一:uncle存在且为RED
if (uncle && uncle->_col == RED)
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
//继续往上处理
cur = grandfather;
parent = cur->_parent;
}
else//情况二/三:uncle不存在 或 存在且为BLACK
{
if (cur == parent->_left) //情况二:右单旋+变色
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else//情况三:左右旋+变色
{
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else //parent == grandfather->_right
{
Node* uncle = grandfather->_left;
//情况一:uncle存在且为RED
if (uncle && uncle->_col == RED)
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
//继续往上处理
cur = grandfather;
parent = cur->_parent;
}
else//情况二/三:uncle不存在 或 存在且为BLACK
{
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;
}
void InOrder()
{
_InOrder(_root);
return;
}
bool IsBalance()
{
if (_root == nullptr)
return true;
if (_root->_col == RED)
{
cout << "根节点不是黑色" << endl;
return false;
}
//黑色节点数量基准值
int benchmark = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLACK)
++benchmark;
cur = cur->_left;
}
return PrevCheck(_root, 0, benchmark);
}
private:
void RotateL(Node* parent)
{
Node* cur = parent->_right;
Node* ocl = cur->_left;
Node* op = parent;
Node* oc = cur;
Node* opp = parent->_parent;
if (opp == nullptr)
{
_root = oc;
}
else
{
if (opp->_left == op)
{
opp->_left = oc;
}
else
{
opp->_right = oc;
}
}
if (ocl != nullptr)
{
ocl->_parent = op;
}
op->_right = ocl;
op->_parent = oc;
oc->_left = op;
oc->_parent = opp;
}
void RotateR(Node* parent)
{
Node* cur = parent->_left;
Node* ocr = cur->_right;
Node* op = parent;
Node* oc = cur;
Node* opp = parent->_parent;
if (opp == nullptr)
{
_root = oc;
}
else
{
if (opp->_left == op)
{
opp->_left = oc;
}
else
{
opp->_right = oc;
}
}
if (ocr != nullptr)
{
ocr->_parent = op;
}
oc->_right = op;
oc->_parent = opp;
op->_left = ocr;
op->_parent = oc;
}
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_kv.first << ":" << root->_kv.second << endl;
_InOrder(root->_right);
return;
}
bool PrevCheck(Node* root, int blackNum, int& benchmark)
{
if (root == nullptr)
{
if (benchmark == 0)
{
benchmark = blackNum;
return true;
}
if (blackNum != benchmark)
{
cout << " 某条路径黑色节点数量不相等" << endl;
return false;
}
else
return true;
}
if (root->_col == BLACK)
{
++blackNum;
}
if (root->_col == RED && root->_parent->_col == RED)
{
cout << "存在连续的红结点" << endl;
return false;
}
return PrevCheck(root->_left, blackNum, benchmark)
&& PrevCheck(root->_right, blackNum, benchmark);
}
};
void testRBTree1()
{
int a[] = { 1,8,9,58,74,36,21,30,45,8,87,1,2,36,4,5,6,7,85,69,20,10,12 };
int b[] = { 1,2,3,4,5,6,7,8,9,10,11,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75 };
int c[] = { 16,3,7,11,9,26,18,14,15 };
int e[] = { 4,2,6,1,3,5,15,7,16,14 };//特殊测试 双旋测试
RBTree<int, int> t1;
for (auto& e : e)
{
t1.Insert(make_pair(e, e + 1));
}
t1.InOrder();//中序遍历只能证明是搜索树
cout << "IsBalance:" << t1.IsBalance() << endl;//平衡验证
}
void testRBTree2()//随机值测试
{
size_t N = 100000;
srand(time(0));
RBTree<int, int> t1;
for (int i = 0; i < N; i++)
{
int x = rand();//C语言的随机数数量量有限制
t1.Insert(make_pair(i, x));
}
t1.InOrder();
cout << "IsBalance:" << t1.IsBalance() << endl;
}