数据结构~红黑树
文章目录
- 一、红黑树的概念
- 二、红黑树的定义
- 三、红黑树的插入
- 四、红黑树的平衡
- 五、红黑树的验证
- 六、红黑树的删除
- 七、完整代码
- 八、总结
一、红黑树的概念
红黑树是一棵二叉搜索树,他的每个结点增加⼀个存储位来表示结点的颜色,可以是红色或者黑色。通过对任何⼀条从根到叶子的路径上各个结点的颜色进行约束,红黑树确保没有⼀条路径会比其他路径长出2倍,因而是接近平衡的。
红黑树的规则:
1.每个结点不是红色就是黑色
2. 根结点是黑色的
3. 如果一个结点是红色的,则它的两个孩子结点必须是黑色的,也就是说任意⼀条路径不会有连续的红色结点。
4. 对于任意⼀个结点,从该结点到其所有NULL结点的简单路径上,均包含相同数量的黑色结点
说明:《算法导论》等书籍上补充了⼀条每个叶⼦结点(NIL)都是黑色的规则。他这里所指的叶子结点不是传统的意义上的叶子结点,而是我们说的空结点,有些书籍上也把NIL叫做外部结点。NIL是为了方便准确的标识出所有路径,《算法导论》在后续讲解实现的细节中也忽略了NIL结点,所以知道一下这个概念即可。
红黑树如何确保最长路径不超过最短路径的2倍的
- 由规则4可知,从根到NULL结点的每条路径都有相同数量的黑色结点,所以极端场景下,最短路径就就是全是黑色结点的路径,假设最短路径长度为bh(black height)。
- 由规则2和规则3可知,任意⼀条路径不会有连续的红色结点,所以极端场景下,最长的路径就是一黑一红间隔组成,那么最长路径的长度为2*bh。
- 综合红黑树的4点规则而言,理论上的全黑最短路径和⼀黑⼀红的最长路径并不是在每棵红黑树都存在的。假设任意⼀条从根到NULL结点路径的长度为x,那么bh<=h<=2*bh。
红黑树的效率
假设N是红黑树树中结点数量,h最短路径的长度,那么,由此推出,也就是意味着红黑树增删查改最坏也就是走最走路径 ,那么时间复杂度还是
O(logN)。
红黑树的表达相对AVL树要抽象⼀些,AVL树通过高度差直观的控制了平衡。红黑树通过4条规则的颜色约束,间接的实现了近似平衡,他们效率都是同⼀档次,但是相对而言,插入相同数量的结点,红黑树的旋转次数是更少的,因为他对平衡的控制没那么严格。
二、红黑树的定义
// 枚举值表⽰颜⾊
enum Colour
{RED,BLACK
};
// 这⾥我们默认按key/value结构实现
template<class K, class V>
struct RBTreeNode
{// 这⾥更新控制平衡也要加⼊parent指针 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:
private:Node* _root = nullptr;
};
一、整体结构
实现了一个红黑树的数据结构。红黑树是一种自平衡的二叉搜索树,它在插入和删除节点时通过旋转和重新着色操作来保持树的平衡,从而保证了高效的插入、删除和查找操作。
二、枚举类型Colour
定义了一个枚举类型Colour,用于表示节点的颜色,只有两种可能的值:RED和BLACK。
三、结构体RBTreeNode
成员变量:
pair<K, V> _kv:存储键值对,代表节点的数据。
RBTreeNode<K, V>* _left:指向左子节点的指针。
RBTreeNode<K, V>* _right:指向右子节点的指针。
RBTreeNode<K, V>* _parent:指向父节点的指针,用于在树中进行遍历和维护树的结构。
Colour _col:表示节点的颜色,为枚举类型Colour的值。
构造函数:接受一个pair<K, V>类型的参数,用于初始化节点的键值对,并将左右子节点和父节点指针初始化为nullptr。
四、类RBTree
成员变量:
Node* _root = nullptr:指向红黑树的根节点的指针,初始化为nullptr,表示空树。
三、红黑树的插入
红黑树树插入一个值的大概过程
1.插入一个值按二叉搜索树规则进行插入,插入后我们只需要观察是否符合红黑树的4条规则。
2. 如果是空树插入,新增结点是黑色结点。如果是非空树插⼊,新增结点必须红色结点,因为非空树
插入,新增黑色结点就破坏了规则4,规则4是很难维护的。
3. 非空树插入后,新增结点必须红色结点,如果父亲结点是黑色的,则没有违反任何规则,插入结束
4. 非空树插入后,新增结点必须红色结点,如果父亲结点是红色的,则违反规则3。进⼀步分析,c是
红色,p为红,g必为黑,这三个颜色都固定了,关键的变化看u的情况,需要根据u分为以下几种
情况分别处理。
说明:下图中假设把新增结点标识为c(cur),c的父亲标识为p(parent),p的父亲标识为g(grandfather),p的兄弟标识为u(uncle)
插入代码
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 (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;if (parent == grandfather->_left){// g// p uNode* 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// cRotateR(grandfather);parent->_col = BLACK;grandfather->_col = RED;}else{// g// p u// cRotateL(parent);RotateR(grandfather);cur->_col = BLACK;grandfather->_col = RED;}break;}}else{// g// u pNode* uncle = grandfather->_left;// 叔叔存在且为红,-》变色即可if (uncle && uncle->_col == RED){parent->_col = uncle->_col = BLACK;grandfather->_col = RED;// 继续往上处理cur = grandfather;parent = cur->_parent;}else // 叔叔不存在,或者存在且为黑{// 情况二:叔叔不存在或者存在且为黑// 旋转+变色// g// u p// cif (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;}
四、红黑树的平衡
情况1:变色
c为红,p为红,g为黑,u存在且为红,则将p和u变黑,g变红。在把g当做新的c,继续往上更新。
分析:因为p和u都是红色,g是黑色,把p和u变黑,左边子树路径各增加⼀个黑色结点,g再变红,相当于保持g所在子树的黑色结点的数量不变,同时解决了c和p连续红色结点的问题,需要继续往上更新是因为,g是红色,如果g的父亲还是红色,那么就还需要继续处理;如果g的父亲是黑色,则处理结束了;如果g就是整棵树的根,再把g变回黑色。
情况1只变色,不旋转。所以无论c是p的左还是右,p是g的左还是右,都是上面的变色处理方式。
跟AVL树类似,上图展示了⼀种具体情况,但是实际中需要这样处理的有很多种情况。
• 图1将以上类似的处理进行了抽象表达,d/e/f代表每条路径拥有hb个黑色结点的子树,a/b代表每条路径拥有hb-1个黑色结点的根为红的子树,hb>=0。
• 图2/图3/图4,分别展示了hb= =0/hb= =1/hb= =2的具体情况组合分析,当hb等于2时,这里组合情况上百亿种,这些样例是帮助我们理解,不论情况多少种,多么复杂,处理方式⼀样的,变色再继续往上处理即可,所以我们只需要看抽象图即可。
情况2:单旋+变色
c为红,p为红,g为黑,u不存在或者u存在且为黑,u不存在,则c⼀定是新增结点,u存在且为黑,则c一定不是新增,c之前是黑色的,是在c的⼦树中插⼊,符合情况1,变色将c从黑色变成红色,更新上来的。
分析:p必须变黑,才能解决,连续红色结点的问题,u不存在或者是黑色的,这里单纯的变色无法解决问题,需要旋转+变色。
如果p是g的左,c是p的左,那么以g为旋转点进行右单旋,再把p变黑,g变红即可。p变成课这颗树新的根,这样子树黑色结点的数量不变,没有连续的红色结点了,且不需要往上更新,因为p的父亲是黑色还是红色或者空都不违反规则。
如果p是g的右,c是p的右,那么以g为旋转点进行左单旋,再把p变黑,g变红即可。p变成课这颗树新的根,这样子树黑色结点的数量不变,没有连续的红色结点了,且不需要往上更新,因为p的父亲是黑色还是红色或者空都不违反规则。
情况3:单旋+变色
c为红,p为红,g为黑,u不存在或者u存在且为黑,u不存在,则c⼀定是新增结点,u存在且为黑,则
c⼀定不是新增,c之前是黑色的,是在c的⼦树中插⼊,符合情况1,变色将c从黑色变成红色,更新上
来的。
分析:p必须变黑,才能解决,连续红色结点的问题,u不存在或者是黑色的,这里单纯的变色无法解
决问题,需要旋转+变色。
如果p是g的左,c是p的右,那么先以p为旋转点进行左单旋,再以g为旋转点进行右单旋,再把c变
黑,g变红即可。c变成课这颗树新的根,这样子树黑色结点的数量不变,没有连续的红色结点了,且
不需要往上更新,因为c的父亲是黑色还是红色或者空都不违反规则。
如果p是g的右,c是p的左,那么先以p为旋转点进行右单旋,再以g为旋转点进行左单旋,再把c变
黑,g变红即可。c变成课这颗树新的根,这样子树黑色结点的数量不变,没有连续的红色结点了,且
不需要往上更新,因为c的父亲是黑色还是红色或者空都不违反规则。
五、红黑树的验证
这里获取最长路径和最短路径,检查最长路径不超过最短路径的2倍是不可行的,因为就算满足这个条
件,红黑树也可能颜色不满足规则,当前暂时没出问题,后续继续插入还是会出问题的。所以我们还
是去检查4点规则,满足这4点规则,⼀定能保证最长路径不超过最短路径的2倍。
- 规则1枚举颜色类型,天然实现保证了颜色不是黑色就是红色。
- 规则2直接检查根即可
- 规则3前序遍历检查,遇到红色结点查孩子不太方便,因为孩子有两个,且不一定存在,反过来检
查父亲的颜色就方便多了。 - 规则4前序遍历,遍历过程中用形参记录跟到当前结点的blackNum(黑色结点数量),前序遍历遇到
黑色结点就++blackNum,走到空就计算出了⼀条路径的黑色结点数量。再任意⼀条路径黑色结点
数量作为参考值,依次比较即可。
验证代码
bool Check(Node* root, int blackNum, const int refNum)
{if (root == nullptr){// 前序遍历⾛到空时,意味着⼀条路径⾛完了 //cout << blackNum << endl;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);
}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);
}
六、红黑树的删除
红黑树删除操作按照以下步骤进行:
-
首先,从根节点开始,按照二叉搜索树的规则找到要删除的节点。
-
如果找到了要删除的节点,根据红黑树的规则进行删除操作。首先,考虑要删除的节点是否有子节点。
-
如果要删除的节点没有子节点,直接删除即可。
-
如果要删除的节点只有一个子节点,将该子节点替代要删除的节点的位置。
-
如果要删除的节点有两个子节点,可以选择以下两种方法来替代要删除的节点:
-
找到要删除的节点的中序后继节点(即比要删除的节点值大的最小节点),将其值复制到要删除的节点,然后删除该中序后继节点。
-
找到要删除的节点的中序前驱节点(即比要删除的节点值小的最大节点),将其值复制到要删除的节点,然后删除该中序前驱节点。
-
-
-
在删除节点后,需要对红黑树进行调整,以保持红黑树的性质。具体的调整步骤如下:
-
如果删除的节点是红色的,不会对红黑树的性质造成任何影响,直接删除即可。
-
如果删除的节点是黑色的,会破坏红黑树的性质。此时,需要对红黑树进行调整,以保持性质。
-
如果删除的节点有一个红色子节点,将子节点染成黑色即可。
-
如果删除的节点是根节点,并且没有子节点,不需要进行任何调整。
-
如果删除的节点是黑色的,并且有一个黑色子节点,此时需要进行进一步调整。可以根据删除节点的兄弟节点的颜色进行分类处理:
-
如果删除节点的兄弟节点是红色的,可以通过旋转操作将兄弟节点染为黑色,然后再进行其他调整。
-
如果删除节点的兄弟节点是黑色的,并且其两个子节点都是黑色的,可以通过染红兄弟节点并向上递归调整的方式解决问题。
-
如果删除节点的兄弟节点是黑色的,并且其左子节点是红色的,右子节点是黑色的,可以通过旋转和染色操作将问题转化为其他情况。
-
如果删除节点的兄弟节点是黑色的,并且其右子节点是红色的,可以通过旋转和染色操作将问题转化为其他情况。
-
-
-
-
删除操作完成后,需要更新红黑树的根节点。
代码实现
template<class K, class V>
class RBTree
{typedef RBTreeNode<K, V> Node;
public:// 删除节点void Delete(const K& key){Node* delNode = Find(key);if (delNode == nullptr)return;Node* replaceNode = nullptr;// 确定要替换删除节点的节点if (delNode->_left == nullptr || delNode->_right == nullptr)replaceNode = delNode;elsereplaceNode = GetSuccessor(delNode);Node* child = nullptr;if (replaceNode->_left!= nullptr)child = replaceNode->_left;elsechild = replaceNode->_right;// 更新孩子节点的父指针if (child!= nullptr)child->_parent = replaceNode->_parent;if (replaceNode->_parent == nullptr)_root = child;else if (replaceNode == replaceNode->_parent->_left)replaceNode->_parent->_left = child;elsereplaceNode->_parent->_right = child;bool delCol = delNode->_col;// 如果要删除的节点和替换节点不同,则进行值的替换if (delNode!= replaceNode){delNode->_kv = replaceNode->_kv;// 更新颜色delNode->_col = replaceNode->_col;}// 如果删除的是黑色节点,需要进行调整if (delCol == BLACK)DeleteFixup(child);}private:Node* _root = nullptr;// 查找节点Node* Find(const K& key){Node* cur = _root;while (cur!= nullptr){if (key < cur->_kv.first)cur = cur->_left;else if (key > cur->_kv.first)cur = cur->_right;elsereturn cur;}return nullptr;}// 获取后继节点Node* GetSuccessor(Node* node){Node* cur = node->_right;while (cur->_left!= nullptr){cur = cur->_left;}return cur;}// 删除调整void DeleteFixup(Node* node){Node* parent = nullptr;Node* brother = nullptr;while (node!= _root && node->_col == BLACK){if (node == node->_parent->_left){brother = node->_parent->_right;// 兄弟节点为红色if (brother->_col == RED){brother->_col = BLACK;node->_parent->_col = RED;LeftRotate(node->_parent);brother = node->_parent->_right;}// 兄弟节点的两个孩子都为黑色if (brother->_left == nullptr || brother->_left->_col == BLACK &&brother->_right == nullptr || brother->_right->_col == BLACK){brother->_col = RED;node = node->_parent;}else{// 兄弟节点的右孩子为黑色if (brother->_right == nullptr || brother->_right->_col == BLACK){if (brother->_left!= nullptr)brother->_left->_col = BLACK;brother->_col = RED;RightRotate(brother);brother = node->_parent->_right;}brother->_col = node->_parent->_col;node->_parent->_col = BLACK;if (brother->_right!= nullptr)brother->_right->_col = BLACK;LeftRotate(node->_parent);node = _root;}}else{brother = node->_parent->_left;// 兄弟节点为红色if (brother->_col == RED){brother->_col = BLACK;node->_parent->_col = RED;RightRotate(node->_parent);brother = node->_parent->_left;}// 兄弟节点的两个孩子都为黑色if (brother->_right == nullptr || brother->_right->_col == BLACK &&brother->_left == nullptr || brother->_left->_col == BLACK){brother->_col = RED;node = node->_parent;}else{// 兄弟节点的左孩子为黑色if (brother->_left == nullptr || brother->_left->_col == BLACK){if (brother->_right!= nullptr)brother->_right->_col = BLACK;brother->_col = RED;LeftRotate(brother);brother = node->_parent->_left;}brother->_col = node->_parent->_col;node->_parent->_col = BLACK;if (brother->_left!= nullptr)brother->_left->_col = BLACK;RightRotate(node->_parent);node = _root;}}}node->_col = BLACK;}// 左旋void LeftRotate(Node* parent){Node* child = parent->_right;parent->_right = child->_left;if (child->_left!= nullptr)child->_left->_parent = parent;child->_parent = parent->_parent;if (parent->_parent == nullptr)_root = child;else if (parent == parent->_parent->_left)parent->_parent->_left = child;elseparent->_parent->_right = child;child->_left = parent;parent->_parent = child;}// 右旋void RightRotate(Node* parent){Node* child = parent->_left;parent->_left = child->_right;if (child->_right!= nullptr)child->_right->_parent = parent;child->_parent = parent->_parent;if (parent->_parent == nullptr)_root = child;else if (parent == parent->_parent->_left)parent->_parent->_left = child;elseparent->_parent->_right = child;child->_right = parent;parent->_parent = child;}
};
七、完整代码
RBTree.h
enum Colour
{RED,BLACK
};template<class K, class V>
struct RBTreeNode
{// 这里更新控制平衡也要加入parent指针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: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 (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;if (parent == grandfather->_left){// g// p uNode* 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// cRotateR(grandfather);parent->_col = BLACK;grandfather->_col = RED;}else{// g// p u// cRotateL(parent);RotateR(grandfather);cur->_col = BLACK;grandfather->_col = RED;}break;}}else{// g// u pNode* uncle = grandfather->_left;// 叔叔存在且为红,-》变色即可if (uncle && uncle->_col == RED){parent->_col = uncle->_col = BLACK;grandfather->_col = RED;// 继续往上处理cur = grandfather;parent = cur->_parent;}else // 叔叔不存在,或者存在且为黑{// 情况二:叔叔不存在或者存在且为黑// 旋转+变色// g// u p// cif (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 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;subL->_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* parentParent = parent->_parent;subR->_left = parent;parent->_parent = subR;if (parentParent == nullptr){_root = subR;subR->_parent = nullptr;}else{if (parent == parentParent->_left){parentParent->_left = subR;}else{parentParent->_right = subR;}subR->_parent = parentParent;}}void InOrder(){_InOrder(_root);cout << endl;}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);}void Delete(const K& key){Node* delNode = Find(key);if (delNode == nullptr)return;Node* replaceNode = nullptr;// 确定要替换删除节点的节点if (delNode->_left == nullptr || delNode->_right == nullptr)replaceNode = delNode;elsereplaceNode = GetSuccessor(delNode);Node* child = nullptr;if (replaceNode->_left != nullptr)child = replaceNode->_left;elsechild = replaceNode->_right;// 更新孩子节点的父指针if (child != nullptr)child->_parent = replaceNode->_parent;if (replaceNode->_parent == nullptr)_root = child;else if (replaceNode == replaceNode->_parent->_left)replaceNode->_parent->_left = child;elsereplaceNode->_parent->_right = child;bool delCol = delNode->_col;// 如果要删除的节点和替换节点不同,则进行值的替换if (delNode != replaceNode){delNode->_kv = replaceNode->_kv;// 更新颜色delNode->_col = replaceNode->_col;}// 如果删除的是黑色节点,需要进行调整if (delCol == BLACK)DeleteFixup(child);}private:// 获取后继节点Node* GetSuccessor(Node* node){Node* cur = node->_right;while (cur->_left != nullptr){cur = cur->_left;}return cur;}// 删除调整void DeleteFixup(Node* node){Node* parent = nullptr;Node* brother = nullptr;while (node != _root && node->_col == BLACK){if (node == node->_parent->_left){brother = node->_parent->_right;// 兄弟节点为红色if (brother->_col == RED){brother->_col = BLACK;node->_parent->_col = RED;RotateL(node->_parent);brother = node->_parent->_right;}// 兄弟节点的两个孩子都为黑色if (brother->_left == nullptr || brother->_left->_col == BLACK &&brother->_right == nullptr || brother->_right->_col == BLACK){brother->_col = RED;node = node->_parent;}else{// 兄弟节点的右孩子为黑色if (brother->_right == nullptr || brother->_right->_col == BLACK){if (brother->_left != nullptr)brother->_left->_col = BLACK;brother->_col = RED;RotateR(brother);brother = node->_parent->_right;}brother->_col = node->_parent->_col;node->_parent->_col = BLACK;if (brother->_right != nullptr)brother->_right->_col = BLACK;RotateL(node->_parent);node = _root;}}else{brother = node->_parent->_left;// 兄弟节点为红色if (brother->_col == RED){brother->_col = BLACK;node->_parent->_col = RED;RotateR(node->_parent);brother = node->_parent->_left;}// 兄弟节点的两个孩子都为黑色if (brother->_right == nullptr || brother->_right->_col == BLACK &&brother->_left == nullptr || brother->_left->_col == BLACK){brother->_col = RED;node = node->_parent;}else{// 兄弟节点的左孩子为黑色if (brother->_left == nullptr || brother->_left->_col == BLACK){if (brother->_right != nullptr)brother->_right->_col = BLACK;brother->_col = RED;RotateL(brother);brother = node->_parent->_left;}brother->_col = node->_parent->_col;node->_parent->_col = BLACK;if (brother->_left != nullptr)brother->_left->_col = BLACK;RotateR(node->_parent);node = _root;}}}node->_col = BLACK;}bool Check(Node* root, int blackNum, const int refNum){if (root == nullptr){// 前序遍历走到空时,意味着一条路径走完了//cout << blackNum << endl;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;}private:Node* _root = nullptr;
};
八、总结
红黑树是一种自平衡的二叉搜索树,它通过保持一些性质来实现自平衡。这些性质使得红黑树的高度保持在一个可控范围内,从而保证了树的操作的时间复杂度为O(log n)。
红黑树的五个性质如下:
- 每个节点要么是红色,要么是黑色。
- 根节点是黑色的。
- 每个叶子节点(NIL节点,空节点)是黑色的。
- 如果一个节点是红色的,则它的两个子节点都是黑色的。
- 从任意一个节点到其子树中的每个叶子节点的路径上都包含相同数目的黑色节点。
这些性质保证了红黑树的平衡性和搜索效率。为了满足这些性质,红黑树进行了一些操作:
- 节点的颜色操作:红黑树中的节点有两种颜色,通过颜色操作可以将节点染成红色或黑色,以满足性质的要求。
- 左旋操作:将某个节点与其右子节点进行左旋,以保持树的平衡性。
- 右旋操作:将某个节点与其左子节点进行右旋,以保持树的平衡性。
- 插入操作:在红黑树中插入一个节点时,首先按照二叉搜索树的规则将其插入,并将其颜色设置为红色。然后通过颜色操作和旋转操作来保持红黑树的性质。
- 删除操作:在红黑树中删除一个节点时,首先按照二叉搜索树的规则找到要删除的节点,并将其替换为其子节点或后继节点。然后通过颜色操作和旋转操作来保持红黑树的性质。
红黑树的优点是在插入和删除操作时能够保持树的平衡性,从而保证了搜索的效率。并且红黑树的高度是相对稳定的,使得其操作的时间复杂度为O(log n)。
总的来说,红黑树是一种高效的自平衡二叉搜索树,通过保持一些性质和进行一些操作来实现平衡,从而保证了搜索的效率。红黑树在实际应用中被广泛使用,例如在C++的STL库中的map和set容器就是使用红黑树来实现的。
最后,今天是1024程序员节日,祝大家节日快乐
std::cout<<"Hello Word"<<std::endl;