前置知识:RBTree(红黑树)
现已完成删除功能
map和set的底层结构就是红黑树,所以直接在map和set中封装一棵红黑树,然后包装下其接口即可。
template<class T, class Ref, class Ptr>
struct __RBTreeIterator
{
typedef RBTreeNode<T> Node;
typedef __RBTreeIterator<T, Ref, Ptr> Self;
Node* _node;
__RBTreeIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
bool operator==(const Self& other) const
{
return _node == other._node;
}
Self& operator++()
{
if (_node->_right)
{
// 下一个,右树最左节点
Node* leftMin = _node->_right;
while (leftMin->_left)
{
leftMin = leftMin->_left;
}
_node = leftMin;
}
else
{
// 下一个,孩子等于父亲左的那个祖先
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = parent->_parent;
}
_node = parent;
}
return *this;
}
};
Iterator Begin()
{
Node* leftMin = _root;
while (leftMin && leftMin->_left)
{
leftMin = leftMin->_left;
}
return Iterator(leftMin);
}
Iterator End()
{
return Iterator(nullptr);
}
ConstIterator End() const
{
return ConstIterator(nullptr);
}
ConstIterator Begin() const
{
Node* leftMin = _root;
while (leftMin && leftMin->_left)
{
leftMin = leftMin->_left;
}
return ConstIterator(leftMin);
}
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 __RBTreeIterator
{
typedef RBTreeNode<T> Node;
typedef __RBTreeIterator<T, Ref, Ptr> Self;
Node* _node;
__RBTreeIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
bool operator==(const Self& other) const
{
return _node == other._node;
}
Self& operator++()
{
if (_node->_right)
{
// 下一个,右树最左节点
Node* leftMin = _node->_right;
while (leftMin->_left)
{
leftMin = leftMin->_left;
}
_node = leftMin;
}
else
{
// 下一个,孩子等于父亲左的那个祖先
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = parent->_parent;
}
_node = parent;
}
return *this;
}
};
template<class K, class T, class KeyOfT>
class RBTree
{
typedef RBTreeNode<T> Node;
public:
typedef __RBTreeIterator<T, T&, T*> Iterator;
typedef __RBTreeIterator<T, const T&, const T*> ConstIterator;
RBTree() = default;
RBTree(const RBTree<K, T, KeyOfT>& t)
{
_root = Copy(t._root);
}
// t2 = t1
RBTree<K, T, KeyOfT>& operator=(RBTree<K, T, KeyOfT> t)
{
swap(_root, t._root);
return *this;
}
~RBTree()
{
Destroy(_root);
_root = nullptr;
}
Iterator Begin()
{
Node* leftMin = _root;
while (leftMin && leftMin->_left)
{
leftMin = leftMin->_left;
}
return Iterator(leftMin);
}
Iterator End()
{
return Iterator(nullptr);
}
ConstIterator End() const
{
return ConstIterator(nullptr);
}
ConstIterator Begin() const
{
Node* leftMin = _root;
while (leftMin && leftMin->_left)
{
leftMin = leftMin->_left;
}
return ConstIterator(leftMin);
}
Iterator Find(const K& key)
{
KeyOfT kot;
Node* cur = _root;
while (cur)
{
if (kot(cur->_data) < key)
{
cur = cur->_right;
}
else if (kot(cur->_data) > key)
{
cur = cur->_left;
}
else
{
return Iterator(cur);
}
}
return End();
}
std::pair<Iterator, bool> Insert(const T& data)
{
if (_root == nullptr)
{
_root = new Node(data);
_root->_col = BLACK;
return make_pair(Iterator(_root), true);
}
KeyOfT kot;
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
// K
// pair
// kot对象,是用来取T类型的data对象中的key
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(Iterator(cur), false);
}
}
cur = new Node(data);
Node* newnode = cur;
cur->_col = RED; // 新增节点给红色
if (kot(parent->_data) < kot(data))
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
cur->_parent = parent;
// parent的颜色是黑色也结束
while (parent && 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->_left)
{
// g
// p u
// c
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g
// p u
// c
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else
{
Node* uncle = grandfather->_left;
// 叔叔存在且为红,-》变色即可
if (uncle && uncle->_col == RED)
{
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
// 继续往上处理
cur = grandfather;
parent = cur->_parent;
}
else // 叔叔不存在,或者存在且为黑
{
// 情况二:叔叔不存在或者存在且为黑
// 旋转+变色
// g
// u p
// c
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(Iterator(newnode), true);
}
// 新增:删除接口,按键删除
bool Erase(const K& key)
{
Iterator it = Find(key);
if (it == End())
return false;
Node* z = it._node;
Node* y = z; // 真正被删除的节点或移位的节点
Colour originalColor = y->_col;
Node* x = nullptr; // 用于替换 y 的子节点
Node* xParent = nullptr; // 记录 x 的父节点
if (z->_left == nullptr)
{
x = z->_right;
xParent = z->_parent;
transplant(z, z->_right);
}
else if (z->_right == nullptr)
{
x = z->_left;
xParent = z->_parent;
transplant(z, z->_left);
}
else
{
y = minimum(z->_right);
originalColor = y->_col;
x = y->_right;
if (y->_parent == z)
{
if (x)
x->_parent = y;
xParent = y;
}
else
{
transplant(y, y->_right);
y->_right = z->_right;
if (y->_right)
y->_right->_parent = y;
xParent = y->_parent;
}
transplant(z, y);
y->_left = z->_left;
if (y->_left)
y->_left->_parent = y;
y->_col = z->_col;
}
delete z;
if (originalColor == BLACK)
eraseFixUp(x, xParent);
return true;
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
Node* ppNode = parent->_parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
_root->_parent = nullptr;
}
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;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
subR->_left = parent;
Node* ppNode = parent->_parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
_root->_parent = nullptr;
}
else
{
if (ppNode->_right == parent)
{
ppNode->_right = subR;
}
else
{
ppNode->_left = subR;
}
subR->_parent = ppNode;
}
}
// 中序遍历(假定 T 为 pair 类型,可以打印 first 与 second)
void InOrder()
{
_InOrder(_root);
cout << endl;
}
// 检查红黑树性质
bool IsBalance()
{
if (_root && _root->_col == RED)
return false;
int refNum = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLACK)
++refNum;
cur = cur->_left;
}
return Check(_root, 0, refNum);
}
private:
Node* Copy(Node* root)
{
if (root == nullptr)
return nullptr;
Node* newroot = new Node(root->_data);
newroot->_col = root->_col;
newroot->_left = Copy(root->_left);
if (newroot->_left)
newroot->_left->_parent = newroot;
newroot->_right = Copy(root->_right);
if (newroot->_right)
newroot->_right->_parent = newroot;
return newroot;
}
void Destroy(Node* root)
{
if (root == nullptr)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
root = nullptr;
}
// 检查黑色节点数量是否一致
bool Check(Node* root, int blackNum, const int refNum)
{
if (root == nullptr)
{
if (refNum != blackNum)
{
cout << "存在黑色节点数量不一致的路径" << endl;
return false;
}
return true;
}
if (root->_col == RED && root->_parent && root->_parent->_col == RED)
return false;
if (root->_col == BLACK)
blackNum++;
return Check(root->_left, blackNum, refNum) &&
Check(root->_right, blackNum, refNum);
}
// 中序遍历辅助函数
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
// 假定 T 为 pair 类型,如 pair
cout << root->_data.first << ":" << root->_data.second << " ";
_InOrder(root->_right);
}
// 以下是删除操作所需的辅助函数
// 用 v 替换 u 子树(不改变 v 的左右孩子)
void transplant(Node* u, Node* v)
{
if (u->_parent == nullptr)
_root = v;
else if (u == u->_parent->_left)
u->_parent->_left = v;
else
u->_parent->_right = v;
if (v)
v->_parent = u->_parent;
}
// 返回以 node 为根的子树中最小节点
Node* minimum(Node* node)
{
while (node && node->_left)
node = node->_left;
return node;
}
// 删除后修正红黑树性质
void eraseFixUp(Node* x, Node* parent)
{
// 注意:空指针视为黑色节点
while ((x == nullptr || x->_col == BLACK) && x != _root)
{
if (x == parent->_left)
{
Node* w = parent->_right;
if (w == nullptr)
{
x = parent;
parent = x->_parent;
continue;
}
if (w->_col == RED)
{
w->_col = BLACK;
parent->_col = RED;
RotateL(parent);
w = parent->_right;
}
if ((w->_left == nullptr || w->_left->_col == BLACK) &&
(w->_right == nullptr || w->_right->_col == BLACK))
{
w->_col = RED;
x = parent;
parent = x->_parent;
}
else
{
if (w->_right == nullptr || w->_right->_col == BLACK)
{
if (w->_left)
w->_left->_col = BLACK;
w->_col = RED;
RotateR(w);
w = parent->_right;
}
w->_col = parent->_col;
parent->_col = BLACK;
if (w->_right)
w->_right->_col = BLACK;
RotateL(parent);
x = _root;
break;
}
}
else // 对称情况:x 为右孩子
{
Node* w = parent->_left;
if (w == nullptr)
{
x = parent;
parent = x->_parent;
continue;
}
if (w->_col == RED)
{
w->_col = BLACK;
parent->_col = RED;
RotateR(parent);
w = parent->_left;
}
if ((w->_right == nullptr || w->_right->_col == BLACK) &&
(w->_left == nullptr || w->_left->_col == BLACK))
{
w->_col = RED;
x = parent;
parent = x->_parent;
}
else
{
if (w->_left == nullptr || w->_left->_col == BLACK)
{
if (w->_right)
w->_right->_col = BLACK;
w->_col = RED;
RotateL(w);
w = parent->_left;
}
w->_col = parent->_col;
parent->_col = BLACK;
if (w->_left)
w->_left->_col = BLACK;
RotateR(parent);
x = _root;
break;
}
}
}
if (x)
x->_col = BLACK;
}
private:
Node* _root = nullptr;
//size_t _size = 0;
};
namespace my_std
{
template<class K, class V>
class map
{
struct MapKeyOfT
{
const K& operator()(const pair<K, V>& kv)
{
return kv.first;
}
};
public:
//在没有示例化的类模板里取内嵌类型的时候要加typename
//typename 让编译器等到实例化后再识别这个类型
typedef typename RBTree<K, std::pair<const K, V>, MapKeyOfT>::Iterator iterator;
typedef typename RBTree<K, const K, MapKeyOfT>::ConstIterator const_iterator;
const_iterator begin() const
{
return _t.Begin();
}
const_iterator end() const
{
return _t.End();
}
iterator begin()
{
return _t.Begin();
}
iterator end()
{
return _t.End();
}
iterator find(const K& key)
{
return _t.Find(key);
}
std::pair<iterator, bool> insert(const std::pair<K, V>& kv)
{
return _t.Insert(kv);
}
bool erase(const K& key) {
return _t.Erase(key);
}
V& operator[](const K& key)
{
pair<iterator, bool> ret = _t.Insert(make_pair(key, V()));
return ret.first->second;
}
private:
RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};
namespace my_std
{
template<class K>
class set
{
struct SetKeyOfT
{
const K& operator()(const K& key)
{
return key;
}
};
public:
typedef typename RBTree<K, const K, SetKeyOfT>::Iterator iterator;
typedef typename RBTree<K, const K, SetKeyOfT>::ConstIterator const_iterator;
const_iterator begin() const
{
return _t.Begin();
}
const_iterator end() const
{
return _t.End();
}
iterator begin()
{
return _t.Begin();
}
iterator end()
{
return _t.End();
}
iterator find(const K& key)
{
return _t.Find(key);
}
pair<iterator, bool> insert(const K& key)
{
return _t.Insert(key);
}
bool erase(const K& key)
{
return _t.Erase(key);
}
private:
RBTree<K, const K, SetKeyOfT> _t;
};