C++ 红黑树
Posted qnbk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 红黑树相关的知识,希望对你有一定的参考价值。
红黑树
红黑树
红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
通过颜色互斥来控制平衡
近似平衡,最长路径最多是最短路径的二倍
红黑树的性质
- 每个结点不是红色就是黑色
- 根节点是黑色的
- 如果一个节点是红色的,则它的两个孩子结点是黑色的(没有连续的红色结点)
- 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点-》每条路径上都包含相同数量的黑色结点
- 每个叶子结点都是黑色的(此处的叶子结点指的是空结点)
最短路径:全部由黑色结点构成
最长路径:一黑一红,红色结点数量跟黑色结点数量相等
思考:为什么满足上面的性质,红黑树就能保证:其最长路径中节点个数不会超过最短路径节点个数的两
倍?
红黑树的结构
为了后续实现关联式容器简单,红黑树的实现中增加一个头结点,因为跟节点必须为黑色,为了与根节点进行区分,将头结点给成黑色,并且让头结点的pParent域指向红黑树的根节点,pLeft域指向红黑树中最小的节点,_pRight域指向红黑树中最大的节点
红黑树的插入
因为新节点的默认颜色是红色,因此:如果其parent节点的颜色是黑色,没有违反红黑树任何性质,则不需要调整;但当新插入节点的parenet节点颜色为红色时,就违反了性质三不能有连在一起的红色节点,
cur为当前节点,p->parent,g->grandfather,u->uncle
情况1
cur为红,p为红,g为黑,u存在且为红
注意:此时所看到的树,可能也是子树
情况2
cur为红,p为红,g为黑,u不存在/u存在且为黑
说明:
u的情况有两种
- 1、如果u结点不存在,则cur一定是新插入结点,因为如果cur不是新插入结点,则cur和p一定有一个结点的颜色是黑色,不满足性质4:每条路径黑色结点相同
- 2、如果u结点存在,则其一定是黑色的,那么cur结点原来的颜色一定是黑色的,现在看到是红色是因为cur在子树调整的过程中将cur结点的颜色由黑色改成红色
p为g的左孩子,cur为p的左孩子,则进行右单旋转;
相反,p为g的右孩子,cur为p的右孩子,则进行左单旋转
p、g变色–p变黑,g变红
情况三
cur为红,p为红,g为黑,u不存在/u为黑
p为g的左孩子,cur为p的右孩子,则针对p做左单旋转;
相反,p为g的右孩子,cur为p的左孩子,则针对p做右单旋转,则转换成了情况2
实现代码
(没封装版)
#pragma once
#include <iostream>
using namespace std;
//red-black tree
enum Color
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;
Color _col;
RBTreeNode(const pair<K,V>& kv)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _kv(kv)
, _col(RED)
;
template<class K,class V>
struct _TreeIterator
typedef RBTreeNode<K, V> Node;
Node* _node;
_TreeIterator(Node* node)
:_node(node)
//operator*();
//operator++();
//operator--();
;
template<class K,class V>
class RBTree
typedef RBTreeNode<K, V> Node;
public:
RBTree()
:_root(nullptr)
void _Destory(Node* root)
if (root == nullptr)
return;
_Destory(root->_left);
_Destory(root->_right);
delete root;
~RBTree()
_Destory(_root);
_root = nullptr;
Node* Find(const K& key)
Node* cur = _root;
while (cur)
if (cur->_kv.first > key)
cur = cur->_left;
else if (cur->_kv.first < key)
cur = cur->_right;
else
return cur;
return nullptr;
pair<Node*, bool> Insert(const pair<K, V>& kv)
if (_root == nullptr)
_root = new Node(kv);
_root->_col = BLACK;
return make_pair(_root, 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 make_pair(cur, false);
Node* newnode = new Node(kv);
newnode->_col = RED;
if (parent->_kv.first < kv.first)
parent->_right = newnode;
newnode->_parent = parent;
else
parent->_left = newnode;
newnode->_parent = parent;
//插入的结点是黑色还是红色?
//插入红色结点,可能破坏规则3,但是影响不大
//插入黑色结点,一定破坏规则4,并且会影响其他路径,影响面很大
cur = newnode;
//如果父亲存在,且颜色为红色就需要处理
while (parent && parent->_col == RED)
//关键看叔叔
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)
Node* uncle = grandfather->_right;
if (uncle && uncle->_col == RED)
//情况1:uncle 存在且为红
//把parent和uncle变黑,grandfather变红
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
//继续往上处理
cur = grandfather;
parent = cur->_parent;
else
//情况2+3
//uncle不存在或uncle存在且为黑
if (cur == parent->_left)
//情况2:需要右单旋
RotateR(grandfather);
grandfather->_col = RED;
parent->_col = BLACK;
else
//情况3:左右双旋
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
break;
else//parent == grandfather->right
Node* uncle = grandfather->_left;
if (uncle && uncle->_col == RED)
//情况一
uncle->_col = parent->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
else
//情况2+情况3
if (cur == parent->_right)
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
else // cur == parent->_left
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
//插入结束
break;
_root->_col = BLACK;
return make_pair(newnode, true);
void RotateR(Node* parent)
Node* subl = parent->_left;
Node* sublr = subl->_right;
parent->_left = sublr;
if (sublr)
sublr->_parent = parent;
subl->_right = parent;
Node* parentparent = parent->_parent;
parent->_parent = subl;
if (parent == _root)
//是一个独立的树
_root = subl;
_root->_parent = nullptr;
else
//只是子树,parent还有parent
if (parentparent->_left == parent)
parentparent->_left = subl;
else
parentparent->_right = subl;
subl->_parent = parentparent;
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 (parent == _root)
//是独立的树
_root = subr;
_root->_parent = nullptr;
else
//是子树
if (parentparent->_left == parent)
parentparent->_left = subr;
else
parentparent->_right = subr;
subr->_parent = parentparent;
bool _CheckBalance(Node* root, int blacknum, int count)
if (root == nullptr)
if (count != blacknum)
cout << "黑色结点数目不相等" << endl;
return false;
return true;
if (root->_col == RED && root->_parent->_col == RED)
cout << "存在连续红色" << endl;
return false;
if (root->_col == BLACK)
count++;
return _CheckBalance(root->_left,blacknum,count)
&& _CheckBalance(root->_right,blacknum,count);
bool CheckBalance()
if (_root == nullptr)
return true;
if (_root->_col == RED)
cout << "root is red" << endl;
return false;
//找最左路径做参考值
int blacknum = 0;
Node* left = _root;
while (left)
if (left->_col == BLACK)
blacknum++;
left = left->_left;
int count = 0;
return _CheckBalance(_root, blacknum, count);
void _Inorder(Node* root)
if (root == nullptr)
return;
_Inorder(root->_left);
cout << root->_kv.first <<"->"<<root->_kv.second<< endl ;
_Inorder(root->_right);
void Inorder()
_Inorder(_root);
private:
Node* _root;
;
红黑树模拟实现STL中的map/set
迭代器
#pragma once
// 反向迭代器--迭代器适配器
template<class Iterator>
struct ReverseIterator
typedef typename Iterator::reference Ref;
typedef typename Iterator::pointer Ptr;
typedef ReverseIterator<Iterator> Self;
Iterator _it;
ReverseIterator(Iterator it)
:_it(it)
Ref operator*()
return *_it;
Ptr operator->()
return _it.operator->();
Self& operator++()
--_it;
return *this;
Self& operator--()
++_it;
rteurn *this;
bool operator!=(const Self& s) const
return _it != s._it;
bool operator==(const Self& s) const
return _it == s._it;
;
改造红黑树
#pragma once
#include <iostream>
using namespace std;
#include "Iterator.h"
enum Colour
RED,
BLACK,
;
//red-black
template<class T>
struct RBTreeNode
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
T _data;
Colour _col;
RBTreeNode(const T& x)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _data(x)
, _col(RED)
;
template<class T, class Ref, class Ptr>
struct __TreeIterator
typedef Ref reference;
typedef Ptr pointer;
typedef RBTreeNode<T> Node;
typedef __TreeIterator<T, Ref, Ptr> Self;
Node* _node;
__TreeIterator(Node* node)
:_node(node)
Ref operator*()
return _node->_data;
Ptr operator->()
return &_node->_data;
bool operator != (const Self& s) const
return _node != s._node;
bool operator == (const Self& s) const
return _node == s._node;
// 难点
Self& operator++()
if (_node->_right)
// 下一个访问就是右树中,中序的第一个节点
Node* left = _node->_right;
while (left->_left)
left = left->_left;
_node = left;
else
// 找祖先里面孩子不是父亲的右的那个
// 因为 cur 右为空,说明cur所在的子树已经访问完了
// cur是parent的右的,说明parent也访问完了,继续往上去找
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
cur = cur->_parent;
parent = parent->_parent;
_node = parent;
return *this;
Self& operator--()
if (_node->_left)
// 左子树的最右节点
Node* right = _node->_left;
while (right->_right)
right = right->_right;
_node = right;
else
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_left)
cur = parent;
parent = parent->_parent;
_node = parent;
return *this;
;
template<class K, class T, class KeyOfT>
class RBTree
typedef RBTreeNode<T> Node;
public:
typedef __TreeIterator < T, T删除红黑树的整个子树会保留其属性吗?