红黑树是一棵二叉搜索树,它在每个节点上增加了一个存储位来表示节点的颜色,可以是Red或Black。
通过对任何一条从根到叶子简单路径上的颜色来约束,红黑树保证最长路径不超过最短路径的两倍,因而近似于平衡。
红黑树是满足下面红黑性质的二叉搜索树:
1. 每个节点,不是红色就是黑色的
2. 根节点是黑色的
3. 如果一个节点是红色的,则它的两个子节点是黑色的
4. 对每个节点,从该节点到其所有后代叶节点的简单路径上,均包含相同数目的黑色节点。
代码实现如下:
#pragma once
#include <iostream>
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;
Color _color;
K _key;
V _value;
RBTreeNode(const K& key, const V& value)
:_key(key)
, _value(value)
, _color(RED) //每次插入红色节点
, _left(NULL)
, _right(NULL)
, _parent(NULL)
{}
};
template <class K, class V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
public:
RBTree() //构造
:_root(NULL)
{}
~RBTree() //析构
{}
bool Insert(const K& key, const V& value) //插入
{
if (_root == NULL) //为空时,插入一个节点,为黑色
{
_root = new Node(key, value);
_root->_color = BLACK;
return true;
}
//不为空时
Node* parent = NULL;
Node* cur = _root;
while (cur)
{
if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else
{
cout << "找到相同的key值" << endl;
return false;
}
}
//找到位置,开始插入
cur = new Node(key, value);
if (parent->_key < key)
{
parent->_right = cur;
cur->_parent = parent;
}
else if (parent->_key > key)
{
parent->_left = cur;
cur->_parent=parent;
}
//插入结束,调整颜色
while (parent && parent->_color == RED) //插入cur后,parent如果为黑色,不用调整
{
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
//1.unclue存在且为红色
if (uncle && uncle->_color == RED)
{
//此时不用旋转,只调整颜色
parent->_color = uncle->_color = BLACK;
grandfather->_color = RED;
cur = grandfather; //继续向上调整,直到不是红色节点,停止
parent = cur->_parent;
}
//2.uncle不存在或uncle为黑
else if (uncle == NULL || uncle->_color == BLACK)
{
if (cur == parent->_right)
{
//双旋
RotateL(parent); //先左旋
swap(parent, cur);
}
RotateR(grandfather); //右旋
parent->_color = BLACK;
grandfather->_color = RED;
break;
}
}
else if (parent == grandfather->_right)
{
Node* uncle = grandfather->_left;
//1.uncle存在且为红
if (uncle && uncle->_color == RED)
{
//不用旋转,只需要变色处理
parent->_color = uncle->_color = BLACK;
grandfather->_color = RED;
cur = grandfather; //继续往上遍历,直到遇到黑色节点停止
parent = cur->_parent;
}
//2.uncle不存在或uncle为黑
else if (uncle == NULL || uncle->_color==BLACK)
{
if (cur == parent->_left)
{
RotateR(parent);
swap(parent, cur);
}
RotateL(grandfather);
grandfather->_color = RED;
parent->_color = BLACK;
break;
}
}
}
_root->_color = BLACK;
return true;
}
void RotateL(Node* parent) //左单旋
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
subR->_left = parent;
Node* pparent = parent->_parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
subR->_parent = NULL;
}
else
{
if (parent == pparent->_left)
{
pparent->_left = subR;
subR->_parent = pparent;
}
else
{
pparent->_right = subR;
subR->_parent = pparent;
}
}
}
void RotateR(Node* parent) //右单旋
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
subL->_right = parent;
Node* pparent = parent->_parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
subL->_parent = NULL;
}
else
{
if (parent == pparent->_left)
{
pparent->_left = subL;
subL->_parent = pparent;
}
else
{
pparent->_right = subL;
subL->_parent = pparent;
}
}
}
void InOrder() //中序遍历
{
_InOrder(_root);
cout << endl;
}
bool IsBlance() //判断是否平衡
{
if (_root == NULL)
{
return true;
}
if (_root->_color == RED)
{
return false;
}
int Blackcount = 0;
Node* left = _root;
while (left)
{
if (left->_color == BLACK)
{
++Blackcount;
}
left = left->_left;
}
int num = 0;
return _IsBlance(_root, Blackcount, num);
}
protected:
void _InOrder(Node* root)
{
if (root == NULL)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
bool _IsBlance(Node* root, const int Blackcount, int num)
{
if (root == NULL)
{
if (num != Blackcount)
{
cout << "黑色节点的数量不相等" << endl;
return false;
}
else
{
return true;
}
}
if (root->_color == RED && root->_parent->_color == RED)
{
cout << "有连续的红节点" << root->_key << endl;
return false;
}
if (root->_color == BLACK)
{
++num;
}
return _IsBlance(root->_left, Blackcount, num) && _IsBlance(root->_right, Blackcount, num);
}
protected:
Node* _root;
};
#include "RBTree.h"
void test()
{
RBTree<int, int> pp;
int a[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
pp.Insert(a[i], i);
}
pp.InOrder();
cout << "是否平衡:" << pp.IsBlance() << endl;
}
int main()
{
test();
return 0;
}