赞
踩
目录
红黑树的实现中使用了旋转的几个函数
红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
1. 每个结点不是红色就是黑色
2. 根节点是黑色的
3. 如果一个节点是红色的,则它的两个孩子结点是黑色的
4. 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点
5. 每个叶子结点都是黑色的(此处的叶子结点指的是空结点)
以上红黑树的性质使得 其最长路径中节点个数不会超过最短路径节点个数的两倍
- enum Colour
- {
- RED,
- BLACK
- };
-
- template<class K,class V>
- struct RBTreeNode
- {
- RBTreeNode<K, V>* _left;
- RBTreeNode<K, V>* _right;
- RBTreeNode<K, V>* _parent;
- pair<K, V> _kv;
- Colour _col;
-
- RBTreeNode(const pair<K, V>& kv)
- :_left(nullptr)
- , _right(nullptr)
- , parent(nullptr)
- , _kv(kv)
- , _col(RED)
- {}
- };

红黑树中新插入节点的颜色一般都初始化成为红色,因为如果插入黑色就会导致各个分支上黑色节点的数量不同,初始化成红色节点也方便向上调整
为了后续实现关联式容器简单,红黑树的实现中增加一个头结点,因为跟节点必须为黑色,为了与根节点进行区分,将头结点给成黑色,并且让头结点的 pParent 域指向红黑树的根节点,pLeft域指向红黑树中最小的节点,_pRight域指向红黑树中最大的节点,如下:
插入操作:
红黑树是在二叉搜索树的基础上加上其平衡限制条件,因此红黑树的插入可分为两步:
1. 按照二叉搜索的树规则插入新节点
2. 检测新节点插入后,红黑树的性质是否造到破坏
因为新节点的默认颜色是红色,因此:如果其双亲节点的颜色是黑色,没有违反红黑树任何性质,则不需要调整;但当新插入节点的双亲节点颜色为红色时,就违反了性质三不能有连在一起的红色节点,此时需要对红黑树分情况来讨论:
约定:cur为当前节点,p为父节点,g为祖父节点,u为叔叔节点情况一: cur为红,p为红,g为黑,u存在且为红
分为了几种情况:
情况一
cur为红,p为红,g为黑,u存在且为红
如果g是根节点,调整完成后,需要将g改为黑色如果g是子树,g一定有双亲,且g的双亲如果是红色,需要继续向上调整。
cur和p均为红,不符合红黑树的规则
所以将p,u改为黑,g改为红,然后把g当成cur,继续向上调整。
情况二
cur为红,p为红,g为黑,u不存在/u存在且为黑
u的情况有两种
1.如果u节点不存在,则cur一定是新插入节点,因为如果cur不是新插入节点则cur和p一定有一个节点的颜色是黑色,就不满足性质4:每条路径黑色节点个数相同。
2.如果u节点存在,则其一定是黑色的,那么cur节点原来的颜色一定是黑色的现在看到其是红色的原因是因为cur的子树在调整的过程中将cur节点的颜色由黑色改成红色。p为g的左孩子,cur为p的左孩子,则进行右单旋转;相反p为g的右孩子,cur为p的右孩子,则进行左单旋转p、g变色--p变黑,g变红
情况三
cur为红,p为红,g为黑,u不存在/u存在且为黑
p为g的左孩子,cur为p的右孩子,则针对p做左单旋转;相反,p为g的右孩子,cur为p的左孩子,则针对p做右单旋转,这样旋转完了之后就变成了我们上面所分析的情况二。
- bool Insert(const pair<K, V>& kv)
- {
- if (_root == nullptr)
- {
- _root = new Node(kv);
- _root->_col = BLACK;
- return true;
- }
-
- Node* parent = nullptr;
- Node* cur = _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 false;
- }
- }
-
- cur = new Node(kv); // 红色的
- if (parent->_kv.first < kv.first)
- {
- parent->_right = cur;
- }
- else
- {
- parent->_left = cur;
- }
- cur->_parent = parent;
-
- while (parent && parent->_col == RED)
- {
- Node* grandfather = parent->_parent;
- if (parent == grandfather->_left)
- {
- Node* uncle = grandfather->_right;
- // 情况一:叔叔存在且为红
- if (uncle && uncle->_col == RED)
- {
- // 变色
- parent->_col = uncle->_col = BLACK;
- grandfather->_col = RED;
-
- // 继续往上处理
- cur = grandfather;
- parent = cur->_parent;
- }
- else
- {
- // 情况二:叔叔不存在或者存在且为黑
- // 旋转+变色
- if (cur == parent->_left)
- {
- // g
- // p u
- // c
- RotateR(grandfather);
- parent->_col = BLACK;
- grandfather->_col = RED;
- }
- else
- {
- // g
- // p u
- // c
- RotateL(parent);
- RotateR(grandfather);
- cur->_col = BLACK;
- grandfather->_col = RED;
- }
-
- break;
- }
- }
- else
- {
- Node* uncle = grandfather->_left;
- // 情况一:叔叔存在且为红
- if (uncle && uncle->_col == RED)
- {
- // 变色
- parent->_col = uncle->_col = BLACK;
- grandfather->_col = RED;
-
- // 继续往上处理
- cur = grandfather;
- parent = cur->_parent;
- }
- else
- {
- // 情况二:叔叔不存在或者存在且为黑
- // 旋转+变色
- // g
- // u p
- // c
- if (cur == parent->_right)
- {
- RotateL(grandfather);
- parent->_col = BLACK;
- grandfather->_col = RED;
- }
- else
- {
- // g
- // u p
- // c
- RotateR(parent);
- RotateL(grandfather);
- cur->_col = BLACK;
- grandfather->_col = RED;
- }
-
- break;
- }
- }
- }
-
- _root->_col = BLACK;
-
- return true;
- }

红黑树的验证就是 判断一下 这课树是否满足 红黑树的这几条性质, 但是 不能用这四条性质推出来的结论来判断这棵树是不是红黑树。
- bool Check(Node* cur)
- {
- if (cur == nullptr)
- return true; //空树也是红黑树
-
- if (cur->_col == RED && cur->_parent->_col == RED)
- {
- cout << cur->_kv.first << "存在连续的红色节点" << endl;
- return false;
- }//判断是否存在红色节点
-
- return Check(cur->_left)
- && Check(cur->_right);
- }
-
- bool IsBalance()
- {
- if (_root && _root->_col == RED)//判断根节点是否为黑色
- return false;
-
- return Check(_root);
- }

- enum Colour
- {
- RED,
- BLACK
- };
-
- template<class K, class V>
- struct RBTreeNode
- {
- RBTreeNode<K, V>* _left;
- RBTreeNode<K, V>* _right;
- RBTreeNode<K, V>* _parent;
- pair<K, V> _kv;
- Colour _col;
-
- RBTreeNode(const pair<K, V>& kv)
- :_left(nullptr)
- , _right(nullptr)
- , _parent(nullptr)
- , _kv(kv)
- ,_col(RED)
- {}
- };
-
- template<class K, class V>
- class RBTree
- {
- typedef RBTreeNode<K, V> Node;
- public:
- bool Insert(const pair<K, V>& kv)
- {
- if (_root == nullptr)
- {
- _root = new Node(kv);
- _root->_col = BLACK;
- return true;
- }
-
- Node* parent = nullptr;
- Node* cur = _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 false;
- }
- }
-
- cur = new Node(kv); // 红色的
- if (parent->_kv.first < kv.first)
- {
- parent->_right = cur;
- }
- else
- {
- parent->_left = cur;
- }
- cur->_parent = parent;
-
- while (parent && parent->_col == RED)
- {
- Node* grandfather = parent->_parent;
- if (parent == grandfather->_left)
- {
- Node* uncle = grandfather->_right;
- // 情况一:叔叔存在且为红
- if (uncle && uncle->_col == RED)
- {
- // 变色
- parent->_col = uncle->_col = BLACK;
- grandfather->_col = RED;
-
- // 继续往上处理
- cur = grandfather;
- parent = cur->_parent;
- }
- else
- {
- // 情况二:叔叔不存在或者存在且为黑
- // 旋转+变色
- if (cur == parent->_left)
- {
- // g
- // p u
- // c
- RotateR(grandfather);
- parent->_col = BLACK;
- grandfather->_col = RED;
- }
- else
- {
- // g
- // p u
- // c
- RotateL(parent);
- RotateR(grandfather);
- cur->_col = BLACK;
- grandfather->_col = RED;
- }
-
- break;
- }
- }
- else
- {
- Node* uncle = grandfather->_left;
- // 情况一:叔叔存在且为红
- if (uncle && uncle->_col == RED)
- {
- // 变色
- parent->_col = uncle->_col = BLACK;
- grandfather->_col = RED;
-
- // 继续往上处理
- cur = grandfather;
- parent = cur->_parent;
- }
- else
- {
- // 情况二:叔叔不存在或者存在且为黑
- // 旋转+变色
- // g
- // u p
- // c
- if (cur == parent->_right)
- {
- RotateL(grandfather);
- parent->_col = BLACK;
- grandfather->_col = RED;
- }
- else
- {
- // g
- // u p
- // c
- RotateR(parent);
- RotateL(grandfather);
- cur->_col = BLACK;
- grandfather->_col = RED;
- }
-
- break;
- }
- }
- }
-
- _root->_col = BLACK;
-
- return true;
- }
-
- void RotateL(Node* parent)
- {
- Node* subR = parent->_right;
- Node* subRL = subR->_left;
-
- parent->_right = subRL;
- if (subRL)
- subRL->_parent = parent;
-
- subR->_left = parent;
- Node* ppnode = parent->_parent;
- parent->_parent = subR;
-
- if (parent == _root)
- {
- _root = subR;
- subR->_parent = nullptr;
- }
- else
- {
- if (ppnode->_left == parent)
- {
- ppnode->_left = subR;
- }
- else
- {
- ppnode->_right = subR;
- }
- subR->_parent = ppnode;
- }
- }
-
- void RotateR(Node* parent)
- {
- Node* subL = parent->_left;
- Node* subLR = subL->_right;
-
- parent->_left = subLR;
- if (subLR)
- subLR->_parent = parent;
-
- subL->_right = parent;
-
- Node* ppnode = parent->_parent;
- parent->_parent = subL;
-
- if (parent == _root)
- {
- _root = subL;
- subL->_parent = nullptr;
- }
- else
- {
- if (ppnode->_left == parent)
- {
- ppnode->_left = subL;
- }
- else
- {
- ppnode->_right = subL;
- }
- subL->_parent = ppnode;
- }
- }
-
- void _InOrder(Node* root)
- {
- if (root == nullptr)
- return;
-
- _InOrder(root->_left);
- cout << root->_kv.first << endl;
- _InOrder(root->_right);
- }
-
- void InOrder()
- {
- _InOrder(_root);
- }
-
- bool Check(Node* cur)
- {
- if (cur == nullptr)
- return true;
-
- if (cur->_col == RED && cur->_parent->_col == RED)
- {
- cout << cur->_kv.first << "存在连续的红色节点" << endl;
- return false;
- }
-
- return Check(cur->_left)
- && Check(cur->_right);
- }
-
- bool IsBalance()
- {
- if (_root && _root->_col == RED)
- return false;
-
- return Check(_root);
- }
-
- private:
- Node* _root = nullptr;
- };

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。