目录
红黑树的概念
红黑树的性质
红黑树节点的定义
红黑树的插入
1. 按照二叉搜索的树规则插入新节点
2. 检测新节点插入后,红黑树的性质是否造到破坏
红黑树的概念
红黑树的性质
- 1. 每个结点不是红色就是黑色
- 2. 根节点是黑色的
- 3. 如果一个节点是红色的,则它的两个孩子结点是黑色的
- 4. 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点
- 5. 每个叶子结点都是黑色的(此处的叶子结点指的是空结点)
红黑树节点的定义
enum Color { 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; Color _col; //节点颜色 RBTreeNode(const pair<K,V>& kv) :_left(nullptr) ,_right(nullptr) ,_parent(nullptr) ,_kv(kv) ,_col(RED) //默认红色 {} };
为什么构造函数中的节点默认给的是红色? 通过红黑树的性质能总结出:
性质3 - 代表着不能出现连续的红色节点
性质4 - 每条路径上都有相同数量的黑色节点
这时会发现违反性质3比违反性质4的代价要更小,而事实上也的确是这样做的,可能会有疑问,这样还是红黑树嘛,其实新增节点之后是需要通过变色来达到红黑树的结构的。(如下)
示例:
此时我们会发现,最后的结构是符合红黑树的:没有出现连续的红色节点;每条路径上的黑色节点数量相同。
红黑树的插入
1. 按照二叉搜索的树规则插入新节点
template<class K, class V> class RBTree { typedef RBTreeNode<K, V> Node; bool Insert(const pair<K, V>& kv) { if (_root == nullptr) { _root = new Node(kv); _root->_col = BLACK; return true; } Node* cur = _root; Node* parent = nullptr; //记录cur的父节点,方便进行链接 while (cur) { if (kv.first < cur->_kv.first) //插入的值小于存储的值 { parent = cur; cur = cur->_left; } else if (kv.first > cur->_kv.first) //插入的值大于存储的值 { parent = cur; cur = cur->_right; } else { return false; //相等,插入失败 } } //注意:新增的节点构造是红色,所以这里不用给颜色 cur = new Node(kv); if (kv.first < parent->_kv.first) { parent->_left = cur; } else { parent->_right = cur; } cur->_parent = parent; //...调整颜色 return true; } protected: Node* _root = nullptr; };
2. 检测新节点插入后,红黑树的性质是否造到破坏
注意:关于旋转处理可以参考AVL树的旋转处理
约定:1.grandfather->_left = parent; grandfather->_right = uncle; (父节点在左,叔节点在右)
grandfather为黑,parent为红,cur(新增节点)为红
① uncle节点存在且为红色 ----》 parent , uncle 改黑,grandfather改红,继续向上调整
② uncle节点不存在 or 存在且为黑 - 单旋
③ uncle节点不存在 or 存在且为黑 - 双旋
约定:2.grandfather->_left = uncle; grandfather->_right = parent; (叔节点在左,父节点在右)
(参考上面)
具体代码演示:
//旋转+更改颜色 while (parent && parent->_col == RED) { //记录祖先节点 Node* grandfather = parent->_parent; //父节点是祖先节点的左子树 if (grandfather->_left == parent) { Node* uncle = grandfather->_right; //①uncle节点存在且为红 if (uncle && uncle->_col == RED) { parent->_col = uncle->_col = BLACK; grandfather->_col = RED; //继续向上调整 cur = grandfather; parent = cur->_parent; //注意需要重新计数parent } else { //②uncle不存在 or 存在且为黑 -> 变色 + 旋转 if (cur == parent->_left) { //单旋 RotateR(grandfather); parent->_col = BLACK; grandfather->_col = RED; } else { //双旋 RotateL(parent); RotateR(grandfather); parent->_col = grandfather->_col = RED; cur->_col = BLACK; } break; } } else if (grandfather->_right == parent) //父节点是祖先节点的右子树 { Node* uncle = grandfather->_left; if (uncle && uncle->_col == RED) { parent->_col = uncle->_col = BLACK; grandfather->_col = RED; cur = grandfather; parent = cur->_parent; } else { if (cur == parent->_right) { //单旋 RotateL(grandfather); parent->_col = BLACK; grandfather->_col = RED; } else { //双旋 RotateR(parent); RotateL(grandfather); parent->_col = grandfather->_col = RED; cur->_col = BLACK; } break; } } else { assert(false); } } //结尾做保险处理,无论什么情况,根节点置成黑 _root->_col = BLACK;
附上红黑树(测试是否是红黑树)部分代码:
RBTree.h
#pragma once #include <iostream> #include <assert.h> #include <string> using namespace std; enum Color { 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; Color _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: void InOrder() { _InOrder(_root); cout << endl; } //检查是否是红黑树 bool IsBalance() { //检查1:根节点颜色 if (_root && _root->_col != BLACK) { cout << "根节点为红" << endl; return false; } //检查2:是否存在连续红色节点 //检查3:每条路径上的黑色节点是否相同 return _Check(_root, 0); } bool Insert(const pair<K, V>& kv) { if (_root == nullptr) { _root = new Node(kv); _root->_col = BLACK; return true; } Node* cur = _root; Node* parent = nullptr; //记录cur的父节点,方便进行链接 while (cur) { if (kv.first < cur->_kv.first) //插入的值小于存储的值 { parent = cur; cur = cur->_left; } else if (kv.first > cur->_kv.first) //插入的值大于存储的值 { parent = cur; cur = cur->_right; } else { return false; //相等,插入失败 } } //注意:新增的节点构造是红色,所以这里不用给颜色 cur = new Node(kv); if (kv.first < parent->_kv.first) { parent->_left = cur; } else { parent->_right = cur; } cur->_parent = parent; //旋转+更改颜色 while (parent && parent->_col == RED) { //记录祖先节点 Node* grandfather = parent->_parent; //父节点是祖先节点的左子树 if (grandfather->_left == parent) { Node* uncle = grandfather->_right; //①uncle节点存在且为红 if (uncle && uncle->_col == RED) { parent->_col = uncle->_col = BLACK; grandfather->_col = RED; //继续向上调整 cur = grandfather; parent = cur->_parent; //注意需要重新计数parent } else { //②uncle不存在 or 存在且为黑 -> 变色 + 旋转 if (cur == parent->_left) { //单旋 RotateR(grandfather); parent->_col = BLACK; grandfather->_col = RED; } else { //双旋 RotateL(parent); RotateR(grandfather); parent->_col = grandfather->_col = RED; cur->_col = BLACK; } break; } } else if (grandfather->_right == parent) //父节点是祖先节点的右子树 { Node* uncle = grandfather->_left; if (uncle && uncle->_col == RED) { parent->_col = uncle->_col = BLACK; grandfather->_col = RED; cur = grandfather; parent = cur->_parent; } else { if (cur == parent->_right) { //单旋 RotateL(grandfather); parent->_col = BLACK; grandfather->_col = RED; } else { //双旋 RotateR(parent); RotateL(grandfather); parent->_col = grandfather->_col = RED; cur->_col = BLACK; } break; } } else { assert(false); } } //结尾做保险处理,无论什么情况,根节点置成黑 _root->_col = BLACK; return true; } protected: void _InOrder(Node* root) { if (root == nullptr) return; _InOrder(root->_left); cout << root->_kv.first << " "; _InOrder(root->_right); } bool _Check(Node* root, int BlackNums) { if (root == nullptr) { cout <<BlackNums << endl; return true; } //这里是传值返回 if (root->_col == BLACK) ++BlackNums; //root与root的子树不好判断,因为节点不一定有子树,但是一定有父节点 if (root->_col == RED &&root->_parent && root->_parent->_col == RED) { cout << "存在连续的红色节点" << endl; return false; } return _Check(root->_left, BlackNums) && _Check(root->_right, BlackNums); } //左单旋 void RotateL(Node* parent) { Node* subR = parent->_right; Node* subRL = subR->_left; parent->_right = subRL; if (subRL) subRL->_parent = parent; //提前记录祖先节点 Node* pparent = parent->_parent; subR->_left = parent; parent->_parent = subR; //值得注意的是,parent节点不一定为根节点,也就是旋转的可能是一棵子树而不是整棵树 if (pparent == nullptr) //意味着parent节点是根节点 { _root = subR; _root->_parent = nullptr; } else { //判断parent 在 祖先节点的左还是右 if (pparent->_right == parent) { pparent->_right = subR; } else { pparent->_left = subR; } subR->_parent = pparent; //更改subR的父节点 } } //右单旋 void RotateR(Node* parent) { Node* subL = parent->_left; Node* subLR = subL->_right; parent->_left = subLR; if (subLR) subLR->_parent = parent; //提前记录祖先节点 Node* pparent = parent->_parent; subL->_right = parent; parent->_parent = subL; if (parent == _root) { _root = subL; _root->_parent = nullptr; } else { //判断parent 在 祖先节点的左还是右 if (pparent->_right == parent) { pparent->_right = subL; } else { pparent->_left = subL; } subL->_parent = pparent; //更改subR的父节点 } } protected: Node* _root = nullptr; }; void Test_RBTree1() { int arr1[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 }; int arr2[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 }; RBTree<int, int> t1; for (auto e : arr1) { t1.Insert(make_pair(e, e)); cout << e << "插入:" << t1.IsBalance() << endl; //插入进行检查 } t1.InOrder(); cout << t1.IsBalance() << endl; }