红黑树的概念:
红黑树是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
红黑树的性质:
- 每个结点不是红色就是黑色
- 根节点是黑色的
- 如果一个节点是红色的,则它的两个孩子结点是黑色的
- 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点
- 每个叶子结点都是黑色的(此处的叶子结点指的是空结点)
红黑树节点的定义:
enum COLOUR
{
BLACK,
RED
};
template <class T, class U>
struct RBTNode
{
pair<T, U> _kv;
RBTNode<T, U>* _left;
RBTNode<T, U>* _right;
RBTNode<T, U>* _parent;
COLOUR _col;
RBTNode(const pair<T, U>& kv = pair<T, U>())
:_left(nullptr)
,_right(nullptr)
,_kv(kv)
,_parent(nullptr)
{}
};
红黑树结构:
红黑树的插入操作:
首先定义四个重要的红黑树节点:g(grandfather)、p(parent)、u(uncle)、c(current)
c为红,p为红,g为黑,u存在且为红
情况一:
c是新增,abcde都是空
情况二:
c不是新增,c之前是黑色,cde是具有一个黑节点的红黑树
c为红,p为红,g为黑,u存在且为黑/u不存在
情况一:
c是新增,u不存在,abcde都是空
情况二:
c不是新增,c之前是黑色,u存在且为黑,a、b是一个红色节点,c是一个具有黑色节点的红黑树,d、e是空或有一个红节点
红黑树的验证:
- 是否满足搜索二叉树
- 是否满足红黑树
bool IsValidRBTree()
{
PNode pRoot = GetRoot();
// 空树也是红黑树
if (nullptr == pRoot)
return true;
// 检测根节点是否满足情况
if (BLACK != pRoot->_color)
{
cout << "违反红黑树性质二:根节点必须为黑色" << endl;
return false;
}
// 获取任意一条路径中黑色节点的个数
size_t blackCount = 0;
PNode pCur = pRoot;
while (pCur)
{
if (BLACK == pCur->_color)
blackCount++;
pCur = pCur->_pLeft;
}
// 检测是否满足红黑树的性质,k用来记录路径中黑色节点的个数
size_t k = 0;
return _IsValidRBTree(pRoot, k, blackCount);
}
bool _IsValidRBTree(PNode pRoot, size_t k, const size_t blackCount)
{
//走到null之后,判断k和black是否相等
if (nullptr == pRoot)
{
if (k != blackCount)
{
cout << "违反性质四:每条路径中黑色节点的个数必须相同" << endl;
return false;
}
return true;
}
// 统计黑色节点的个数
if (BLACK == pRoot->_color)
k++;
// 检测当前节点与其双亲是否都为红色
PNode pParent = pRoot->_pParent;
if (pParent && RED == pParent->_color && RED == pRoot->_color)
{
cout << "违反性质三:没有连在一起的红色节点" << endl;
return false;
}
return _IsValidRBTree(pRoot->_pLeft, k, blackCount) &&
_IsValidRBTree(pRoot->_pRight, k, blackCount);
}
红黑树的源码:
#pragma once
#include <iostream>
#include <assert.h>
#include <string>
#include <vector>
using namespace std;
enum COLOUR
{
BLACK,
RED
};
template <class T, class U>
struct RBTNode
{
pair<T, U> _kv;
RBTNode<T, U>* _left;
RBTNode<T, U>* _right;
RBTNode<T, U>* _parent;
COLOUR _col;
RBTNode(const pair<T, U>& kv = pair<T, U>())
:_left(nullptr)
,_right(nullptr)
,_kv(kv)
,_parent(nullptr)
{}
};
template<class T,class U>
struct RBTreeIterator
{
typedef RBTNode<T, U> Node;
typedef RBTreeIterator<T, U> Self;
Node* _node;
RBTreeIterator(Node *node = Node())
:_node(node)
{}
T& operator*()
{
return _node->_kv.first;
}
/*U& operator*()
{
return _node->_kv.second;
}*/
Self& operator++()
{
if (_node->_right)
{
// 右不为空,右子树最左节点就是中序下一个
Node* leftMost = _node->_right;
while (leftMost->_left)
{
leftMost = leftMost->_left;
}
_node = leftMost;
}
else
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
bool operator==(const Self& s)
{
return _node == s._node;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
};
template <class T, class U>
class RBTtree
{
typedef RBTNode<T, U> Node;
typedef RBTreeIterator<T, U> iterator;
public:
RBTtree()
:_root(nullptr)
{}
iterator begin()
{
Node* pcur = _root;
while (pcur && pcur->_left)
{
pcur = pcur->_left;
}
return iterator(pcur);
}
iterator end()
{
return iterator(nullptr);
}
bool Insert(const pair<T, U>& key)
{
if (_root == nullptr)
{
_root = new Node(key);
_root->_col = BLACK;
return true;
}
Node* parent = nullptr;
Node* pcur = _root;
while (pcur)
{
if (pcur->_kv.first < key.first)
{
parent = pcur;
pcur = pcur->_right;
}
else if (pcur->_kv.first > key.first)
{
parent = pcur;
pcur = pcur->_left;
}
else
{
return false;
}
}
pcur = new Node(key);
pcur->_col = RED;
if (parent->_kv.first > key.first)
{
parent->_left = pcur;
}
else if (parent->_kv.first < key.first)
{
parent->_right = pcur;
}
pcur->_parent = parent;
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
if (uncle && uncle->_col == RED)
{
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
pcur = grandfather;
parent = pcur->_parent;
}
else
{
if (pcur == parent->_left)
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
RotateL(parent);
RotateR(grandfather);
pcur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else
{
Node* uncle = grandfather->_left;
if (uncle && uncle->_col == RED)
{
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
pcur = grandfather;
parent = pcur->_parent;
}
else
{
if (pcur == parent->_right)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
RotateR(parent);
RotateL(grandfather);
pcur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return true;
}
bool Find(const pair<T, U>& key)
{
Node* pcur = _root;
while (pcur)
{
if (key.first < pcur->_kv.first)
{
pcur = pcur->_left;
}
else if (key.first > pcur->_kv.first)
{
pcur = pcur->_right;
}
else
{
return true;
}
}
return false;
}
bool Inorder()
{
_Inorder(_root);
return true;
}
int Height()
{
return _height(_root);
}
int Size()
{
return _size(_root);
}
~RBTtree()
{
_BSTtree(_root);
_root = nullptr;
}
RBTtree(const RBTtree<T, U>& b)
{
_root = _Copy(b._root);
}
RBTtree<T, U>&operator=(RBTtree<T, U> node)
{
std::swap(node._root, _root);
return *this;
}
private:
void _Inorder(Node* root)
{
if (root == nullptr)
{
return;
}
_Inorder(root->_left);
printf("%d ", root->_kv.first);
_Inorder(root->_right);
}
void _BSTtree(Node* root)
{
if (root == nullptr)
{
return;
}
_BSTtree(root->_left);
_BSTtree(root->_right);
delete root;
}
Node* _Copy(Node* root)
{
if (root == nullptr)
{
return nullptr;
}
Node* newroot = new Node(root->_kv);
newroot->_left = _Copy(root->_left);
newroot->_right = _Copy(root->_right);
return newroot;
}
int _height(Node* root)
{
if (root == nullptr)
{
return 0;
}
int left = _height(root->_left);
int right = _height(root->_right);
return left > right ? left + 1 : right + 1;
}
int _size(Node* root)
{
return root == nullptr ? 0 : _size(root->_left) + _size(root->_right) + 1;
}
void RotateL(Node* parent)
{
_rotateNum++;
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 RotateR(Node* parent)
{
_rotateNum++;
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* parentParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (parentParent == nullptr)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parent == parentParent->_left)
{
parentParent->_left = subL;
}
else
{
parentParent->_right = subL;
}
subL->_parent = parentParent;
}
}
Node* _root = nullptr;
public:
int _rotateNum = 0;
};