红黑树:红黑树是一棵二叉搜索树,它在每个节点上增加了一个存储位来表示节点的颜色,可以是Red或Black。通过对任何一条从根到叶子简单 路径上的颜色来约束,红黑树保证最长路径不超过最短路径的两倍,因而近似于平衡。
性质:1. 每个节点,不是红色就是黑色的
2. 根节点是黑色的
3. 如果一个节点是红色的,则它的两个子节点是黑色的
4. 对每个节点,从该节点到其所有后代叶节点的简单路径上,均包含相同数目的黑色节点
红黑树的插入有以下几种情况:
1.cur为红,p为红,g为黑,u存在且为红 则将p,u改为黑,g改为红,然后把g当成cur,继续向上调整
2.cur为红,p为红,g为黑,u不存在/u为黑
p为g的左孩子,cur为p的左孩子,则进行右单旋转;相反,p为g的右孩子,cur为p的右孩子,则进行左单旋转 p、g变色--p变黑,g变红
3.cur为红,p为红,g为黑,u不存在/u为黑
p为g的左孩子,cur为p的右孩子,则针对p做左单旋转;相反,p为g的右孩子,cur为p的左孩子,则针对p做右单旋转 则转换成了情况2
实现代码如下:
#include<iostream>
using namespace std;
enum Colour
{
RED,
BLACK,
};
template<class K, class V>
struct RBTreeNode
{
K _key;
V _value;
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
Colour _col;
RBTreeNode(const K& key, const V& value, Colour col = RED)
:_key(key)
, _value(value)
, _left(NULL)
, _right(NULL)
, _parent(NULL)
, _col(col)
{}
};
template<class K, class V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
public:
RBTree()
:_root(NULL)
{}
bool Insert(const K& key, const V& value)
{
if (_root == NULL)
{
_root = new Node(key, value, BLACK);
return true;
}
Node* parent = NULL;
Node* cur = _root;
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(key, value, RED);
if (parent->_key > key)
{
parent->_left = cur;
cur->_parent = parent;
}
else
{
parent->_right = cur;
cur->_parent = parent;
}
while (cur != _root && 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->_right)
{
_RotateL(parent);
swap(cur, parent);
}
_RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
}
else //parent == grandfather->right
{
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->_left)
{
_RotateR(parent);
swap(cur, parent);
}
_RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
}
}
_root->_col = BLACK;
return true;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool IsBalanceTree()
{
return _IsBalance(_root);
}
protected:
bool _IsBalance(Node* root)
{
if (root == NULL)
{
return true;
}
int lheight = _Height(root->_left);
int rheight = _Height(root->_right);
int bf = abs(rheight - lheight);
if (bf > 1)
{
return false;
}
return _IsBalance(root->_left) && _IsBalance(root->_right);
}
int _Height(Node *root)
{
if (root == NULL)
{
return 0;
}
int lheight = _Height(root->_left) + 1;
int rheight= _Height(root->_right) + 1;
return lheight > rheight ? lheight : rheight;
}
void _RotateL(Node* parent)
{
Node *subR = parent->_right;
Node *subRL = subR->_left;
parent->_right = subRL;
if (subRL != NULL)
{
subRL->_parent = parent;
}
subR->_left = parent;
subR->_parent = parent->_parent;
parent->_parent = subR;
parent = subR;
if (parent->_parent == NULL)
{
_root = parent;
}
else
{
if (parent->_parent->_key < parent->_key)
{
parent->_parent->_right = parent;
}
else
{
parent->_parent->_left = parent;
}
}
}
void _RotateR(Node* parent)
{
Node *subL = parent->_left;
Node *subLR = subL->_right;
parent->_left = subLR;
if (subLR != NULL)
{
subLR->_parent = parent;
}
subL->_right = parent;
subL->_parent = parent->_parent;
parent->_parent = subL;
parent = subL;
if (parent->_parent == NULL)
{
_root = parent;
}
else
{
if (parent->_parent->_key < parent->_key)
{
parent->_parent->_right = parent;
}
else
{
parent->_parent->_left = parent;
}
}
}
void _InOrder(Node *root)
{
if (root == NULL)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
private:
Node* _root;
};
int main()
{
RBTree<int, int> rb;
int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
rb.Insert(a[i], a[i]);
}
rb.InOrder();
cout<<rb.IsBalanceTree();
return 0;
}