发布时间:2023-11-11 11:30
一、红黑树的性质
enum COLOR
{
BLACK,
RED
};
template
struct RBTreeNode
{
RBTreeNode(const K& key, const V& value, COLOR color = RED)
:_pLeft(NULL)
, _pRight(NULL)
, _pParent(NULL)
, _key(key)
, _value(value)
, _color(color)
{}
RBTreeNode *_pLeft;
RBTreeNode *_pRight;
RBTreeNode *_pParent;
K _key;
V _value;
COLOR _color;
};
bool Insert(const K& key, const V& value)
{
if (NULL == _pRoot)
{
_pRoot = new Node(key, value, BLACK); //根节点为黑色
return true;
}
//找插入位置
pNode pCur = _pRoot;
pNode pParent = NULL;
while (pCur)
{
if (key < pCur->_key)
{
pParent = pCur;
pCur = pCur->_pLeft;
}
else if (key > pCur->_key)
{
pParent = pCur;
pCur = pCur->_pRight;
}
else
{
return false;
}
}
pCur = new Node(key, value);
if (key < pParent->_key)
pParent->_pLeft = pCur;
else
pParent->_pRight = pCur;
pCur->_pParent = pParent;
//有可能会违反红黑树的性质---3
while (pParent&&pParent->_color == RED)
{
pNode grandParent = pParent->_pParent;
if (pParent == grandParent->_pLeft)
{
pNode uncle = grandParent->_pRight;
//情况一,叔叔节点存在且为红
if (uncle&&uncle->_color == RED)
{
pParent->_color = BLACK;
uncle->_color = BLACK;
grandParent->_color = RED;
pCur = grandParent; //继续向上调整
pParent = pCur->_pParent;
}
//叔叔不存在或者存在且为黑
else
{
//若是第三种情况转化为第二种
if (pCur == pParent->_pRight)
{
RotateL(pParent);
swap(pParent, pCur);
}
RotateR(grandParent);
pParent->_color = BLACK;
grandParent->_color = RED;
break;
}
}
//父亲节点为双亲的右节点
else
{
pNode uncle = grandParent->_pLeft;
//情况一,叔叔节点存在且为红
if (uncle&&uncle->_color == RED)
{
pParent->_color = BLACK;
uncle->_color = BLACK;
grandParent->_color = RED;
pCur = grandParent; //继续向上调整
pParent = pCur->_pParent;
}
//叔叔不存在或者存在且为黑
else
{
//若是第三种情况转化为第二种
if (pCur == pParent->_pLeft)
{
RotateR(pParent);
swap(pParent, pCur);
}
RotateL(grandParent);
pParent->_color = BLACK;
grandParent->_color = RED;
}
}
}
_pRoot->_color = BLACK;
return true;
}
bool IsRBTree()
{
if (_pRoot == NULL)
return true;
if (_pRoot->_color == RED) //根节点为红色
return false;
int blackNum = 0; //黑色节点的个数
int num = 0; //任意路径的黑色节点个数
pNode pCur = _pRoot;
while (pCur)
{
if (pCur->_color == BLACK)
{
blackNum++;
}
pCur = pCur->_pLeft;
}
return _IsRBTree(_pRoot, blackNum, num);
}
bool _IsRBTree(pNode pRoot, const int blackNum, int num)
{
if (NULL == pRoot) //一条路径结束
{
if (num != blackNum)
{
return false;
}
else
{
return true;
}
}
if (pRoot->_color == BLACK)
{
++num;
}
//两个相邻的红色节点
if ((pRoot->_color == RED) && (pRoot->_pParent->_color == RED))
{
return false;
}
return _IsRBTree(pRoot->_pLeft, blackNum, num) && _IsRBTree(pRoot->_pRight, blackNum, num);
}
#pragma once
enum COLOR
{
BLACK,
RED
};
template
struct RBTreeNode
{
RBTreeNode(const K& key, const V& value, COLOR color = RED)
:_pLeft(NULL)
, _pRight(NULL)
, _pParent(NULL)
, _key(key)
, _value(value)
, _color(color)
{}
RBTreeNode *_pLeft;
RBTreeNode *_pRight;
RBTreeNode *_pParent;
K _key;
V _value;
COLOR _color;
};
template
class RBTree
{
typedef RBTreeNode Node;
typedef Node* pNode;
public:
RBTree()
:_pRoot(NULL)
{}
bool Insert(const K& key, const V& value)
{
if (NULL == _pRoot)
{
_pRoot = new Node(key, value, BLACK); //根节点为黑色
return true;
}
//找插入位置
pNode pCur = _pRoot;
pNode pParent = NULL;
while (pCur)
{
if (key < pCur->_key)
{
pParent = pCur;
pCur = pCur->_pLeft;
}
else if (key > pCur->_key)
{
pParent = pCur;
pCur = pCur->_pRight;
}
else
{
return false;
}
}
pCur = new Node(key, value);
if (key < pParent->_key)
pParent->_pLeft = pCur;
else
pParent->_pRight = pCur;
pCur->_pParent = pParent;
//有可能会违反红黑树的性质---3
while (pParent&&pParent->_color == RED)
{
pNode grandParent = pParent->_pParent;
if (pParent == grandParent->_pLeft)
{
pNode uncle = grandParent->_pRight;
//情况一,叔叔节点存在且为红
if (uncle&&uncle->_color == RED)
{
pParent->_color = BLACK;
uncle->_color = BLACK;
grandParent->_color = RED;
pCur = grandParent; //继续向上调整
pParent = pCur->_pParent;
}
//叔叔不存在或者存在且为黑
else
{
//若是第三种情况转化为第二种
if (pCur == pParent->_pRight)
{
RotateL(pParent);
swap(pParent, pCur);
}
RotateR(grandParent);
pParent->_color = BLACK;
grandParent->_color = RED;
break;
}
}
//父亲节点为双亲的右节点
else
{
pNode uncle = grandParent->_pLeft;
//情况一,叔叔节点存在且为红
if (uncle&&uncle->_color == RED)
{
pParent->_color = BLACK;
uncle->_color = BLACK;
grandParent->_color = RED;
pCur = grandParent; //继续向上调整
pParent = pCur->_pParent;
}
//叔叔不存在或者存在且为黑
else
{
//若是第三种情况转化为第二种
if (pCur == pParent->_pLeft)
{
RotateR(pParent);
swap(pParent, pCur);
}
RotateL(grandParent);
pParent->_color = BLACK;
grandParent->_color = RED;
}
}
}
_pRoot->_color = BLACK;
return true;
}
bool IsRBTree()
{
if (_pRoot == NULL)
return true;
if (_pRoot->_color == RED) //根节点为红色
return false;
int blackNum = 0; //黑色节点的个数
int num = 0; //任意路径的黑色节点个数
pNode pCur = _pRoot;
while (pCur)
{
if (pCur->_color == BLACK)
{
blackNum++;
}
pCur = pCur->_pLeft;
}
return _IsRBTree(_pRoot, blackNum, num);
}
void InOrder()
{
if (NULL == _pRoot)
return;
_InOrder(_pRoot);
}
private:
bool _IsRBTree(pNode pRoot, const int blackNum, int num)
{
if (NULL == pRoot) //一条路径结束
{
if (num != blackNum)
{
return false;
}
else
{
return true;
}
}
if (pRoot->_color == BLACK)
{
++num;
}
//两个相邻的红色节点
if ((pRoot->_color == RED) && (pRoot->_pParent->_color == RED))
{
return false;
}
return _IsRBTree(pRoot->_pLeft, blackNum, num) && _IsRBTree(pRoot->_pRight, blackNum, num);
}
//左单旋
void RotateL(pNode pParent)
{
pNode pSubR = pParent->_pRight;
pNode pSubRL = pSubR->_pLeft;
//处理pSubR
pParent->_pRight = pSubRL;
if (pSubRL)
pSubRL->_pParent = pParent;
pSubR->_pLeft = pParent;
pNode pPParent = pParent->_pParent;
pSubR->_pParent = pPParent;
pParent->_pParent = pSubR;
//判断是否为根
if (pParent == _pRoot)
_pRoot = pSubR;
//判断是左子树还是右子树
else
{
if (pPParent->_pLeft == pParent)
pPParent->_pLeft = pSubR;
else
pPParent->_pRight = pSubR;
}
}
//右单旋
void RotateR(pNode pParent)
{
pNode pSubL = pParent->_pLeft;
pNode pSubLR = pSubL->_pRight;
//处理pSubR
pParent->_pLeft = pSubLR;
if (pSubLR)
pSubLR->_pParent = pParent;
pSubL->_pRight = pParent;
pNode pPParent = pParent->_pParent;
pSubL->_pParent = pPParent;
pParent->_pParent = pSubL;
//判断是否为根
if (pParent == _pRoot)
_pRoot = pSubL;
//判断是左子树还是右子树
else
{
if (pPParent->_pLeft == pParent)
pPParent->_pLeft = pSubL;
else
pPParent->_pRight = pSubL;
}
}
void _InOrder(pNode pRoot)
{
if (pRoot)
{
_InOrder(pRoot->_pLeft);
cout << "<" << pRoot->_key << "," << pRoot->_color << ">" << endl;
_InOrder(pRoot->_pRight);
}
}
private:
pNode _pRoot;
};
void RBTreeTest()
{
int array[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
RBTree t;
for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++)
{
t.Insert(array[i], i);
}
t.InOrder();
if (t.IsRBTree())
cout << "是红黑树" << endl;
else
cout << "不是红黑树" << endl;
}