map底层(RBTree.hpp)红黑树
#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;
KeyOfValue keyofvalue;
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;
};
用于封装底层红黑树的Map_RBTree.hpp
#include"RBTree.hpp"
#include<string>
namespace bite{
template <class K,class V>
class Map{
typedef pair<K,V> ValueType;
struct KeyOfValue{
const K&operator()(const ValueType& data){
return data.first;
}
};
public:
typedef typename RBTree<K,pair<K,V>,ValueType>::Iterator Iterator;
public:
Map()
:_t()
{
}
Iterator begin(){
return _t.Begin();
}
Iterator end(){
return _t.End();
}
size_t size()const{
return _t.Size();
}
bool empty()const{
return _t.Empty();
}
Iterator find(const K& key){
return _t.Find(make_pair(key,V()));
}
V& operator[](const K& key){
pair<Iterator,bool> ret=Insert(make_pair(key,V()));
return ret.first->second;
}
const V& operator[](const K& key)const{
return ( _t.Insert(make_pair(key,V()) ).first)->second;
}
pair<Iterator,bool> Insert(const ValueType& data){
return _t.Insert(data);
}
private:
RBTree<K,pair<K,V>,ValueType> _t;
};
}
用于测试模拟实现map的代码
#include"Map_RBTree.hpp"
void testMap(){
bite::Map<int ,string> m;
m.Insert(pair<int,string>(1, "你好"));
m.Insert(pair<int,string>(4,"将来"));
m.Insert(make_pair(2,"比特"));
m[3] = "中国";
bite::Map<int,string>::Iterator t=m.begin();
while(t!=m.end()){
cout<<t->first<<" "<<t->second<<endl;
t++;
}
cout<<"该容器中存储的键值对个数:"<<m.size()<<endl;
}
int main(){
testMap();
return 0;
}
- 对于map[n]=x的执行过程的讲解
- 首先执行operator[]操作,其内部是将键值和V()进行插入
- 再对插入过程的返回值的first执行operator()操作取到_pNode->kv
- 再取其键值对中的second返回,之后将V()使用x进行代替