目录
1.红黑树的概念
红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或 Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。
2 . 红黑树的性质
1. 每个结点不是红色就是黑色
2. 根节点是黑色的
3. 如果一个节点是红色的,则它的两个孩子结点是黑色的
4. 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点
5. 每个叶子结点都是黑色的(此处的叶子结点指的是空结点)
思考:为什么满足上面的性质,红黑树就能保证:其最长路径中节点个数不会超过最短路径节点 个数的两倍?
3. 红黑树节点的定义
enum Colour
{
RED,
BLACK
};
template <class K, class V>
struct RBTNode
{
pair<K, V> _kv;
RBTNode<K, V>* _left;
RBTNode<K, V>* _right;
RBTNode<K, V>* _parent;
Colour _col;
RBTNode(const pair<K, V>& kv)
:_kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
{}
};
4. 红黑树的插入操作
红黑树是在二叉搜索树的基础上加上其平衡限制条件,因此红黑树的插入可分为两步:
1. 按照二叉搜索的树规则插入新节点
2. 检测新节点插入后,红黑树的性质是否造到破坏因为新节点的默认颜色是红色,因此:
如果其双亲节点的颜色是黑色,没有违反红黑树任何 性质,则不需要调整;
但当新插入节点的双亲节点颜色为红色时,就违反了性质三(不能有连在一起的红色节点),此时需要对红黑树分情况来讨论:
约定:cur为当前节点,p为父节点,g为祖父节点,u为叔叔节点
5. 红黑树的验证
红黑树的检测分为两步:
1. 检测其是否满足二叉搜索树(中序遍历是否为有序序列)
2. 检测其是否满足红黑树的性质
bool IsBalance()
{
if (_root == nullptr)
return true;
if (_root->_col == RED)
{
return false;
}
// 参考值
int refNum = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLACK)
{
++refNum;
}
cur = cur->_left;
}
return Check(_root, 0, refNum);
}
bool Check(Node* root, int blackNum, const int refNum)
{
if (root == nullptr)
{
//cout << blackNum << endl;
if (refNum != blackNum)
{
cout << "存在黑色节点的数量不相等的路径" << endl;
return false;
}
return true;
}
if (root->_col == RED && root->_parent->_col == RED)
{
cout << root->_kv.first << "存在连续的红色节点" << endl;
return false;
}
if (root->_col == BLACK)
{
blackNum++;
}
return Check(root->_left, blackNum, refNum)
&& Check(root->_right, blackNum, refNum);
}
6. 红黑树的删除
红黑树的删除本节不做讲解,有兴趣的同学可参考:《算法导论》或者《STL源码剖析》
http://www.cnblogs.com/fornever/archive/2011/12/02/2270692.html
7. 红黑树与AVL树的比较
红黑树和AVL树都是高效的平衡二叉树,增删改查的时间复杂度都是O(log_2 N),红黑树不追 求绝对平衡,其只需保证最长路径不超过最短路径的2倍,相对而言,降低了插入和旋转的次数, 所以在经常进行增删的结构中性能比AVL树更优,而且红黑树实现比较简单,所以实际运用中红 黑树更多。
8. 红黑树的应用
1. C++ STL库 -- map/set、mutil_map/mutil_set
2. Java 库
3. linux内核
4. 其他一些库
http://www.cnblogs.com/yangecnu/p/Introduce-Red-Black-Tree.html
总代码:
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
enum Colour
{
RED,
BLACK
};
template <class K, class V>
struct RBTNode
{
pair<K, V> _kv;
RBTNode<K, V>* _left;
RBTNode<K, V>* _right;
RBTNode<K, V>* _parent;
Colour _col;
RBTNode(const pair<K, V>& kv)
:_kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
{}
};
template<class K, class V>
class RBTree
{
typedef RBTNode<K, V> Node;
public:
RBTree()
:_root(nullptr)
{}
RBTree(const RBTree<K, V>& t)
{
_root = Copy(t._root);
}
~RBTree()
{
Destroy(_root);
_root = nullptr;
}
void Inorder()
{
_Inorder(_root);
}
Node* Find(const K& key)
{
//从根开始找
Node* cur = _root;
while (cur)
{
if (cur->_kv.first > key)
{
cur = cur->_left;
}
else if (cur->_kv.first < key)
{
cur = cur->_right;
}
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* cur = _root;
Node* parent = nullptr;//parent是要插入位置的父亲
while (cur != nullptr)
{
if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
//找到了,不用插入
return false;
}
}
Node* newNode = new Node(kv);
//新增节点,颜色给红色
newNode->_col = RED;
if (parent->_kv.first < kv.first)
{
parent->_right = newNode;
}
else
{
parent->_left = newNode;
}
newNode->_parent = parent;
cur = newNode;
//父亲为黑色直接结束
//父亲节点为红色(出现双红情况),进行处理
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
// g
// p
//new
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
if (uncle && uncle->_col == RED)// u存在且为红 -》变色再继续往上处理
{
// g
// p u
//new
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
//向上处理
cur = grandfather;
parent = cur->_parent;
}
else// u不存在或存在且为黑
{
// g g
// p p u
//cur cur
if (cur == parent->_left)//右单旋
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else//双旋
{
// g g
// p p u
// cur cur
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else
{
// g
// u p
// new
Node* uncle = grandfather->_left;
if (uncle && uncle->_col == RED)
{
// g
// u p
// new
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
//向上处理
cur = grandfather;
parent = cur->_parent;
}
else
{
// g g
// p u p
// cur cur
if (cur == parent->_right)//单旋
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g g
// p u p
// cur cur
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return true;
}
bool IsBalance()
{
if (_root == nullptr)
return true;
if (_root->_col == RED)
{
return false;
}
// 参考值
int refNum = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLACK)
{
++refNum;
}
cur = cur->_left;
}
return Check(_root, 0, refNum);
}
private:
bool Check(Node* root, int blackNum, const int refNum)
{
if (root == nullptr)
{
//cout << blackNum << endl;
if (refNum != blackNum)
{
cout << "存在黑色节点的数量不相等的路径" << endl;
return false;
}
return true;
}
if (root->_col == RED && root->_parent->_col == RED)
{
cout << root->_kv.first << "存在连续的红色节点" << endl;
return false;
}
if (root->_col == BLACK)
{
blackNum++;
}
return Check(root->_left, blackNum, refNum)
&& Check(root->_right, blackNum, refNum);
}
int _Height(Node* Root)
{
if (Root == nullptr)
return 0;
int leftHeight = _Height(Root->_left);
int rightHeight = _Height(Root->_right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
void RotateR(Node* parent)
{
if (parent == nullptr)
return;
Node* SubL = parent->_left;
Node* SubLR = SubL->_right;
Node* parent2 = parent->_parent;
parent->_left = SubLR;
if (SubLR)
{
SubLR->_parent = parent;
}
SubL->_right = parent;
parent->_parent = SubL;
if (parent2)
{
if (parent2->_left == parent)
{
parent2->_left = SubL;
}
else
{
parent2->_right = SubL;
}
SubL->_parent = parent2;
}
else
{
_root = SubL;
SubL->_parent = nullptr;//容易忘
}
}
void RotateL(Node* parent)
{
if (parent == nullptr)
return;
Node* SubR = parent->_right;
Node* SubRL = SubR->_left;
Node* parent2 = parent->_parent;
parent->_parent = SubR;
SubR->_left = parent;
parent->_right = SubRL;
if (SubRL)
SubRL->_parent = parent;
if (parent2)
{
if (parent2->_left == parent)
{
parent2->_left = SubR;
}
else
{
parent2->_right = SubR;
}
SubR->_parent = parent2;
}
else
{
_root = SubR;
_root->_parent = nullptr;
}
}
Node* Copy(Node* root)
{
if (root == nullptr)
return nullptr;
//前序创建树
Node* newRoot = new Node(root->_kv.first, root->_kv.second);
newRoot->_left = Copy(root->_left);
newRoot->_right = Copy(root->_right);
return newRoot;
}
void Destroy(Node* root)
{
if (root == nullptr)
return;
//后序毁树
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
void _Inorder(Node* root)
{
if (root == nullptr)
{
return;
}
_Inorder(root->_left);
cout << root->_kv.first << ":" << root->_kv.second << endl;
_Inorder(root->_right);
}
private:
Node* _root;
};
void TestRBTree1()
{
RBTree<int, int> t;
//int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14,18,26,24,31 };
for (auto e : a)
{
/*if (e == 9)
{
int i = 0;
}*/
t.Insert({ e, e });
//cout << e << "->" << t.IsBalanceTree() << endl;
}
t.Inorder();
cout << t.IsBalance() << endl;
}
标签:Node,cur,parent,实现,C++,红黑树,root,col
From: https://blog.csdn.net/m0_73751295/article/details/143443042