这两天看了一下红黑树,自己总结了一点红黑树的性质和如何插入数据,写出来自己的理解分享给大家,有什么问题希望指出。
》什么是红黑树:红黑树是基于平衡树也就是它也需要符合平衡树的特性。其次,红黑树,顾名思义就是树的节点不仅包括数据,指针,还要有颜色,一个节点不是红色就是黑色。
》红黑树的性质:1.树的根节点必须是黑色
2.不能有连续的两个红色节点,即父子节点不能同时为红色
3.树的每条路径上的黑色节点数量必须相同
》接下来说一下,红黑树节点如何插入?
首先在构造新的Node节点时,节点的颜色应该给红色的,插入之后再进行相应调整。
节点的插入分如下几种情况:
1.空树:如果树是空的,那么就直接插入Node,并且将节点的颜色设置为黑色的直接返回即可。
2.找到相应位置,插入之后如果父节点是黑色,直接退出即可。
3.以上两种情况都不是,那么就是父节点也是红色的。然后这个分为以下几种情况:
>父节点是祖父节点的左节点:(叔叔节点是祖父节点的右孩子)
@叔叔节点存在并且为红色的:那么叔叔节点必然为祖父节点的右节点,并且祖父节点肯定是黑色的(根据红黑树的第二条性质得出),这时候就只需要将父节点与叔叔节点设置为黑色,将祖父节点设置为红色即可
@叔叔节点不存在或者为黑色:如果孩子节点是父节点的左孩子那么将祖父与父亲的颜色交换然后以祖父为轴右旋;如果孩子节点是父节点的右孩子,那么将祖父节点与子节点的颜色交换,然后先以父节点为轴左旋,再以祖父节点为轴右旋
》父节点是祖父节点的右节点:(那么叔叔节点是祖父节点的左孩子)
@叔叔节点存在并且为红色的:那么叔叔节点必然为祖父节点的左节点,并且祖父节点肯定是黑色的(根据红黑树的第二条性质得出),这时候就只需要将父节点与叔叔节点设置为黑色,将祖父节点设置为红色即可
@叔叔节点不存在或者为黑色:如果孩子节点是父节点的右孩子那么将祖父与父亲的颜色交换然后以祖父为轴左旋;如果孩子节点是父节点的左孩子,那么将祖父节点与子节点的颜色交换,然后先以父节点为轴右旋,再以祖父节点为轴左旋
第三种情况总体要写在一个循环里,因为祖父节点的颜色变化了会影响上层节点。
我觉得只要以这种思路,红黑树节点的插入就会很简单。
下面是我的代码,稍后我会上传测试函数:
<span style="font-size:18px;">#pragma once
enum Nodecol
{
BLACK,
RED
};
template <typename K,typename V>
struct RBTreeNode
{
K _key;
V _value;
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
Nodecol _col;
RBTreeNode(const K& key, const V& value)
:_key(key)
, _value(value)
, _left(NULL)
, _right(NULL)
, _parent(NULL)
, _col(RED)
{}
};
template <typename K,typename V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
public:
RBTree()
:_root(NULL)
{}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
void _InOrder(Node* root)
{
if (root == NULL)
return;
_InOrder(root->_left);
cout << root->_key << endl;
_InOrder(root->_right);
}
bool Insert(const K& key, const V& value)
{
//情况一,新节点为根节点
if (_root == NULL)
{
_root = new Node(key, value);
_root->_col = BLACK;
return true;
}
//插入节点到找到的位置
Node* root = _root;
Node* parent = NULL;
while (root)
{
if (root->_key > key)
{
parent = root;
root = root->_left;
}
else if (root->_key < key)
{
parent = root;
root = root->_right;
}
else
return false;
}
Node* cur = new Node(key, value);
if (parent->_key>key)
parent->_left = cur;
else
parent->_right = cur;
cur->_parent = parent;
//情况二,父节点是黑色的,直接返回
if (cur->_parent->_col == BLACK)
return true;
//情况三:父节点是红色的
while (cur->_parent&&cur->_parent->_col == RED)
{
parent = cur->_parent;
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;
}
else//叔叔节点是黑色或者不存在
{
if (cur == parent->_right)//
{
RotateL(parent);
cur->_col = BLACK;
grandfather->_col = RED;
RotateR(grandfather);
}
else
{
parent->_col = BLACK;
grandfather->_col = RED;
RotateR(grandfather);
}
}
}
else
{ //叔叔节点为祖父节点的左节点
Node* uncle = grandfather->_left;
if (uncle&&uncle->_col == RED)//叔叔节点存在并且为红
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
}
else//叔叔节点不存在或者为黑
{
if (cur == parent->_left)
{
RotateR(parent);
cur->_col = BLACK;
grandfather->_col = RED;
RotateL(grandfather);
}
else
{
parent->_col = BLACK;
grandfather->_col = RED;
RotateL(grandfather);
}
}
}
cur = grandfather;
}
_root->_col = BLACK;
return true;
}</span>
<span style="font-size:18px;"> void RotateR(Node*& parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
if (subL)
{
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 = NULL;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
}
}
void RotateL(Node*& parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
if (subR)
{
parent->_right = subRL;
}
if (subRL)
{
subRL->_parent = parent;
}
subR->_left = parent;
Node* ppNode = parent->_parent;
parent->_parent = subR;
if (_root == parent)
{
_root = subR;
subR->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subR;
}
else
{
ppNode->_right = subR;
}
subR->_parent = ppNode;
}
}
protected:
Node* _root;
};
void Test()
{
RBTree<int, int> rb;
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
for (size_t i = 0; i < 10; ++i)
{
rb.Insert(a[i], i);
}
rb.InOrder();
}</span>
要检查一个红黑树是否平衡,我们需要一个函数来检测一下:
bool IsBalanceRBTree()
{
if (_root == NULL)
return true;;
if (_root->_col == RED)//条件一
return false;
if (!_NoTwoRed(_root))//条件二
return false;
Node* root = _root;
int count = 0;
while(root)
{
if (root->_col == BLACK)
count++;
root = root->_left;
}
int key = 0;
return _IsBalaceRBTree(_root, count,key);//条件四
}
bool _IsBalaceRBTree(Node* root, int& count,int key)
{
if (root == NULL)
return true;
if (root->_col == BLACK)
{
key++;
}
if (root->_left == NULL&&root->_right == NULL)
{
if (key != count)
return false;
else
return true;
}
return _IsBalaceRBTree(root->_left, count,key) && _IsBalaceRBTree(root->_right,count,key);
}
bool _NoTwoRed(Node* root)
{
if (root == NULL)
return true;
Node* parent = root->_parent;
if (parent && (parent->_col == root->_col == RED))
{
return false;
}
else
return true;
_NoTwoRed(root->_left);
_NoTwoRed(root->_right);
}
结果: