hello~ 很高兴见到大家! 这次带来的是C++中关于红黑树这部分的一些知识点,如果对你有所帮助的话,可否留下你宝贵的三连呢? 个 人 主 页: 默|笙



enum Colour
{
RED,
BLACK
};
template<class K, class V>
struct RBTreeNode
{
RBTreeNode(const pair<K, V>& kv)
:_kv(kv)
,_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
{ };
pair<K, V> _kv;
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
Colour _col = RED;
};
template<class K, class V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
public:
private:
Node* _root = nullptr;
};bool Insert(const pair<K, V>& kv)
{
//处理空树的情况
if (_root == nullptr)
{
_root = new Node(kv);
_root->_col = BLACK;
}
//找到要插入的位置
else
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (kv.first < cur->_kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (kv.first > cur->_kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
//插入新节点
cur = new Node(kv);
if (parent->_kv.first > kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
return true;
接下来cur简记为c,parent简记为p,grandfather简记为g,uncle简记为u。

旋转具体实现过程看<AVL树博客>
当p为g的左孩子且cur为p的左孩子时,右单旋。

当p为g的右孩子且cur为p的右孩子时,左单旋。

p是g的左孩子且cur是p的右孩子时,进行左右双旋。

当p为g的右孩子且cur为p的左孩子时,右左双旋。

插入完整代码:
bool Insert(const pair<K, V>& kv)
{
//处理空树的情况
if (_root == nullptr)
{
_root = new Node(kv);
_root->_col = BLACK;
return true;
}
//找到要插入的位置
else
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (kv.first < cur->_kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (kv.first > cur->_kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
//插入新节点
cur = new Node(kv);
if (parent->_kv.first > kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
//父节点为红色的情况下需要进行处理
if (parent->_col == RED)
{
while (parent && parent->_col == RED)
{
//记录节点
Node* grandfather = parent->_parent;
Node* uncle = nullptr;
if (grandfather->_left == parent)
{
uncle = grandfather->_right;
}
else
{
uncle = grandfather->_left;
}
//uncle为红色的情况
//仅变色
if (uncle && uncle->_col == RED)
{
uncle->_col = parent->_col = BLACK;
grandfather->_col = RED;
//更新
cur = grandfather;
parent = cur->_parent;
}
//uncle为黑色或为空的情况
else
{
//右旋转
if (grandfather->_left == parent && parent->_left == cur)
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
break;
}
//左旋转
else if (grandfather->_right == parent && parent->_right == cur)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
break;
}//左右双旋
else if (grandfather->_left == parent && parent->_right == cur)
{
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
break;
}
//右左双旋
else
{
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
break;
}
}
}
}
}
//处理根节点颜色
_root->_col = BLACK;
return true;
}红黑树检测代码:
bool IsBalance()
{
if (_root->_col == RED)
{
return false;
}
int leftMost = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLACK)
{
leftMost++;
}
cur = cur->_left;
}
return _check(_root, 0, leftMost);
}
bool _check(Node* cur, int BlackNum, const int leftMost)
{
if (cur == nullptr)
{
if (BlackNum != leftMost)
{
return false;
cout << "黑色节点的数量不相等" << endl;
}
else
return true;
}
if (cur->_col == RED && cur->_parent && cur->_parent->_col == RED)
{
cout << cur->_kv.first << "->" << "连续的红色节点" << endl;
return false;
}
if (cur->_col == BLACK)
{
BlackNum++;
}
return _check(cur->_left, BlackNum, leftMost) && _check(cur->_right, BlackNum, leftMost);
}#pragma once
#include<iostream>
using namespace std;
enum Colour
{
RED,
BLACK
};
template<class K, class V>
struct RBTreeNode
{
RBTreeNode(const pair<K, V>& kv)
:_kv(kv)
,_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
{ };
pair<K, V> _kv;
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
Colour _col = RED;
};
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;
}
//找到要插入的位置
else
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (kv.first < cur->_kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (kv.first > cur->_kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
//插入新节点
cur = new Node(kv);
if (parent->_kv.first > kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
//父节点为红色的情况下需要进行处理
if (parent->_col == RED)
{
while (parent && parent->_col == RED)
{
//记录节点
Node* grandfather = parent->_parent;
Node* uncle = nullptr;
if (grandfather->_left == parent)
{
uncle = grandfather->_right;
}
else
{
uncle = grandfather->_left;
}
//uncle为红色的情况
//仅变色
if (uncle && uncle->_col == RED)
{
uncle->_col = parent->_col = BLACK;
grandfather->_col = RED;
//更新
cur = grandfather;
parent = cur->_parent;
}
//uncle为黑色或为空的情况
else
{
//右旋转
if (grandfather->_left == parent && parent->_left == cur)
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
break;
}
//左旋转
else if (grandfather->_right == parent && parent->_right == cur)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
break;
}//左右双旋
else if (grandfather->_left == parent && parent->_right == cur)
{
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
break;
}
//右左双旋
else
{
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
break;
}
}
}
}
}
//处理根节点颜色
_root->_col = BLACK;
return true;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool IsBalance()
{
if (_root->_col == RED)
{
return false;
}
int leftMost = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLACK)
{
leftMost++;
}
cur = cur->_left;
}
return _check(_root, 0, leftMost);
}
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;
}
private:
int _Size(Node* root)
{
return root == nullptr ? 0 : _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;
}
bool _check(Node* cur, int BlackNum, const int leftMost)
{
if (cur == nullptr)
{
if (BlackNum != leftMost)
return false;
else
return true;
}
if (cur->_col == RED && cur->_parent && cur->_parent->_col == RED)
{
return false;
}
if (cur->_col == BLACK)
{
BlackNum++;
}
return _check(cur->_left, BlackNum, leftMost) && _check(cur->_right, BlackNum, leftMost);
}
void _InOrder(const Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.first << " ";
_InOrder(root->_right);
}
void RotateR(Node* parent)
{
//记录节点
Node* subL = parent->_left;
Node* subLR = subL->_right;
Node* parentParent = parent->_parent;
//改变指针
parent->_left = subLR;
subL->_right = parent;
if (parentParent == nullptr)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subL;
}
else
{
parentParent->_right = subL;
}
subL->_parent = parentParent;
}
//避免subLR = nullptr出现空指针解引用的情况
if (subLR)
{
subLR->_parent = parent;
}
parent->_parent = subL;
}
void RotateL(Node* parent)
{
//记录节点
Node* subR = parent->_right;
Node* subRL = subR->_left;
Node* parentParent = parent->_parent;
//改变指针指向
subR->_left = parent;
parent->_right = subRL;
if (parentParent == nullptr)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subR;
}
else
{
parentParent->_right = subR;
}
subR->_parent = parentParent;
}
if (subRL)
{
subRL->_parent = parent;
}
parent->_parent = subR;
}
Node* _root = nullptr;
};今天的分享就到此结束啦,如果对读者朋友们有所帮助的话,可否留下宝贵的三连呢~~ 让我们共同努力, 一起走下去!