c++红黑树的插入-创新互联
#include创新互联建站致力于互联网网站建设与网站营销,提供成都网站制作、成都网站建设、外贸营销网站建设、网站开发、seo优化、网站排名、互联网营销、小程序开发、公众号商城、等建站开发,创新互联建站网站建设策划专家,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。using namespace std; enum Color{ RED, BLACK, }; template struct RBTreeNode { K _key; V _value; RBTreeNode* _left; RBTreeNode* _right; RBTreeNode* _parent; Color _color; RBTreeNode(const K& key, const V& value) :_key(key) , _value(value) , _left(NULL) , _right(NULL) , _parent(NULL) , _color(RED) {} }; template class RBTree { public: typedef RBTreeNode Node; RBTree() :_root(NULL) {} bool Insert(const K& key, const V& value) { if (_root == NULL) { _root = new Node(key,value); _root->_color = BLACK; return true; } Node* cur = _root, *prev = NULL; while (cur) { if (cur->_key < key) { prev = cur; if (cur->_right){ cur = cur->_right; } else { cur->_right = new Node(key, value); cur = cur->_right; cur->_parent = prev; break; } } else if (cur->_key>key) { prev = cur; if (cur->_left){ cur = cur->_left; } else { cur->_left = new Node(key, value); cur = cur->_left; cur->_parent = prev; break; } } else return false; } Node* gf = prev->_parent; while (gf) { if (prev->_color == BLACK) break; Node* uc = NULL; if (prev == gf->_left) uc = gf->_right; else uc = gf->_left; if (uc == NULL||uc->_color==BLACK) { //单旋 if (prev == gf->_left&&cur == prev->_left){//右旋 RotateR(prev, gf); prev->_color = BLACK; gf->_color = RED; } else if (prev == gf->_right&&cur == prev->_right){//左旋 RotateL(prev, gf); prev->_color = BLACK; gf->_color = RED; } //双旋 else if (prev == gf->_right&&cur == prev->_left)//右左旋 { RotateR(cur,prev); RotateL(prev,gf); prev->_color = BLACK; gf->_color = RED; } else//左右旋 { RotateL(cur, prev); RotateR(prev, gf); prev->_color = BLACK; gf->_color = RED; } } else { prev->_color = BLACK; uc->_color = BLACK; if (gf == _root) return true; gf->_color = RED; cur = gf; prev = cur->_parent; gf = prev->_parent; } } return true; } void RotateR(Node* subL,Node* parent) { Node* ppNode = parent->_parent; Node* subLR = subL->_right; subL->_right = parent; parent->_left = subLR; if (subLR) subLR->_parent = parent; if (ppNode){ subL->_parent = ppNode; if (ppNode->_left == parent) ppNode->_left = subL; else ppNode->_right = subL; } else _root = subL; } void RotateL(Node* subR, Node* parent) { Node* ppNode = parent->_parent; Node* subRL = subR->_left; subR->_left = parent; parent->_right = subRL; if (subRL) subRL->_parent = parent; if (ppNode){ subR->_parent = ppNode; if (ppNode->_left == parent) ppNode->_left = subR; else ppNode->_right = subR; } else _root = subR; } bool Remove(const K& key) { } bool IsBalance()//是否平衡 { if (_root == NULL) return true; int num = 0; Node* tmp = _root; while (tmp) { if (tmp->_color == BLACK) num++; tmp = tmp->_left; } return _IsBalance(_root,num); } private: Node* _root; bool _IsBalance(Node* root, int num) { if (root == NULL) return true; if (root->_color == BLACK) num--; if (root->_color == RED){ if (root->_left&&root->_left->_color == RED || root->_right&&root->_right->_color == RED) { cout << "color not balance! key:" << root->_key << endl; return false; } } if (root->_left == NULL&&root->_right == NULL) { if (num == 0) return true; else { cout << "num not balance! key:" << root->_key << endl; return false; } } return _IsBalance(root->_left, num) && _IsBalance(root->_right, num); } };
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:c++红黑树的插入-创新互联
分享链接:http://scjbc.cn/article/iseie.html