下面开始本章红黑树的内容~
红黑树,当然也是一种二叉搜索树,我们可以通过中序遍历可以得到一个有序序列;
不同的是每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。

这个时候友友们就很疑惑,下面说红黑树的性质:
通过上面规则,红黑树就能保证:其最长路径中节点个数不会超过最短路径节点个数的两倍。(最坏情况最短路径全黑,最长一半黑一半红)
// 节点的颜色
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)
{}
};
为了方便了解红黑树,我把它写死了;就是pair类型。
if (_root == nullptr)
{
_root = new Node(kv);
_root->_col = BLACK;
return true;
}
//找find
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;
注意:我们插入节点为红色便于调整。
因为新节点的默认颜色是红色,因此:如果其双亲节点的颜色是黑色,没有违反红黑树任何
性质,则不需要调整;但当新插入节点的双亲节点颜色为红色时,就违反了性质三不能有连在一起的红色节点;
此时需要对红黑树分情况来讨论:约定:cur为当前节点,p为父节点,g为祖父节点,u为叔叔节点。
注意:旋转上一篇AVL树旋转已经详细说明了,此章不做补充了。
情况一: cur为红,p为红,g为黑,u存在且为红

情况二: cur为红,p为红,g为黑,u不存在/u存在且为黑

情况三: cur为红,p为红,g为黑,u不存在/u存在且为黑

代码里面也有详细注释供uu们分析~
bool Insert(const pair<K, V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
_root->_col = BLACK;
return true;
}
//找find
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;
assert(grandfather);
assert(grandfather->_col == BLACK);
//关键看叔叔
// g
// p or p
//
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
//情况一: uncle存在且为红,变色+继续往上调整
// g
// p u
// c
if (uncle && uncle->_col==RED)
{
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else //uncle || uncle->_col == BLACK
{
//情况二、三: uncle存在且为黑或者不存在,变色+旋转
if (cur == parent->_left)
{
// g p
// p u c g
// c u
//(g)右单旋+变色
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g g c
// p u c u p g
// c p u
//(p)左单旋+(g)右单旋+ 变色
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else //parent == grandfather->_right
{
Node* uncle = grandfather->_left;
if (uncle && uncle->_col == RED)
{
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else //uncle || uncle->_col == BLACK
{
//情况二、三: uncle存在且为黑或者不存在,变色+旋转
if (cur == parent->_right)
{
//(g)左单旋+变色
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
//(p)右单旋+(g)左单旋+ 变色
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return true;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool IsBalance()
{
// 空树也是红黑树
if (_root == nullptr)
{
return true;
}
// 检测根节点是否满足情况
if (_root->_col == RED)
{
return false;
}
// 获取任意一条路径中黑色节点的个数
//黑色节点数量的基准
int benchmark = 0;
return PrecCheck(_root, 0, benchmark);
}
bool PrecCheck(Node* root, int blackNum, int& benchmark)
{
if (root == nullptr)
{
if (benchmark == 0)
{
benchmark = blackNum;
return true;
}
if (benchmark != blackNum)
{
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 PrecCheck(root->_left, blackNum, benchmark)
&& PrecCheck(root->_right, blackNum, benchmark);
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.second << " ";
_InOrder(root->_right);
}
// .h文件
#pragma once
#include
#include
using namespace std;
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)
{}
};
template<class K, class V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
public:
RBTree()
{}
bool Insert(const pair<K, V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
_root->_col = BLACK;
return true;
}
//找find
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;
assert(grandfather);
assert(grandfather->_col == BLACK);
//关键看叔叔
// g
// p or p
//
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
//情况一: uncle存在且为红,变色+继续往上调整
// g
// p u
// c
if (uncle && uncle->_col==RED)
{
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else //uncle || uncle->_col == BLACK
{
//情况二、三: uncle存在且为黑或者不存在,变色+旋转
if (cur == parent->_left)
{
// g p
// p u c g
// c u
//(g)右单旋+变色
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g g c
// p u c u p g
// c p u
//(p)左单旋+(g)右单旋+ 变色
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else //parent == grandfather->_right
{
Node* uncle = grandfather->_left;
if (uncle && uncle->_col == RED)
{
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else //uncle || uncle->_col == BLACK
{
//情况二、三: uncle存在且为黑或者不存在,变色+旋转
if (cur == parent->_right)
{
//(g)左单旋+变色
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
//(p)右单旋+(g)左单旋+ 变色
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return true;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool IsBalance()
{
if (_root == nullptr)
{
return true;
}
if (_root->_col == RED)
{
return false;
}
//黑色节点数量的基准
int benchmark = 0;
return PrecCheck(_root, 0, benchmark);
}
private:
bool PrecCheck(Node* root, int blackNum, int& benchmark)
{
if (root == nullptr)
{
if (benchmark == 0)
{
benchmark = blackNum;
return true;
}
if (benchmark != blackNum)
{
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 PrecCheck(root->_left, blackNum, benchmark)
&& PrecCheck(root->_right, blackNum, benchmark);
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.second << " ";
_InOrder(root->_right);
}
void RotateR(Node* parent) //右旋
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
Node* ppNode = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (_root == parent)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
}
}
void RotateL(Node* parent) //左旋
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node* ppNode = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
_root->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subR;
}
else //ppNode->_right == parent
{
ppNode->_right = subR;
}
subR->_parent = ppNode;
}
}
Node* _root = nullptr;
};
void TestRBTree1()
{
//int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
//int a[] = { 1,2,3,4,5,6,7,8,9 };
RBTree<int, int> t;
for (auto e : a)
{
t.Insert(make_pair(e, e));
}
t.InOrder();
if (t.IsBalance())
cout << "ture" << endl;
else
cout << "false" << endl;
}
void TestRBTree()
{
size_t N = 1000;
srand(time(0));
RBTree<int, int> t1;
for (size_t i = 0; i < N; i++)
{
int x = rand();
t1.Insert(make_pair(x, i));
}
cout << "IsBalance:" << t1.IsBalance() << endl;
}
/.cpp文件
#include"BR_TREE.h"
int main()
{
TestRBTree1();
TestRBTree();
return 0;
}
