C++第十六讲:红黑树
- 1.什么是红黑树
- 1.1红黑树的特点
- 2.MyRBTree实现
- 2.1红黑树的结构
- 2.2红黑树的插入
- 2.2.1插入的总体逻辑
- 2.2.2情况一:变色
- 2.2.3情况二:单旋 + 变色
- 2.2.4情况三:双旋 + 变色
- 2.2.4插入代码总结
- 2.3红黑树的检查
- 2.4完整代码实现
- 3.红黑树的删除
1.什么是红黑树
总结:
1.每一个结点不是红色就是黑色。
2.根节点必须是黑色的。
3.红色节点的子节点必须是黑节点,也就是说任意一条道路上不会有连续的红色结点。
4.对任意一个结点,从该结点到其所有NULL结点的简单路径上,均包含相同数量的黑色结点。
上面说的是红黑树的规则,但是在算法导论上,又补充了一条规则:每一个叶子结点(NIL)都是黑色的规则,这个叶子结点不是先前理解的叶子结点,而是空结点。
所以上面的图里面其实是有6条路径的,而不是两条路径。
1.1红黑树的特点
根据红黑树的规则可以推出:红黑树能够确保最长路径不超过最短路径的两倍,我们分析一下为什么:
假设每一个路径上都存在bh个黑色结点,那么在极端情况下存在的最短路径为:全部都是黑色结点,也就是bh个结点。最长路径为:一个黑色结点,一个红色结点,交叉排列,也就是2bh个结点,所以说,这就保证了,最长路径不会超过最短路径的两倍。
2.MyRBTree实现
2.1红黑树的结构
红黑树,首先要有颜色的标识,而且要有结点的表示,最后就是红黑树的总体表示:
enum Color {
BLACK,
RED
};
template <class K, class V>
struct RBTreeNode
{
pair<K.V> _kv;
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
Color color;
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;
};
2.2红黑树的插入
2.2.1插入的总体逻辑
1.首先要保证二叉搜索树的逻辑,也就是二叉搜索树的插入逻辑
2.要保证红黑树的规则,这里会存在三种情况,下面就是来讨论三种情况的
插入的总体逻辑就是二叉搜索树的插入:
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)
{
parent = cur;
if (kv->first < cur->_kv.first)
cur = cur->_left;
else if (kv->first > cur->_kv.first)
cur = cur->_right;
else return false;
}
cur = new Node(kv);
cur->color = RED;//先假设插入的结点是红色的
}
2.2.2情况一:变色
2.2.3情况二:单旋 + 变色
2.2.4情况三:双旋 + 变色
2.2.4插入代码总结
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)
{
parent = cur;
if (kv->first < cur->_kv.first)
cur = cur->_left;
else if (kv->first > cur->_kv.first)
cur = cur->_right;
else return false;
}
cur = new Node(kv);
cur->color = RED;
if (cur->_kv.first < parent->_kv.first) parent->_left = cur;
else parent->_right = cur;
cur->_parent = parent;
//变色,当多个红色在一起出现时,就要进行变色处理
while (parent && parent->color == RED)
{
Node* grandfather = parent->_parent;
if (grandfather->_left == parent)
{
Node* uncle = grandfather->_right;
if (uncle && uncle->color == RED)
{
//当unclde存在且为红色时,进行变色
parent->color = uncle->color = BLACK;
grandfather->color = RED;
cur = grandfather;
parent = cur->_parent;
}
else
{
//当uncle不存在或者unclde存在但为黑时,旋转 + 变色
if (cur == parent->_left)
{
//插入的位置在左边,单旋 + 变色
RotateR(grandfather);
parent->color = BLACK;
grandfather->color = RED;
}
else
{
RotateL(parent);
RotateR(grandfather);
cur->color = BLACK;
grandfather->color = RED;
}
break;
}
}
else//grandfather->_right == parent
{
Node* uncle = grandfather->_left;
if (uncle && uncle->color == RED)
{
//当unclde存在且为红色时,进行变色
parent->color = uncle->color = BLACK;
grandfather->color = RED;
cur = grandfather;
parent = cur->_parent;
}
else
{
//当uncle不存在或者unclde存在但为黑时,旋转 + 变色
if (cur == parent->_right)
{
//插入的位置在右边,单旋 + 变色
RotateL(grandfather);
parent->color = BLACK;
grandfather->color = RED;
}
else
{
RotateR(parent);
RotateL(grandfather);
cur->color = BLACK;
grandfather->color = RED;
}
break;
}
}
}
_root->color = BLACK;//这一点也要注意
return true;
}
2.3红黑树的检查
我们创建的红黑树结构是否正确,还需要我们进行检查,检查的方法为检查各个路径下的黑色结点是否相同。算法为先算出来一条道路上的黑色结点数量,再通过递归检查所有道路上的黑色结点数量是否相同:
bool Check(Node* root, int blackNum, const int refNum)
{
if (root == nullptr)
{
//root为空,证明一条道路走完了,此时再进行数量的比较
if (blackNum != refNum)
{
cout << "路径上的黑色结点数目为:" << blackNum << endl;
cout << "和参考值的黑色结点数目不同:" << refNum << endl;
return false;
}
return true;
}
if (root->color == BLACK) blackNum++;
if (root->color == RED && root->_parent->color == RED)
{
cout << "存在连续的红色结点" << endl;
return false;
}
return Check(root->_left, blackNum, refNum) &&
Check(root->_right, blackNum, refNum);
}
bool IsBalance()
{
if (_root == nullptr) return true;
if (_root->color == RED) return false;
int refNum = 0;//设置一个参考值
Node* cur = _root;
while (cur)
{
if (cur->color == BLACK) refNum++;
cur = cur->_left;
}
return Check(_root, 0, refNum);//递归进行检查
}
2.4完整代码实现
#pragma once
enum Color {
BLACK,
RED
};
template <class K, class V>
struct RBTreeNode
{
pair<K, V> _kv;
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
Color color;
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->color = BLACK;
return true;
}
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
parent = cur;
if (kv.first < cur->_kv.first)
cur = cur->_left;
else if (kv.first > cur->_kv.first)
cur = cur->_right;
else return false;
}
cur = new Node(kv);
cur->color = RED;
if (cur->_kv.first < parent->_kv.first) parent->_left = cur;
else parent->_right = cur;
cur->_parent = parent;
//变色,当多个红色在一起出现时,就要进行变色处理
while (parent && parent->color == RED)
{
Node* grandfather = parent->_parent;
if (grandfather->_left == parent)
{
Node* uncle = grandfather->_right;
if (uncle && uncle->color == RED)
{
//当unclde存在且为红色时,进行变色
parent->color = uncle->color = BLACK;
grandfather->color = RED;
cur = grandfather;
parent = cur->_parent;
}
else
{
//当uncle不存在或者unclde存在但为黑时,旋转 + 变色
if (cur == parent->_left)
{
//插入的位置在左边,单旋 + 变色
RotateR(grandfather);
parent->color = BLACK;
grandfather->color = RED;
}
else
{
RotateL(parent);
RotateR(grandfather);
cur->color = BLACK;
grandfather->color = RED;
}
break;
}
}
else//grandfather->_right == parent
{
Node* uncle = grandfather->_left;
if (uncle && uncle->color == RED)
{
//当unclde存在且为红色时,进行变色
parent->color = uncle->color = BLACK;
grandfather->color = RED;
cur = grandfather;
parent = cur->_parent;
}
else
{
//当uncle不存在或者unclde存在但为黑时,旋转 + 变色
if (cur == parent->_right)
{
//插入的位置在右边,单旋 + 变色
RotateL(grandfather);
parent->color = BLACK;
grandfather->color = RED;
}
else
{
RotateR(parent);
RotateL(grandfather);
cur->color = BLACK;
grandfather->color = RED;
}
break;
}
}
}
_root->color = BLACK;//这一点也要注意
return true;
}
bool Check(Node* root, int blackNum, const int refNum)
{
if (root == nullptr)
{
//root为空,证明一条道路走完了,此时再进行数量的比较
if (blackNum != refNum)
{
cout << "路径上的黑色结点数目为:" << blackNum << endl;
cout << "和参考值的黑色结点数目不同:" << refNum << endl;
return false;
}
return true;
}
if (root->color == BLACK) blackNum++;
if (root->color == RED && root->_parent->color == RED)
{
cout << "存在连续的红色结点" << endl;
return false;
}
return Check(root->_left, blackNum, refNum) &&
Check(root->_right, blackNum, refNum);
}
bool IsBalance()
{
if (_root == nullptr) return true;
if (_root->color == RED) return false;
int refNum = 0;//设置一个参考值
Node* cur = _root;
while (cur)
{
if (cur->color == BLACK) refNum++;
cur = cur->_left;
}
return Check(_root, 0, refNum);//递归进行检查
}
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;
}
protected:
int _Size(Node* root)
{
if (root == nullptr)
return 0;
return _Size(root->_left) + _Size(root->_right) + 1;
}
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;
}
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 (parent == _root)
{
_root = subL;
subL->_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* 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(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.first << ":" << root->_kv.second << endl;
_InOrder(root->_right);
}
private:
Node* _root = nullptr;
};
3.红黑树的删除
红黑树的删除不做讲解,感兴趣的自行了解,《算法导论》或《STL源码剖析》中都有讲解。