?
?新增节点的颜色默认给红色
因为新增节点若为黑色节点,插入后会影响所有路径(红黑树的性质规定每条路径必须有相同数量的黑色节点)
而新增插入红色节点只会影响父节点,(父子节点的组合:黑+黑,黑+红,红+黑)
(若父节点为黑,则无影响,若父节点为红,则有连续的红节点,需要调整,下面会讲)
enum Colour
{
RED,
BLACK
};
template<class T> // T可以是set的K,可以是map的pair<K,V>
struct RBTreeNode
{
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
T _data;
Colour _col;
RBTreeNode(const T& data)
:_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
,_data(data)
,_col(RED)//新增节点默认给红色
{}
};
情况二: cur为红,p为红,g为黑,u不存在/u存在且为黑 ?
在这种情况下,单纯变色无法解决问题,需要旋转+变色
解决方案:旋转(单选/双旋)+变色
需要双旋时的情况:
pair<Node*, bool> Insert(const T& data)
{
//插入一个红色节点
if (_root == nullptr)
{
_root = new Node(data);
_root->_col = BLACK;
return make_pair(_root, true);
}
Node* cur = _root;
Node* parent = nullptr;
KeyOfT kot;
while (cur)
{
if (kot(cur->_data) < kot(data))
{
parent = cur;
cur = cur->_right;
}
else if (kot(cur->_data) > kot(data))
{
parent = cur;
cur = cur->_left;
}
else
{
return make_pair(cur, false);
}
}
//新增节点给红色
cur = new Node(data);
Node* newnode = cur;
if (kot(parent->_data)>kot(data))
{
parent->_left = cur;
cur->_parent = parent;
}
else
{
parent->_right = cur;
cur->_parent = parent;
}
//红黑树调整--有连续的红节点
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)
{
// g
// p u
// c
Node* uncle = grandfather->_right;
if (uncle && uncle->_col == RED)//uncle存在且为红--变色
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
//继续向上调整
cur = grandfather;
parent = cur->_parent;
}
else//uncle不存在或者存在且为黑--旋转+变色
{
if (cur == parent->_left)//右单旋
{
// g
// p
// c
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else//左右双旋
{
// g
// p
// c
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else//parent == grandfather->_right
{
// g
// u p
// c
Node* uncle = grandfather->_left;
if (uncle && uncle->_col == RED)//uncle存在且为红--变色
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
//继续向上调整
cur = grandfather;
parent = cur->_parent;
}
else//uncle不存在或者存在且为黑--旋转+变色
{
if (cur == parent->_right)//左单旋
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else//右左双旋
{
// g
// u p
// c
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return make_pair(newnode, true);
}
需要用到的左单旋 右单旋:(在AVL数的代码实现中有具体讲解)
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node*parentParent = parent->_parent;
parent->_parent = subR;
subR->_left = parent;
if (_root == parent)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subR;
}
else
{
parentParent->_right = subR;
}
subR->_parent = parentParent;
}
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* parentParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (_root == parent)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subL;
}
else
{
parentParent->_right = subL;
}
subL->_parent = parentParent;
}
}
bool IsBalance()
{
//检查根节点
if (_root == nullptr)
return true;
if (_root->_col == RED)
return false;
//检查是否有连续的红节点+每条路径的黑色节点数目是否一样
int refVal = 0;//参考值
Node* cur = _root;
while (cur)//以最左边的路径上的黑色节点数目为参考值
{
if (cur->_col == BLACK)
refVal++;
cur = cur->_left;
}
int blacknum = 0;
return Check(_root, refVal, blacknum);
}
bool Check(Node* root, const int refVal,int blacknum)
{
if (root == nullptr)
{
if (blacknum != refVal)
{
cout << "存在黑色节点数量不相等的路径" << endl;
return false;
}
return true;
}
if (root->_col == BLACK)//节点为黑色--统计
{
blacknum++;
}
if(root->_col == RED && root->_parent->_col == RED)//节点为红色--检查
{
cout << "有连续的红色节点" << endl;
return false;
}
return Check(root->_left, refVal, blacknum)
&& Check(root->_right, refVal, blacknum);
}
MyMap.h
#pragma once
#include"RBTree.h"
namespace djx
{
template<class K,class V>
class map
{
public:
struct MapKeyOfT//获取关键字K,map存储的是pair<K,V>
{
const K& operator()(const pair<K, V>&kv)
{
return kv.first;
}
};
// 对类模板取内嵌类型,加typename告诉编译器这里是类型
typedef typename RBTree<K, pair<const K, V> ,MapKeyOfT>::iterator iterator;
typedef typename RBTree<K, pair<const K, V> ,MapKeyOfT>::const_iterator const_iterator;
iterator begin()
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
V& operator[](const K&key)
{
pair<iterator, bool> ret = insert(make_pair(key, V()));
return ret.first->second;//ret.first是迭代器,能够找到节点
}
pair<iterator, bool> insert(const pair<K, V>&kv)
{
return _t.Insert(kv);
}
private:
RBTree<K, pair<const K, V> ,MapKeyOfT> _t;//封装红黑树
};
}
MySet.h
#pragma once
#include"RBTree.h"
namespace djx
{
template<class K>
class set
{
public:
struct SetKeyOfT//仿函数,返回关键字K,set存储的就是K
{
const K& operator()(const K& key)
{
return key;
}
};
typedef typename RBTree<K, K,SetKeyOfT>::const_iterator iterator;//set中的元素不可被修改,所以普通迭代器就用const_iterator来实现
typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator;
iterator begin()const
{
return _t.begin();
}
iterator end() const
{
return _t.end();
}
pair<iterator, bool> insert(const K& key)
{
return _t.Insert(key);
}
private:
RBTree<K, K, SetKeyOfT> _t;
};
}
RBTree.h
#pragma once
// set ->key
// map ->key/value
enum Colour
{
RED,
BLACK
};
template<class T>
struct RBTreeNode//节点
{
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
T _data;
Colour _col;
RBTreeNode(const T& data)
:_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
,_data(data)
,_col(RED)
{}
};
template<class T,class Ref,class Ptr>
struct __TreeIterator//迭代器
{
typedef RBTreeNode<T> Node;
typedef __TreeIterator<T, Ref, Ptr> Self;
Node* _node;
__TreeIterator(Node* node)
:_node(node)
{}
Ref operator* ()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
Self& operator++()
{
//顺序:左 中 右
if (_node->_right)//这颗子树没有走完--找右子树的最左节点
{
Node* cur = _node->_right;
while (cur->_left)
{
cur = cur->_left;
}
_node = cur;
}
else//这颗子树已经走完--找一个祖先(这个子树是它左孩子的祖先)
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && parent->_right == cur)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
bool operator!=(const Self& s)
{
return s._node != _node;
}
bool operator==(const Self& s)
{
return s._node == _node;
}
};
template<class K,class T,class KeyOfT>
class RBTree
{
typedef RBTreeNode<T> Node;
public:
typedef __TreeIterator<T, T&, T*> iterator;
typedef __TreeIterator<T, const T&, const T*> const_iterator;
iterator begin()//红黑树中序序列得到有序序列,begin()可设计成最左节点的迭代器
{
Node* cur = _root;
while (cur && cur->_left)
{
cur = cur->_left;
}
return iterator(cur);
}
iterator end()
{
return iterator(nullptr);
}
const_iterator begin()const
{
Node* cur = _root;
while (cur&& cur->_left)
{
cur = cur->_left;
}
return const_iterator(cur);
}
const_iterator end()const
{
return const_iterator(nullptr);
}
//返回值不能是pair<iterator, bool>,因为set的普通迭代器实际也是const_iterator,set设计insert时要返回的pair<iterator, bool> 实际是pair<const_iterator, bool> ,而封装红黑树,复用红黑树的Insert(返回值若是pair<iterator, bool>,红黑树的普通迭代器就是普通迭代器,那么因为普通迭代器iterator不能转成const_iterator,所以代码会报错)
设计成pair<Node*, bool>就很好,节点指针Node*可以通过const_iterator迭代器的构造函数完成转变
pair<Node*, bool> Insert(const T& data)
{
//插入一个红色节点
if (_root == nullptr)
{
_root = new Node(data);
_root->_col = BLACK;
return make_pair(_root, true);
}
Node* cur = _root;
Node* parent = nullptr;
KeyOfT kot;
while (cur)
{
if (kot(cur->_data) < kot(data))
{
parent = cur;
cur = cur->_right;
}
else if (kot(cur->_data) > kot(data))
{
parent = cur;
cur = cur->_left;
}
else
{
return make_pair(cur, false);
}
}
//新增节点给红色
cur = new Node(data);
Node* newnode = cur;
if (kot(parent->_data)>kot(data))
{
parent->_left = cur;
cur->_parent = parent;
}
else
{
parent->_right = cur;
cur->_parent = parent;
}
//红黑树调整--有连续的红节点
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)
{
// g
// p u
// c
Node* uncle = grandfather->_right;
if (uncle && uncle->_col == RED)//uncle存在且为红--变色
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
//继续向上调整
cur = grandfather;
parent = cur->_parent;
}
else//uncle不存在或者存在且为黑--旋转+变色
{
if (cur == parent->_left)//右单旋
{
// g
// p
// c
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else//左右双旋
{
// g
// p
// c
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else//parent == grandfather->_right
{
// g
// u p
// c
Node* uncle = grandfather->_left;
if (uncle && uncle->_col == RED)//uncle存在且为红--变色
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
//继续向上调整
cur = grandfather;
parent = cur->_parent;
}
else//uncle不存在或者存在且为黑--旋转+变色
{
if (cur == parent->_right)//左单旋
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else//右左双旋
{
// g
// u p
// c
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return make_pair(newnode, true);
}
iterator Find(const K& key)
{
//...
return end();
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node*parentParent = parent->_parent;
parent->_parent = subR;
subR->_left = parent;
if (_root == parent)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subR;
}
else
{
parentParent->_right = subR;
}
subR->_parent = parentParent;
}
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* parentParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (_root == parent)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subL;
}
else
{
parentParent->_right = subL;
}
subL->_parent = parentParent;
}
}
bool IsBalance()//红黑树的验证
{
//检查根节点
if (_root == nullptr)
return true;
if (_root->_col == RED)
return false;
//检查是否有连续的红节点+每条路径的黑色节点数目是否一样
int refVal = 0;//参考值
Node* cur = _root;
while (cur)//以最左边的路径上的黑色节点数目为参考值
{
if (cur->_col == BLACK)
refVal++;
cur = cur->_left;
}
int blacknum = 0;
return Check(_root, refVal, blacknum);
}
bool Check(Node* root, const int refVal,int blacknum)
{
if (root == nullptr)
{
if (blacknum != refVal)
{
cout << "存在黑色节点数量不相等的路径" << endl;
return false;
}
return true;
}
if (root->_col == BLACK)//节点为黑色--统计
{
blacknum++;
}
if(root->_col == RED && root->_parent->_col == RED)//节点为红色--检查
{
cout << "有连续的红色节点" << endl;
return false;
}
return Check(root->_left, refVal, blacknum)
&& Check(root->_right, refVal, blacknum);
}
int Height()
{
return _Height(_root);
}
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;
}
size_t Size()
{
return _Size(_root);
}
size_t _Size(Node* root)
{
if (root == nullptr)
return 0;
return _Size(root->_left) + _Size(root->_right) + 1;
}
private:
Node* _root = nullptr;
};
处理设计红黑树Insert函数返回值的细节: