红黑树(迭代器)

来源:互联网 发布:三国群英传7Mac 编辑:程序博客网 时间:2024/06/03 19:39
#include<iostream>using namespace std;enum COLOR{RED, BLACK};template<class K, class V>struct RBTreeNode{RBTreeNode(const K& key, const V& value, const COLOR& color = RED): _pLeft(NULL), _pRight(NULL), _pParent(NULL), _key(key), _value(value), _color(color){}RBTreeNode<K, V>* _pLeft;RBTreeNode<K, V>* _pRight;RBTreeNode<K, V>* _pParent;K _key;V _value;COLOR _color;};template<class K,class V,class Ref,class Ptr>class RBTreeTterator{public:typedef RBTreeTterator<K,V,Ref,Ptr> Self;typedef RBTreeNode<K,V> Node;public:RBTreeTterator():_pNode(NULL){}RBTreeTterator(Node* pNode):_pNode(pNode){}RBTreeTterator(const Self& it):_pNode(it._pNode){}Self& operator++(){_Increament();return *this;}Self operator++(int){Self temp(*this);_Increament();return temp;}Self& operator--(){_Decreament();return *this;}Self operator--(int){Self temp(*this);_Decreament();return temp;}Ref operator*(){return _pNode->_key;}Ptr operator->(){return &(_pNode->key);}bool operator!=(const Self& it){return _pNode!=it._pNode;}bool operator==(const Self& it){return _pNode==it->_pNode;}private:void _Increament(){if(_pNode->_pRight){_pNode=_pNode->_pRight;while(_pNode->_pLeft)_pNode=_pNode->_pLeft;}else{Node* pprent=_pNode->_pParent;while(_pNode==pprent->_pRight){_pNode=pprent;pprent=pprent->_pParent;}if(_pNode->_pRight!=pprent)_pNode=pprent;}}void _Decreament(){if(_pNode->_color==RED&&_pNode->_pParent->_pParent==_pNode){_pNode=_pNode->_pRight;return;}if(_pNode->_pLeft){_pNode=_pNode->_pLeft;while(_pNode->_pRight)_pNode=_pNode->_pRight;}else{Node* pprent=_pNode->_pParent;while(_pNode==pprent->_pLeft){_pNode=pprent;pprent=pprent->_pParent;}_pNode=pprent;}}private:Node* _pNode;};template<class K, class V>class RBTree{public:typedef RBTreeNode<K, V> Node;typedef RBTreeTterator<K,V,K&,K*> Iterator;public:RBTree(){_pHead=new Node(0,0);_pHead->_pLeft=_pHead;_pHead->_pRight=_pHead;_pHead->_pParent=NULL;}bool Insert(const K& key, const V& value){Node*& _pRoot=_pHead->_pParent;if(_pRoot==NULL){_pRoot=new Node(key,value);_pRoot->_pParent=_pHead;_pHead->_pParent=_pRoot;_pRoot->_color=BLACK;return true;}Node* pCur=_pRoot;Node* Pparent=NULL;while(pCur){if(key<pCur->_key){Pparent=pCur;pCur=pCur->_pLeft;}else if(key>pCur->_key){Pparent=pCur;pCur=pCur->_pRight;}elsereturn false;}pCur=new Node(key,value);if(key<Pparent->_key)Pparent->_pLeft=pCur;elsePparent->_pRight=pCur;pCur->_pParent=Pparent;while(pCur!=_pRoot&&Pparent->_color==RED){Node* GrandFather=Pparent->_pParent;if(GrandFather->_pLeft==Pparent){Node* Uncle=GrandFather->_pRight;if(Uncle&&Uncle->_color==RED){Pparent->_color=BLACK;Uncle->_color=BLACK;GrandFather->_color=RED;pCur=GrandFather;Pparent=pCur->_pParent;}else{if(Pparent->_pRight==pCur){_RotateL(Pparent);std::swap(Pparent,pCur);}Pparent->_color=BLACK;GrandFather->_color=RED;_RotateR(GrandFather);}}else{Node* Uncle=GrandFather->_pLeft;if(Uncle&&Uncle->_color==RED){GrandFather->_color=RED;Uncle->_color=BLACK;Pparent->_color=BLACK;pCur=GrandFather;Pparent=pCur->_pParent;}else{if(Pparent->_pLeft==pCur){_RotateR(Pparent);std::swap(Pparent,pCur);}Pparent->_color=BLACK;GrandFather->_color=RED;_RotateL(GrandFather);}}}_pRoot->_color=BLACK;_pHead->_pLeft=GetMin();_pHead->_pRight=GetMax();}void InOrder(){Node* _pRoot=_pHead->_pParent;cout<<" InOrder: ";_InOrder(_pRoot);cout<<endl;}Iterator Begin(){return Iterator(_pHead->_pLeft);}Iterator End(){return Iterator(_pHead);}bool CheckRBTree(){Node* _pRoot=_pHead->_pParent;if(_pRoot==NULL)return true;if(_pRoot->_color==RED)return false;Node* pCur=_pRoot;size_t blackcount=0;while(pCur){if(pCur->_color==BLACK)blackcount++;pCur=pCur->_pLeft;}return _CheckRBTree(_pRoot,blackcount,0);}protected:void _RotateL(Node* pParent){Node* _pRoot=_pHead->_pParent;if(pParent==NULL)return;Node* pSubR=pParent->_pRight;Node* pSubRL=pSubR->_pLeft;if(pSubRL)pSubRL->_pParent=pParent;pParent->_pRight=pSubRL;Node* pparent=pParent->_pParent;if(pparent&&pparent!=_pHead){if(pparent->_pRight==pParent)pparent->_pRight=pSubR;elsepparent->_pLeft=pSubR;}else{_pRoot=pSubR;_pRoot->_pParent=_pHead;_pHead->_pParent=_pRoot;}pSubR->_pParent=pparent;pSubR->_pLeft=pParent;pParent->_pParent=pSubR;}void _RotateR(Node* pParent){Node* _pRoot=_pHead->_pParent;if(pParent==NULL)return;Node* pSubL=pParent->_pLeft;Node* pSubLR=pSubL->_pRight;if(pSubLR){pSubLR->_pParent=pParent;}pParent->_pLeft=pSubLR;Node* pparent=pParent->_pParent;if(pparent&&pparent!=_pHead){if(pparent->_pRight==pParent)pparent->_pRight=pSubL;elsepparent->_pLeft=pSubL;}else{_pRoot=pSubL;_pRoot->_pParent=_pHead;_pHead->_pParent=_pRoot;}pSubL->_pParent=pparent;pSubL->_pRight=pParent;pParent->_pParent=pSubL;}void _InOrder(Node* pRoot){if(pRoot){_InOrder(pRoot->_pLeft);cout<<pRoot->_key<<" ";_InOrder(pRoot->_pRight);}}bool _CheckRBTree(Node* pRoot, const size_t blackCount, size_t k){if(pRoot==NULL){return true;}if(pRoot->_pParent&&pRoot->_pParent->_color==RED&&pRoot->_color==RED){return false;}if(pRoot->_color==BLACK){k++;}if(pRoot->_pLeft==NULL&&pRoot->_pRight==NULL){if(k==blackCount)return true;elsereturn false;}return _CheckRBTree(pRoot->_pLeft,blackCount,k)&&_CheckRBTree(pRoot->_pRight,blackCount,k);}Node* GetMin(){Node* pCur=_pHead->_pParent;while(pCur->_pLeft){pCur=pCur->_pLeft;}return pCur;}Node* GetMax(){Node* pCur=_pHead->_pParent;while(pCur->_pRight){pCur=pCur->_pRight;}return pCur;}protected:Node* _pHead;};void TestRBTree(){int a[] = {10, 7, 8, 15, 5, 6, 11, 13, 12};//int a[]={10,18,7,6};RBTree<int, int> t;for(int idx = 0; idx < sizeof(a)/sizeof(a[0]); ++idx)t.Insert(a[idx], idx);t.InOrder();RBTree<int,int>::Iterator it=t.Begin();while(it!=t.End()){cout<<*it<<" ";it++;}cout<<endl;it=t.End();while(it!=t.Begin()){it--;cout<<*it<<" ";}t.CheckRBTree();}int main(){TestRBTree();return 0;}