红黑树

来源:互联网 发布:yum配置 编辑:程序博客网 时间:2024/06/10 23:24
#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 RBTree{typedef RBTreeNode<K, V> Node;public:RBTree(): _pRoot(NULL){}bool Insert(const K& key, const V& value){if(_pRoot==NULL){_pRoot=new Node(key,value);_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;}void InOrder(){cout<<" InOrder: ";_InOrder(_pRoot);cout<<endl;}bool CheckRBTree(){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){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){if(pparent->_pRight==pParent)pparent->_pRight=pSubR;elsepparent->_pLeft=pSubR;}else_pRoot=pSubR;pSubR->_pParent=pparent;pSubR->_pLeft=pParent;pParent->_pParent=pSubR;}void _RotateR(Node* 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){if(pparent->_pRight==pParent)pparent->_pRight=pSubL;elsepparent->_pLeft=pSubL;}else_pRoot=pSubL;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);}protected:Node* _pRoot;};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();t.CheckRBTree();}int main(){TestRBTree();return 0;}

原创粉丝点击