set底层的红黑树实现
#pragma once
#include<iostream>
#include<functional>
using namespace std;
enum Color{
Red,
Black
};
template <class T>
struct RBTNode{
RBTNode(const T& kv=T(),Color color=Red)
:_kv(kv)
,_color(color)
,_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
{
}
T _kv;
Color _color;
RBTNode<T>* _left;
RBTNode<T>* _right;
RBTNode<T>* _parent;
};
template <class T>
struct RBTreeIterator{
typedef RBTNode<T> Node;
typedef Node* pNode;
typedef RBTreeIterator<T> Self;
RBTreeIterator(pNode PNode)
:_pNode(PNode)
{
}
RBTreeIterator()
:_pNode(nullptr)
{
}
RBTreeIterator(const Self& s)
:_pNode(s._pNode)
{
}
T& operator*(){
return _pNode->_kv;
}
T* operator->(){
return &_pNode->_kv;
}
Self& operator++(){
Increament();
return *this;
}
Self operator++(int){
Self temp(*this);
Increament();
return temp;
}
Self& operator--(){
DeIncreament();
return *this;
}
Self operator--(int){
Self temp(*this);
DeIncreament();
return temp;
}
bool operator==(const Self& s)const{
return _pNode==s._pNode;
}
bool operator!=(const Self& s)const{
return _pNode!=s._pNode;
}
private:
void Increament(){
if(_pNode->_right){
_pNode=_pNode->_right;
while(_pNode->_left){
_pNode=_pNode->_left;
}
}else{
Node* parent=_pNode->_parent;
while(_pNode==parent->_right){
_pNode=parent;
parent=_pNode->_parent;
}
if(_pNode->_right!=parent)
_pNode=parent;
}
}
void DeIncreament(){
if (_pNode->_parent->_parent == _pNode&& Red==_pNode->_color){
_pNode = _pNode->_right;
}
else if (_pNode->_left){
_pNode = _pNode->_left;
while (_pNode->_right){
_pNode = _pNode->_right;
}
}
else{
Node* pParent = _pNode->_parent;
while (_pNode == pParent->_left){
_pNode = pParent;
pParent = _pNode->_parent;
}
_pNode = pParent;
}
}
private:
Node* _pNode;
};
template <class K,class V,class KeyOfValue>
class RBTree{
public:
typedef RBTNode<V> Node;
typedef Node* pNode;
typedef RBTreeIterator<V> Iterator;
Iterator Begin(){
return Iterator(_head->_left);
}
Iterator End(){
return Iterator(_head);
}
Iterator Find(const V&data)const{
pNode cur=_head->_parent;
while(cur){
if(KeyOfValue(data)==KeyOfValue(cur->_kv)){
return Iterator(cur);
}else if(KeyOfValue(data)<KeyOfValue(cur->_kv)) {
cur=cur->_left;
}else{
cur=cur->_right;
}
return End();
}
}
bool Empty()const{
return _head->_parent==nullptr;
}
size_t Size()const{
return _size;
}
public:
RBTree()
:_size(0)
{
_head=new Node;
_head->_left=_head;
_head->_right=_head;
}
pair<Iterator,bool> Insert(const V& kv){
pNode pRoot=GetRoot();
pNode pNewNode=nullptr;
if(pRoot==nullptr){
pNewNode=pRoot=new Node(kv,Black);
pRoot->_parent=_head;
_head->_parent=pRoot;
_head->_left=pRoot;
_head->_right=pRoot;
++_size;
return make_pair(Iterator(pNewNode),true);
}
else
{
pNode cur=pRoot;
pNode parent=nullptr;
while(cur){
parent=cur;
if(KeyOfValue(cur->_kv) > KeyOfValue(kv)){
cur=cur->_left;
}else if(KeyOfValue(kv) > KeyOfValue(cur->_kv)){
cur=cur->_right;
}else{
return make_pair(Iterator(cur),false);
}
}
pNewNode=cur=new Node(kv);
if(KeyOfValue(cur->_kv) < KeyOfValue(parent->_kv)){
parent->_left=cur;
}else{
parent->_right=cur;
}
cur->_parent=parent;
while(cur!=pRoot && parent->_color==Red){
pNode gparent=parent->_parent;
if(gparent->_left==parent){
pNode uncle=gparent->_right;
if(uncle && uncle->_color==Red){
parent->_color=Black;
uncle->_color=Black;
gparent->_color=Red;
cur=gparent;
parent=cur->_parent;
}else{
if(parent->_right==cur){
RotateL(parent);
swap(parent,cur);
}
RotateR(gparent);
parent->_color=Black;
gparent->_color=Red;
break;
}
}else{
pNode uncle=gparent->_left;
if(uncle && uncle->_color==Red){
parent->_color=uncle->_color=Black;
gparent->_color=Red;
cur=gparent;
parent=cur->_parent;
}else{
if(parent->_left==cur){
RotateR(parent);
swap(cur,parent);
}
RotateL(gparent);
gparent->_color=Red;
parent->_color=Black;
break;
}
}
}
}
++_size;
pRoot->_color=Black;
_head->_left=leftMost();
_head->_right=rightMost();
return make_pair(Iterator(pNewNode),true);
}
pNode leftMost(){
pNode pRoot=GetRoot();
if(pRoot==nullptr){
return _head;
}
pNode cur=pRoot;
while(cur && cur->_left){
cur=cur->_left;
}
return cur;
}
pNode rightMost(){
pNode pRoot=GetRoot();
if(pRoot==nullptr){
return _head;
}
pNode cur=pRoot;
while(cur && cur->_right){
cur=cur->_right;
}
return cur;
}
void RotateL(pNode parent){
pNode subR=parent->_right;
pNode subRL=subR->_left;
subR->_left=parent;
parent->_right=subRL;
if(subRL){
subRL->_parent=parent;
}
if(parent!=_head->_parent){
pNode gparent=parent->_parent;
if(gparent->_left==parent)
gparent->_left=subR;
else
gparent->_right=subR;
subR->_parent=gparent;
}
else {
subR->_parent=_head;
_head->_parent=subR;
}
parent->_parent=subR;
}
void RotateR(pNode parent){
pNode subL=parent->_left;
pNode subLR=subL->_right;
subL->_right=parent;
parent->_left=subLR;
if(subLR){
subLR->_parent=parent;
}
if(parent!=_head->_parent){
pNode gParent=parent->_parent;
if(gParent->_left==parent){
gParent->_left=subL;
}else{
gParent->_right=subL;
}
subL->_parent=gParent;
}else{
subL->_parent=_head;
_head->_parent=subL;
}
parent->_parent=subL;
}
void Inorder(){
_Inorder(_head->_parent);
cout<<endl;
}
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->_left;
}
size_t k = 0;
return _IsValidRBTree(pRoot, k, blackCount);
}
private:
void _Inorder(pNode parent){
if(parent){
_Inorder(parent->_left);
cout<<"Key:"<<parent->_kv.first<<"Value:"<<parent->_kv.second<<endl;
_Inorder(parent->_right);
}
}
pNode& GetRoot(){
return _head->_parent;
}
bool _IsValidRBTree(pNode pRoot, size_t k, const size_t blackCount) {
if (nullptr == pRoot)
{
if (k != blackCount)
{
cout << "违反性质四:每条路径中黑色节点的个数必须相同" << endl;
return false;
}
return true;
}
if (Black == pRoot->_color)
k++;
pNode pParent = pRoot->_parent;
if (pParent && Red == pParent->_color && Red == pRoot->_color)
{
cout << "违反性质三:没有连在一起的红色节点" << endl;
return false;
}
return _IsValidRBTree(pRoot->_left, k, blackCount) &&
_IsValidRBTree(pRoot->_right, k, blackCount);
}
private:
pNode _head;
size_t _size;
};
对底层进行封装的Set.hpp
#pragma once
#include"RBTree.hpp"
namespace bite{
template<class K>
class Set{
typedef K ValueType;
public:
typedef typename RBTree<K,K,ValueType>::Iterator Iterator;
pair<Iterator,bool> Insert(const ValueType& key){
return _t.Insert(key);
}
Iterator begin(){
return _t.Begin();
}
Iterator end(){
return _t.End();
}
bool empty(){
return _t.Empty();
}
K& operator[](const K& key){
K ret=_t.Insert(key);
return ret;
}
private:
RBTree<K,K,ValueType> _t;
};
}
对模拟实现的Set进行测试
#include"Set.hpp"
#include<iostream>
using namespace std;
void TestSet(){
bite::Set<int> set;
set.Insert(0);
set.Insert(9);
for(auto& e:set){
cout<<e<<endl;
}
cout<<"该容器中是否为空:"<<set.empty()<<endl;
}
int main(){
TestSet();
return 0;
}