赞
踩
目录
2.3、cur为红,p为红,g为黑,u不存在/u存在且为黑(变种)
本篇文章以前一篇文章《AVL树》为基础, 在阅读本篇文章之前,需要具备该文章中所讲解的旋转等知识。
红黑树,是一种二叉搜索树,但在每个节点上增加一个存储位表示节点的颜色,可以是 Red 或 Black 。 通过对任何一条从根到叶子的路径上各个节点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
红黑树的性质如下:
红黑树的性质决定了,路径中不可能出现连续的红色节点,且每条路径上都有相同数量的黑色节点。
所以红黑树中最短路径是全部都由黑色节点组成的路径,最长的路径是由红黑节点交替组成的路径。假设全部的黑色节点有 N 个,那么最短路径长度为 logN ,整棵树的节点数量在 N ~ 2N 之间,所以最长路径长度为 2logN 。
- 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);
- private:
- Node* _root = nullptr;
- };
红黑树节点的结构体中多了一个表示颜色的枚举。需要注意的是,在红黑树节点的拷贝构造中,对于颜色的默认值给的是 RED 。
这是因为如果新插入的节点是黑色的,那么一定会违反红黑树的性质 4 ,即导致每条路径上的黑色节点不一致。而如果插入的节点是红色的,则不一定会使红黑树违反性质 3 ,只有在新插入的红色节点的父亲也是红色节点时,才需要进行改动。
红黑树是在二叉搜索树的基础上加上其平衡限制条件,因此红黑树的插入可分为两步:
因为新节点的默认颜色是红色,因此:如果其双亲节点的颜色是黑色,没有违反红黑树任何性质,则不需要调整。但当新插入节点的双亲节点颜色为红色时,就违反了性质三不能有连在一起的红色节点,此时需要对红黑树分情况来讨论:
约定:cur为当前节点,p为父节点,g为祖父节点,u为叔叔节点
cur和p均为红,违反了性质三。解决方式:将p、u改为黑,g改为红,然后把g当成cur,继续向上调整。
实现代码:
- while (parent && parent->_col == RED)
- {
- Node* grandfather = parent->_parent;
- //情况一
- if (grandfather->_left == parent)
- {
- Node* uncle = grandfather->_right;
- //情况1:u存在且为红,变色处理,并继续网上处理
- if (uncle && uncle->_col == RED)
- {
- parent->_col = BLACK;
- uncle->_col = BLACK;
- grandfather->_col = RED;
-
- //继续向上调整
- cur = grandfather;
- parent = cur->_parent;
- }
- //...
- }
- //...
- }
情况2一定是由 2.1 的情况变换过来,并继续向上更新的,否则插入前的状态就不符合红黑树。其演变过程:
此时 c 子树一定包含一个黑节点,d、e子树只能是一个红节点。
解决方式:
解决方法:
此时,情况三变为了情况二,再使用 2.2 的方法就可以。
实现代码:
- while (parent && parent->_col == RED)
- {
- Node* grandfather = parent->_parent;
- if (grandfather->_left == parent)
- {
- Node* uncle = grandfather->_right;
- // u存在且为红,变色处理,并继续网上处理
- if (uncle && uncle->_col == RED)
- {
- parent->_col = BLACK;
- uncle->_col = BLACK;
- grandfather->_col = RED;
-
- //继续向上调整
- cur = grandfather;
- parent = cur->_parent;
- }
- else //情况2与情况3:u不存在或者为黑,旋转+变色
- {
- if (cur == parent->_left)
- {
- RotateR(grandfather);
- parent->_col = BLACK;
- grandfather->_col = RED;
- }
- else
- {
- RotateL(parent);
- RotateR(grandfather);
- cur->_col = BLACK;
- grandfather->_col = RED;
- }
- break;
- }
- }
- else
- {
- Node* uncle = grandfather->_left;
- // u存在且为红,变色处理,并继续网上处理
- if (uncle && uncle->_col == RED)
- {
- parent->_col = BLACK;
- uncle->_col = BLACK;
- grandfather->_col = RED;
-
- //继续向上调整
- cur = grandfather;
- parent = cur->_parent;
- }
- else //情况2与情况3:u不存在或者为黑,旋转+变色
- {
- if (cur == parent->_right)
- {
- RotateL(grandfather);
- parent->_col = BLACK;
- grandfather->_col = RED;
- }
- else
- {
- RotateR(parent);
- RotateL(grandfather);
- cur->_col = BLACK;
- grandfather->_col = RED;
- }
- break;
- }
- }
- }
红黑树的检测分为两步:
编写测试代码:
观察到该树满足二叉搜索树。
根据红黑树的性质编写代码:
- bool _Check(Node* root, int blackNum, int benchmark)
- {
- if (root == nullptr)
- {
- //cout << blackNum << endl;
- if (benchmark != blackNum)
- {
- cout << "某条路径黑色节点的数量不相等" << endl;
- return false;
- }
- return true;
- }
-
- if (root->_col == BLACK)
- {
- ++blackNum;
- }
-
- if (root->_col == RED
- && root->_parent
- && root->_parent->_col == RED)
- {
- cout << "存在连续的红色节点" << endl;
- return false;
- }
-
- return _Check(root->_left, blackNum)
- && _Check(root->_right, blackNum);
- }
-
- bool isBalance()
- {
- if (_root && _root->_col == RED)
- {
- cout << "根节点的颜色是红色" << endl;
- return false;
- }
-
- int benchmark = 0;
- Node* cur = _root;
- while (cur)
- {
- if (cur->_col == BLACK)
- ++benchmark;
- cur = cur->_left;
- }
-
- //是否有连续红色节点,黑色节点数量是否都相同
- _Check(_root, 0, benchmark);
- }
观察到满足红黑树的性质。
红黑树和AVL树都是高效的平衡二叉树,增删改查的时间复杂度都是 O(log N) ,红黑树不追求绝对平衡,其只需保证最长路径不超过最短路径的2倍,相对而言,降低了插入和旋转的次数,所以在经常进行增删的结构中性能比AVL树更优,而且红黑树实现比较简单,所以实际运用中红黑树更多。
- 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:
- ~RBTree()
- {
- _Destroy(_root);
- _root = nullptr;
- }
-
- Node* Find(const K& key)
- {
- Node* cur = _root;
- while (cur)
- {
- if (cur->_kv.first < key)
- {
- cur = cur->_right;
- }
- else if (cur->_kv.first > key)
- {
- cur = cur->_left;
- }
- else
- {
- return cur;
- }
- }
-
- return nullptr;
- }
-
- 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 (grandfather->_left == parent)
- {
- Node* uncle = grandfather->_right;
- // u存在且为红,变色处理,并继续网上处理
- if (uncle && uncle->_col == RED)
- {
- parent->_col = BLACK;
- uncle->_col = BLACK;
- grandfather->_col = RED;
-
- //继续向上调整
- cur = grandfather;
- parent = cur->_parent;
- }
- else //情况2与情况3:u不存在或者为黑,旋转+变色
- {
- if (cur == parent->_left)
- {
- RotateR(grandfather);
- parent->_col = BLACK;
- grandfather->_col = RED;
- }
- else
- {
- RotateL(parent);
- RotateR(grandfather);
- cur->_col = BLACK;
- grandfather->_col = RED;
- }
- break;
- }
- }
- else
- {
- Node* uncle = grandfather->_left;
- // u存在且为红,变色处理,并继续网上处理
- if (uncle && uncle->_col == RED)
- {
- parent->_col = BLACK;
- uncle->_col = BLACK;
- grandfather->_col = RED;
-
- //继续向上调整
- cur = grandfather;
- parent = cur->_parent;
- }
- else //情况2与情况3:u不存在或者为黑,旋转+变色
- {
- if (cur == parent->_right)
- {
- RotateL(grandfather);
- parent->_col = BLACK;
- grandfather->_col = RED;
- }
- else
- {
- RotateR(parent);
- RotateL(grandfather);
- cur->_col = BLACK;
- grandfather->_col = RED;
- }
- break;
- }
- }
- }
- _root->_col = BLACK;
- return true;
- }
-
- void InOrder()
- {
- _InOrder(_root);
- cout << endl;
- }
-
- bool isBalance()
- {
- if (_root && _root->_col == RED)
- {
- cout << "根节点的颜色是红色" << endl;
- return false;
- }
-
- int benchmark = 0;
- Node* cur = _root;
- while (cur)
- {
- if (cur->_col == BLACK)
- ++benchmark;
- cur = cur->_left;
- }
-
- //是否有连续红色节点,黑色节点数量是否都相同
- _Check(_root, 0, benchmark);
- }
-
- int Height()
- {
- return _Height(_root);
- }
-
- private:
- void _Destroy(Node* root)
- {
- if (root == nullptr)
- {
- return;
- }
-
- _Destroy(root->_left);
- _Destroy(root->_right);
- delete root;
- }
-
- int _Height(Node* root)
- {
- if (root == NULL)
- return 0;
-
- int leftH = _Height(root->_left);
- int rightH = _Height(root->_right);
-
- return leftH > rightH ? leftH + 1 : rightH + 1;
- }
-
- bool _Check(Node* root, int blackNum, int benchmark)
- {
- if (root == nullptr)
- {
- //cout << blackNum << endl;
- if (benchmark != blackNum)
- {
- cout << "某条路径黑色节点的数量不相等" << endl;
- return false;
- }
- return true;
- }
-
- if (root->_col == BLACK)
- {
- ++blackNum;
- }
-
- if (root->_col == RED
- && root->_parent
- && root->_parent->_col == RED)
- {
- cout << "存在连续的红色节点" << endl;
- return false;
- }
-
- return _Check(root->_left, blackNum, benchmark)
- && _Check(root->_right, blackNum, benchmark);
- }
-
- void RotateL(Node* parent)
- {
- Node* subR = parent->_right;
- Node* subRL = subR->_left;
-
- parent->_right = subRL;
- if (subRL)
- subRL->_parent = parent;
-
- Node* ppnode = parent->_parent;
-
- subR->_left = parent;
- parent->_parent = subR;
-
- if (ppnode == nullptr)
- {
- _root = subR;
- _root->_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;
-
- Node* ppnode = parent->_parent;
-
- subL->_right = parent;
- parent->_parent = subL;
-
- if (ppnode == nullptr)
- {
- _root = subL;
- _root->_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 << " ";
- _InOrder(root->_right);
- }
-
- private:
- Node* _root = nullptr;
- };
关于红黑树的相关内容就讲到这里,希望同学们多多支持,如果有不对的地方欢迎大佬指正,谢谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。