赞
踩
目录
红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,确保红黑树没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
每个叶子结点都是黑色的(此处的叶子结点指的是空结点)
- //枚举颜色
- enum Colour
- {
- RED,
- BLACK,
- };
- template<class K, class V>
- struct RBtreeNode
- {
- RBtreeNode(const pair<K, V>& kv)
- :_left(nullptr)
- ,_right(nullptr)
- ,_parent(nullptr)
- ,_kv(kv)
- //初始化给红色,红色比黑色更好处理
- ,_col(RED)
- {}
- //三叉链
- RBtreeNode<K, V>* _left;
- RBtreeNode<K, V>* _right;
- RBtreeNode<K, V>* _parent;
- //数据
- pair<K,V> _kv;
- //颜色
- Colour _col;
- };
- template<class K,class V>
- class RBtree
- {
- typedef RBtreeNode<K,V> Node;
- public:
- RBtree()
- :_root(nullptr)
- {}
-
- //旋转
- void RotateL(Node* parent)
- void RotateR(Node* parent)
- //插入
- pair<Node*, bool> Insert(const pair<K, V> kv)
- //寻找
- Node* Find(const K& key)
- //测试自己的写的的红黑树,是否合适
- bool CheckBalance()
- private:
- Node* _root;
-
- };
- pair<Node*, bool> Insert(const pair<K, V> 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;
- else
- parentParent->_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);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。