@[TOC]红黑树
一 红黑树概念
红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
简单来说红黑树通过了对颜色的控制进而对树的长度做出了限制,不再需要平衡因子。
红黑树性质
- 每个节点不是红色就是黑色
- 根节点为黑色
- 如果一个节点是红色,那么他的孩子节点就是黑色。没有连续的红色节点
- 对于每个节点,他的所以路径上的黑色节点的数量是相同的。
- 每个叶子节点都是黑色的(注意:这里的叶子节点指的是空节点)
红黑树通过这些性质保证了他的高度是合法的。
红黑树保证的是:最长路径不超过最短路径的两倍
二 红黑树的实现
2.1 红黑树的结构
enum Color
{Red,Black,
};template<class K, class V>
struct RBTreeNode
{RBTreeNode<K, V>* _left;RBTreeNode<K, V>* _right;RBTreeNode<K, V>* _parent;Color _color;pair<K, V> _kv;RBTreeNode(const pair<K, V>& kv):_left(nullptr), _right(nullptr), _parent(nullptr), _color(Red), _kv(kv){}
};
template<class K,class V>
class RBtree
{typedef RBTreeNode<K, V> Node;private:Node* _node=nullptr;
}
2.2 红黑树的插入
我们要注意:我们在插入新节点的时候,默认插入元素的颜色为红色,(黑色节点不好控制,因为要保证全部的路径的黑色节点数量是相同的,插入了一个黑色节点,就不能保证这一原则了)
然后插入元素对整棵树的影响我们就要从局部开始看,
- 如果插入元素的父亲为黑色那就不用在变化了;
- 插入元素的父亲为红色,此时就出翔了连续的红色节点,我们就要对这颗树进行处理;
我们对第二种情况分类讨论(用上述的图片为例)
第一种:cur为红,p为红,g为黑,u存在且为红
把p和u变为黑色,再把g变成红色;
到这里就要继续分类讨论:
- g为根节点,那就把g再变成黑色。
- 如果不是根节点,就把g=c,向上继续处理
注意(p/u是g的左还是右都不影响,同样cur是p的左还是右也不影响)
第二种: cur为红,p为红,g为黑,u不存在/u存在且为黑
(i)u不存在
这里cur一定是新插入的节点,如果cur不是新插入的节点,cur和p一定有一个是黑色的。
(ii)u存在且为黑色
这里的cur就不是新插入的节点,u是黑色那么,每个路径下黑色节点数量一定相同,所以cur原本一定是黑色的,是因为新插入的原因被变成了红色。
以上这两个情况本质上是一种。
这里就要用到旋转,旋转和前面AVL树的旋转一模一样
p为g的左孩子,cur为p的左孩子,则进行右单旋转;相反,
p为g的右孩子,cur为p的右孩子,则进行左单旋转
旋转完:p、g变色–p变黑,g变红。
参考下面的图,进一步理解
第三种 cur为红,p为红,g为黑,u不存在/u存在且为黑,但这里的左右位置不同
(i) p为g的左,cur为p的右
可以看到这里就变回了情况二,根据上面的我们再进行右单旋即可。
(i) p为g的右,cur为p的左
这个就是先右旋+左旋即可。这个图留个大家去完成。
2.3代码
bool Insert(const pair<K, V>& kv){if (_root == nullptr){_root = new Node(kv);_root->_color = 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);//红色if (parent->_kv.first < kv.first){parent->_right = cur;}else{parent->_left = cur;}cur->_parent = parent;while (parent && parent->_color == Red){Node* grandfather = parent->_parent;if (parent == grandfather->_left){Node* uncle = grandfather->_right;//情况一:存在且为红if (uncle && uncle->_color == Red){//变色parent->_color = Black;uncle->_color = Black;grandfather->_color = Red;//向上处理cur = grandfather;parent = cur->_parent;}//情况二:不存在or存在且为黑else{//旋转+变色// g// p u//cif (cur == parent->_left){RotateR(grandfather);parent->_color = Black;grandfather->_color = Red;}else{//情况三// g// p u// c RotateL(parent);RotateR(grandfather);cur->_color = Black;grandfather->_color = Red;}break;}}else//p是g的右孩子{Node* uncle = grandfather->_left;//情况一:存在且为红if (uncle && uncle->_color == Red){//变色parent->_color = Black;uncle->_color = Black;grandfather->_color = Red;//向上处理cur = grandfather;parent = cur->_parent;}//情况二:不存在or存在且为黑else{//旋转+变色// g// u p// cif (cur == parent->_right){RotateL(grandfather);parent->_color = Black;grandfather->_color = Red;}else{//情况三// g// u p// cRotateR(parent);RotateL(grandfather);cur->_color = Black;grandfather->_color = Red;}break;}}}_root->_color = Black;return true;}
三 检查函数
bool Check(Node* cur, int BlackNum, int refBlackNum){if (cur == nullptr){if (BlackNum != refBlackNum){cout << "黑色节点的数量不匹配" << endl;return false;}elsereturn true;}if (cur->_color == Red && cur->_parent->_color == Red){cout << "出现了连续的红色节点" << endl;return false;}if (cur->_color == Black)++BlackNum;return Check(cur->_left, BlackNum, refBlackNum) &&Check(cur->_right, BlackNum, refBlackNum);}bool IsBalance(){if (_root == nullptr || _root->_color == Red)return false;//给一个基准值,和其他的黑色节点去比较int refBlackNum = 0;Node* cur = _root;while (cur){if (cur->_color == Black)++refBlackNum;cur = cur->_left;}return Check(_root, 0, refBlackNum);}
四 总结
总的来说红黑树的实现和AVL树非常像,他们就是兄弟,红黑树就是把考虑的因素从平衡因子转变成了颜色。
五 完整代码
#pragma once
#include<assert.h>
#include<vector>
enum Color
{Red,Black,
};template<class K, class V>
struct RBTreeNode
{RBTreeNode<K, V>* _left;RBTreeNode<K, V>* _right;RBTreeNode<K, V>* _parent;Color _color;pair<K, V> _kv;RBTreeNode(const pair<K, V>& kv):_left(nullptr), _right(nullptr), _parent(nullptr), _color(Red), _kv(kv){}
};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->_color = 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);//红色if (parent->_kv.first < kv.first){parent->_right = cur;}else{parent->_left = cur;}cur->_parent = parent;while (parent && parent->_color == Red){Node* grandfather = parent->_parent;if (parent == grandfather->_left){Node* uncle = grandfather->_right;//情况一:存在且为红if (uncle && uncle->_color == Red){//变色parent->_color = Black;uncle->_color = Black;grandfather->_color = Red;//向上处理cur = grandfather;parent = cur->_parent;}//情况二:不存在or存在且为黑else{//旋转+变色// g// p u//cif (cur == parent->_left){RotateR(grandfather);parent->_color = Black;grandfather->_color = Red;}else{//情况三// g// p u// c RotateL(parent);RotateR(grandfather);cur->_color = Black;grandfather->_color = Red;}break;}}else//p是g的右孩子{Node* uncle = grandfather->_left;//情况一:存在且为红if (uncle && uncle->_color == Red){//变色parent->_color = Black;uncle->_color = Black;grandfather->_color = Red;//向上处理cur = grandfather;parent = cur->_parent;}//情况二:不存在or存在且为黑else{//旋转+变色// g// u p// cif (cur == parent->_right){RotateL(grandfather);parent->_color = Black;grandfather->_color = Red;}else{//情况三// g// u p// cRotateR(parent);RotateL(grandfather);cur->_color = Black;grandfather->_color = Red;}break;}}}_root->_color = Black;return true;}void RotateL(Node* parent){//++rotateSize;Node* subR = parent->_right;Node* subRL = subR->_left;parent->_right = subRL;if (subRL)subRL->_parent = parent;subR->_left = parent;Node* ppnode = parent->_parent;parent->_parent = subR;if (parent == _root){_root = subR;subR->_parent = nullptr;}else{if (ppnode->_left == parent){ppnode->_left = subR;}else{ppnode->_right = subR;}subR->_parent = ppnode;}}void RotateR(Node* parent){//++rotateSize;Node* subL = parent->_left;Node* subLR = subL->_right;parent->_left = subLR;if (subLR)subLR->_parent = parent;subL->_right = parent;Node* ppnode = parent->_parent;parent->_parent = subL;if (parent == _root){_root = subL;subL->_parent = nullptr;}else{if (ppnode->_left == parent){ppnode->_left = subL;}else{ppnode->_right = subL;}subL->_parent = ppnode;}}void _InOrder(Node* root){if (root == nullptr)return;_InOrder(root->_left);cout << root->_kv.first << endl;_InOrder(root->_right);}void InOrder(){_InOrder(_root);}/////检查函数bool Check(Node* cur, int BlackNum, int refBlackNum){if (cur == nullptr){if (BlackNum != refBlackNum){cout << "黑色节点的数量不匹配" << endl;return false;}elsereturn true;}if (cur->_color == Red && cur->_parent->_color == Red){cout << "出现了连续的红色节点" << endl;return false;}if (cur->_color == Black)++BlackNum;return Check(cur->_left, BlackNum, refBlackNum) &&Check(cur->_right, BlackNum, refBlackNum);}bool IsBalance(){if (_root == nullptr || _root->_color == Red)return false;//给一个基准值,和其他的黑色节点去比较int refBlackNum = 0;Node* cur = _root;while (cur){if (cur->_color == Black)++refBlackNum;cur = cur->_left;}return Check(_root, 0, refBlackNum);}
private:Node* _root = nullptr;
};void TestRBTree1()
{//int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };int a[] = { 4, 2, 6, 1, 14,16};RBTree<int, int> t;for (auto e : a){if (e == 16){int x = 0;}t.Insert(make_pair(e, e));// 1、先看是插入谁导致出现的问题// 2、打条件断点,画出插入前的树// 3、单步跟踪,对比图一一分析细节原因cout << e << "->" << t.IsBalance() << endl;}t.InOrder();cout << t.IsBalance() << endl;
}