目录
一、红黑树的概念
红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
二、红黑树的性质
- 每个结点不是红色就是黑色
- 根节点是黑色的
- 如果一个节点是红色的,则它的两个孩子结点是黑色的(没有连续的红色节点)
- 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点(每条路径黑色节点的数量相同)
- 每个叶子结点都是黑色的(此处的叶子结点指的是空结点——NIL节点)
假设每条路径黑节点的数量是N,则:
N <= 任意路径长度 <= 2N
最短路径:全黑
最长路径:一黑一红
最长路径不超过最短路径的2倍
AVL树:左右两边更均衡,高度更接近logN
满二多叉树:2^h - 1 = N
红黑树的两边没有那么均衡,整体高度:2 * logN
假设红黑树中一条路径黑色节点的数量是X,高度为h,N为节点数量
则:X <= h <= 2X
2^X - 1 <= N <= 2^2X - 1
全黑满二叉树 ~ 一黑一红满二叉树
AVL严格平衡,效率logN,红黑树是近似平衡,效率是2 * logN
而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)
, _col(RED)
{}
};
template <class K, class V>
struct RBTree
{
typedef RBTreeNode<K, V> Node;
public:
RBTree()
: _root(nullptr)
{}
private:
Node* _root;
};
四、红黑树插入操作
前半部分插入按照线索二叉树的插入原则,比他大,插在右边,小就插在左边
在插入操作,我们是插入红结点还是黑节点?——红节点,因为插入了黑节点可能会影响其他路径了,问题比较严重,所以我们选择插入红色节点,然后再去进行调整
如图若22为新插入节点,并且是黑色,那么绿色路径有3个黑色节点,橙色路径有两个黑色节点,影响会更大一些
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 (_root->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (_root->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(kv);
cur->_col = RED;
if (parent->_kv.first > kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
// 接下来控制平衡
}
在插入新节点后,要看红黑树性质是否被破坏
因为新节点的默认颜色是红色,因此:如果其双亲节点的颜色是黑色,没有违反红黑树任何性质,则不需要调整;但当新插入节点的双亲节点颜色为红色时,就违反了性质三不能有连在一起的红色节点,此时需要对红黑树分情况来讨论
约定:cur为当前节点,p为父节点,g为祖父节点,u为叔叔节点
4.1 cur为红,p为红,g为黑,u存在且为红
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 (_root->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (_root->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(kv);
cur->_col = RED;
if (parent->_kv.first > kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
// 接下来控制平衡
while (parent && parent->_col == RED)
{
Node* grandFather = parent->_parent;
if (parent == grandFather->_left)
{
Node* uncle = grandFather->_right;
if (uncle && uncle->_col == RED) // 叔叔存在的情况
{
parent->_col = uncle->_col = BLACK;
grandFather->_col = RED;
cur = grandFather;
parent = cur->_parent;
}
}
}
_root->_col = BLACK;
return true;
}
4.2 cur为红,p为红,g为黑,u不存在/u存在且为黑 (单旋情况)
u情况说明:
如果u节点不存在,则cur一定是新插入节点,因为如果cur不是新插入节点,则cur和p一定有一个节点的颜色是黑色,这样子就不满足性质4(每条路径黑色节点个数要相同)
当u节点存在且为黑色的时候,cur节点的原先颜色一定是黑色,现在是红色的原因是子树在调整过程中,将cur节点由黑色改为了红色
处理方法:
- 当左边高进行右旋,右边高进行左旋
- p和g进行变色,p变黑色、g变红色
4.2.1 左单旋
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node* parentParent = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (_root == parent)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
parentParent->_left = subR;
else
parentParent->_right = subR;
subR->_parent = parentParent;
}
}
4.2.2 右单旋
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* parentParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
parentParent->_left = subL;
else
parentParent->_right = subL;
subL->_parent = parentParent;
}
}
4.3 cur为红,p为红,g为黑,u不存在/u存在且为黑 (双旋情况)
综上所述 我们代码可以进行完善
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 (_root->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (_root->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
// 找到该位置后进行插入
cur = new Node(kv);
cur->_col = RED;
if (parent->_kv.first > kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
// 接下来控制平衡
// 只有插入的节点和双亲节点都是红色才需要去进行处理
while (parent && parent->_col == RED)
{
Node* grandFather = parent->_parent;
if (parent == grandFather->_left) // parent在左侧的情况
{
Node* uncle = grandFather->_right;
// 情况1:叔叔存在且为红
if (uncle && uncle->_col == RED)
{
parent->_col = uncle->_col = BLACK;
grandFather->_col = RED;
cur = grandFather;
parent = cur->_parent;
}
// 情况2:叔叔不存在/叔叔存在且为黑
else
{
/*
单旋
g
p
c
双旋
g
p
c
*/
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 // if (parent == grandFather->_left) // parent在右侧的情况
{
Node* uncle = grandFather->_left;
if (uncle && uncle->_col == RED)
{
// 变色+向上处理
parent->_col = uncle->_col = BLACK;
grandFather->_col = RED;
cur = grandFather;
parent = cur->_parent;
}
else
{
/*
单旋
g
p
c
双旋
g
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;
}
五、补充
5.1 求红黑树高度
int Height()
{
return _Height(_root);
}
int _Height(Node* root)
{
if (root == NULL)
{
return 0;
}
int leftHight = _Height(root->_left);
int rightHight = _Height(root->_right);
return leftHight > rightHight ? leftHight + 1 : rightHight + 1;
}
5.2 判断一棵树是否为红黑树
bool IsBalance()
{
if (_root && _root->_col == RED)
{
cout << "根节点不是黑色" << endl;
return false;
}
int banchmark = 0; // 基准值—计算一边的黑色节点数目
Node* left = _root;
while (left)
{
if (left->_col == BLACK)
++banchmark;
left = left->_left;
}
int blackNum = 0;
return _IsBalance(_root, banchmark, blackNum);
}
bool _IsBalance(Node* root, int banchmark, int blackNum)
{
if (root == nullptr)
{
if (banchmark != blackNum)
{
cout << "黑色节点数量不相同" << endl;
return false;
}
return true;
}
if (root->_col == RED && root->_parent->_col == RED)
{
cout << "存在连续的红色节点" << endl;
return false;
}
if (root->_col == BLACK)
blackNum++;
return _IsBalance(root->_left, banchmark, blackNum)
&& _IsBalance(root->_right, banchmark, blackNum);
}
六、红黑树与AVL数的比较
红黑树和AVL树都是高效的平衡二叉树,增删改查的时间复杂度都是O(logN),红黑树不追求绝对平衡,其只需保证最长路径不超过最短路径的2倍,相对而言,降低了插入和旋转的次数,所以在经常进行增删的结构中性能比AVL树更优,而且红黑树实现比较简单,所以实际运用中红黑树更多
红黑树实际中应用有:
- map/set 、 multi_map/multi_set
- java库
- linux内核
- 其他库
七、完整代码
#pragma once
#include <iostream>
#include <string>
#include <ctime>
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)
, _col(RED)
, _kv(kv)
{}
};
template <class K, class V>
struct RBTree
{
typedef RBTreeNode<K, V> Node;
public:
RBTree()
: _root(nullptr)
{}
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 (_root->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (_root->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
// 找到该位置后进行插入
cur = new Node(kv);
cur->_col = RED;
if (parent->_kv.first > kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
// 接下来控制平衡
// 只有插入的节点和双亲节点都是红色才需要去进行处理
while (parent && parent->_col == RED)
{
Node* grandFather = parent->_parent;
if (parent == grandFather->_left) // parent在左侧的情况
{
Node* uncle = grandFather->_right;
// 情况1:叔叔存在且为红
if (uncle && uncle->_col == RED)
{
parent->_col = uncle->_col = BLACK;
grandFather->_col = RED;
cur = grandFather;
parent = cur->_parent;
}
// 情况2:叔叔不存在/叔叔存在且为黑
else
{
/*
单旋
g
p
c
双旋
g
p
c
*/
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 // if (parent == grandFather->_left) // parent在右侧的情况
{
Node* uncle = grandFather->_left;
if (uncle && uncle->_col == RED)
{
// 变色+向上处理
parent->_col = uncle->_col = BLACK;
grandFather->_col = RED;
cur = grandFather;
parent = cur->_parent;
}
else
{
/*
单旋
g
p
c
双旋
g
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;
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node* parentParent = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (_root == parent)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
parentParent->_left = subR;
else
parentParent->_right = subR;
subR->_parent = parentParent;
}
}
void RotateR(Node * parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* parentParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
parentParent->_left = subL;
else
parentParent->_right = subL;
subL->_parent = parentParent;
}
}
void InOrder()
{
_InOrder(_root);
}
void _InOrder(Node* root)
{
if (root == NULL)
return;
_InOrder(root->_left);
cout << root->_kv.first << " -> " << root->_kv.second << endl;
_InOrder(root->_right);
}
int Height()
{
return _Height(_root);
}
int _Height(Node* root)
{
if (root == NULL)
{
return 0;
}
int leftHight = _Height(root->_left);
int rightHight = _Height(root->_right);
return leftHight > rightHight ? leftHight + 1 : rightHight + 1;
}
// 判断是否为红黑树(防止只是单纯的二叉搜索树)
bool IsBalance()
{
if (_root && _root->_col == RED)
{
cout << "根节点不是黑色" << endl;
return false;
}
int banchmark = 0; // 基准值—计算一边的黑色节点数目
Node* left = _root;
while (left)
{
if (left->_col == BLACK)
++banchmark;
left = left->_left;
}
int blackNum = 0;
return _IsBalance(_root, banchmark, blackNum);
}
bool _IsBalance(Node* root, int banchmark, int blackNum)
{
if (root == nullptr)
{
if (banchmark != blackNum)
{
cout << "黑色节点数量不相同" << endl;
return false;
}
return true;
}
if (root->_col == RED && root->_parent->_col == RED)
{
cout << "存在连续的红色节点" << endl;
return false;
}
if (root->_col == BLACK)
blackNum++;
return _IsBalance(root->_left, banchmark, blackNum)
&& _IsBalance(root->_right, banchmark, blackNum);
}
private:
Node* _root;
};
void TestRBTree()
{
RBTree<int, int> t;
//int a[] = {5,4,3,2,1,0};
//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(make_pair(e, e));
/*if (!t.IsBalance())
{
cout << "Insert" << e << endl;
}*/
}
t.InOrder();
cout << t.IsBalance() << endl;
cout << "高度:" << t.Height() << endl;
}