目录
1.红黑树的概念
1.1红黑树的特性(4+1)
2.红黑树的框架
3.红黑树的插入
3.1parent在grandfather的左边
3.1parent在grandfather的右边
4.测试自己的红黑树是不是平衡的
红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,确保红黑树没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
每个叶子结点都是黑色的(此处的叶子结点指的是空结点)
//枚举颜色 enum Colour {RED,BLACK, }; template
struct RBtreeNode {RBtreeNode(const pair & kv):_left(nullptr),_right(nullptr),_parent(nullptr),_kv(kv)//初始化给红色,红色比黑色更好处理,_col(RED){}//三叉链RBtreeNode * _left;RBtreeNode * _right;RBtreeNode * _parent;//数据pair _kv;//颜色Colour _col; }; template class RBtree {typedef RBtreeNode Node; public:RBtree():_root(nullptr){}//旋转void RotateL(Node* parent)void RotateR(Node* parent)//插入pair Insert(const pair kv)//寻找Node* Find(const K& key)//测试自己的写的的红黑树,是否合适bool CheckBalance()private:Node* _root;};
pair Insert(const pair kv)//是否为空树{if (_root == nullptr){_root = new Node(kv);_root->_col = BLACK;return make_pair(_root, true);}Node* cur = _root,*parent=_root;while (cur){if (cur->_kv.first < kv.first){parent = cur;cur = cur->_right;}else if (cur->_kv.first > kv.first){parent = cur;cur = cur->_left;}else{return make_pair(cur, false);}}Node* newnode = new Node(kv);newnode->_col = RED;if (parent->_kv.first < kv.first){parent->_right = newnode;newnode->_parent = parent;}else{parent->_left = newnode;newnode->_parent = parent;}cur = newnode;while (parent && parent->_col == RED){// 如果父亲存在,且颜色为红色就需要处理Node* grandfather = parent->_parent;if (parent == grandfather->_left){// 关键是看叔叔Node* uncle = grandfather->_right;// 情况1:uncle存在且为红if (uncle&&uncle->_col == RED){parent->_col = uncle->_col = BLACK;grandfather->_col = RED;// 继续往上处理cur = grandfather;parent = cur->_parent;}else// 情况2+3:uncle不存在 uncle存在且为黑{// 情况2:单旋if (cur == parent->_left){RotateR(grandfather);parent->_col = BLACK ;grandfather->_col = RED;}// 情况3:双旋else{RotateL(parent);RotateR(grandfather);cur->_col = BLACK;grandfather->_col = RED;}//最上面的节点已经变黑了,不用继续break;}}// 如果父亲存在,且颜色为红色就需要处理else{// 关键是看叔叔Node* uncle = grandfather->_left;// 情况1:uncle存在且为红if (uncle && uncle->_col == RED){parent->_col = uncle->_col = BLACK;grandfather->_col = RED;// 继续往上处理cur = grandfather;parent = cur->_parent;}else // 情况2 + 3:uncle不存在 uncle存在且为黑{// 情况2:单旋if (cur == parent->_right){RotateL(grandfather);parent->_col = BLACK;grandfather->_col = RED;}// 情况3:双旋else{RotateR(parent);RotateL(grandfather);cur->_col = BLACK;grandfather->_col = RED;}}break;}}_root->_col = BLACK;return make_pair(newnode, true);}
插入整体逻辑:
3.1不平衡处理
如果有父亲且父亲为红色说明不平衡,就一直向上调整,直到cur到头节点或者parent为黑色
1.第一种情况:有uncle并且uncle为红色;处理:parent和uncle变为黑色,grandfather变红色
2.第二种情况,没有uncle或者有uncle且为黑色,有uncle一定是第一种情况变化而来
这种情况单纯的变色已经做不到平衡了,怎么办?
旋转处理:parent在grandfather的左边,右单旋和左右双旋
旋转代码:
void RotateL(Node* parent){Node* subR = parent->_right;Node* subRL = subR->_left;parent->_right = subRL;if (subRL){subRL->_parent = parent;}subR->_left = parent;Node* parentParent = parent->_parent;parent->_parent = subR;if (parent == _root){_root = subR;_root->_parent = nullptr;}else{if (parentParent->_left == parent){parentParent->_left = subR;}else{parentParent->_right = subR;}subR->_parent = parentParent;}}void RotateR(Node* parent){Node* subL = parent->_left;Node* subLR = subL->_right;parent->_left = subLR;if (subLR)subLR->_parent = parent;subL->_right = parent;Node* parentParent = parent->_parent;parent->_parent = subL;if (parent == _root){_root = subL;_root->_parent = nullptr;}else{if (parentParent->_left == parent)parentParent->_left = subL;elseparentParent->_right = subL;subL->_parent = parentParent;}}
bool _CheckBalance(Node* root,int LeftNum,int count){if (root == nullptr){if (count != LeftNum){return false;}return true;}if (root->_col == RED && root->_parent->_col == RED){cout << "存在连续的红色节点" << endl;return false;}if (root->_col == BLACK){count++;}return _CheckBalance(root->_left, LeftNum, count) && _CheckBalance(root->_right, LeftNum, count);}bool CheckBalance(){if (_root == nullptr){//空树是红黑树return true;}else if(_root->_col==RED){cout << "根节点是红色的" << endl;return false;}else{int LeftNum = 0;Node* left = _root;// 找最左路径做黑色节点数量参考值while (left){if (left->_col == BLACK){LeftNum++;}left = left->_left;}int count = 0;return _CheckBalance(_root, LeftNum, count);}}