hello~ 很高兴见到大家! 这次带来的是C++中关于set和map封装这部分的一些知识点,如果对你有所帮助的话,可否留下你宝贵的三连呢? 个 人 主 页: 默|笙

set和map的底层都是红黑树,但由于存储元素的差异(一个只存储key,一个既存储key又存储value),我们要么创造出两棵稍微不一样的红黑树,或者是改变红黑树的结构,使其能完美匹配上set和map。库里面采用了后者。
库里面RBTree的实现一共有5个模板参数,分别是 Key,Value,KeyOfValue,Compare和Alloc,我们主要分析前三个模板参数的作用)。Compare是控制比较规则的仿函数类型,Alloc是内存池。



我们在进行插入时,需要根据key的大小来找到插入的位置,而由于set和map存储类型的不同,set直接用Value(key)类型的元素就好,而map则需要取出Value(pair<const key, value>)里面的key值来找到插入的位置。



set:
namespace mosheng {
template<class K>
class identity
{
public:
K& operator()(const K& key)
{
return key;
}
};
template<class Key>
class set
{
typedef RBTree<Key, Key, identity<Key>> rbType;
};
}map:
namespace mosheng {
template<class K, class KV>
class select1st
{
public:
K& operator()(const KV& kv)
{
return kv.first;
}
};
template<class Key, class Value>
class map
{
typedef RBTree<Key, std::pair<Key, Value>, select1st<Key, std::pair<Key, Value>> rbType;
};
}#pragma once
enum Colour
{
RED,
BLACK
};
template<class Value>
struct RBTreeNode
{
RBTreeNode(Value& kv)
:_kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
{
};
Value _kv;
RBTreeNode<Value>* _left;
RBTreeNode<Value>* _right;
RBTreeNode<Value>* _parent;
Colour _col = RED;
};
template<class Key, class Value, class KeyOfValue>
class RBTree
{
typedef RBTreeNode<Value> Node;
public:
bool Insert(Value& kv)
{
//处理空树的情况
if (_root == nullptr)
{
_root = new Node(kv);
_root->_col = BLACK;
return true;
}
//找到要插入的位置
else
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (KeyOfValue()(kv) < KeyOfValue()(cur->_kv))
{
parent = cur;
cur = cur->_left;
}
else if (KeyOfValue()(kv) > KeyOfValue()(cur->_kv))
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
//插入新节点
cur = new Node(kv);
if (KeyOfValue()(parent->_kv) > KeyOfValue()(kv))
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
//父节点为红色的情况下需要进行处理
if (parent->_col == RED)
{
while (parent && parent->_col == RED)
{
//记录节点
Node* grandfather = parent->_parent;
Node* uncle = nullptr;
if (grandfather->_left == parent)
{
uncle = grandfather->_right;
}
else
{
uncle = grandfather->_left;
}
//uncle为红色的情况
//仅变色
if (uncle && uncle->_col == RED)
{
uncle->_col = parent->_col = BLACK;
grandfather->_col = RED;
//更新
cur = grandfather;
parent = cur->_parent;
}
//uncle为黑色或为空的情况
else
{
//右旋转
if (grandfather->_left == parent && parent->_left == cur)
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
break;
}
//左旋转
else if (grandfather->_right == parent && parent->_right == cur)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
break;
}//左右双旋
else if (grandfather->_left == parent && parent->_right == cur)
{
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
break;
}
//右左双旋
else
{
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
break;
}
}
}
}
}
//处理根节点颜色
_root->_col = BLACK;
return true;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
Node* Find(const Key& key)
{
Node* cur = _root;
while (cur)
{
if (KeyOfValue()(cur->_kv) < key)
{
cur = cur->_right;
}
else if (KeyOfValue()(cur->_kv) > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}
Node* _root = nullptr;
};迭代器的实现也就是在节点指针的基础之上做封装操作,并重载一些运算符。map和set的迭代器是双向迭代器,关键是需要实现++和–的重载。
template<class Value>
class TreeIterator
{
typedef RBTreeNode<Value> Node;
typedef TreeIterator<Value> Self;
public:
TreeIterator(Node* node)
:_node(node)
{}
Value& operator* ()
{
return _node->_kv;
}
Value* operator->()
{
return &(_node->_kv);
}
bool operator==(const Self& s)const
{
return _node == s->_node;
}
bool operator!=(const Self& s)const
{
return _node != s->_node;
}
private:
Node* _node;
};我们希望达成的效果是++从当前节点移动到下一个按照中序遍历的节点。- -则是反过来。++只需要知道当前节点的下一个节点是哪一个。


++重载实现
Self& operator++()
{
if (_node->_right)
{
Node* min = _node;
while (min->left)
{
min = min->_left
}
_node = min;
}
else
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && parent->_right == cur)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}相较于operator++,operator- - 的思路就是++的逆思路。
我们通过模板来实现对迭代器的复用,这样就不用单独再实现一个跟iterator十分有九分像的const_iterator迭代器了。
template<class Value, class Ref, class Ptr>
class TreeIterator
{
typedef RBTreeNode<Value> Node;
typedef TreeIterator<Value> Self;
public:
TreeIterator(Node* node)
:_node(node)
{}
Ref operator* ()
{
return _node->_kv;
}
Ref operator* ()const
{
return _node->_kv;
}
Ptr operator->()
{
return &(_node->_kv);
}
Ptr operator->()const
{
return &(_node->_kv);
}
bool operator==(const Self& s)const
{
return _node == s._node;
}
bool operator!=(const Self& s)const
{
return _node != s._node;
}
//++重载
Self& operator++()
{
if (_node->_right)
{
Node* min = _node->_right;
while (min->_left)
{
min = min->_left;
}
_node = min;
}
else
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && parent->_right == cur)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
private:
Node* _node;
};
template<class Key, class Value, class KeyOfValue>
class RBTree
{
typedef RBTreeNode<Value> Node;
typedef TreeIterator<Value, Value&, Value*> iterator;
typedef TreeIterator<Value, const Value&, const Value*> const_iterator;
public:
iterator begin()
{
Node* it = _root;
while (it->left)
{
it = it->_left;
}
return iterator(it);
}
const_iterator begin()const
{
Node* it = _root;
while (it->left)
{
it = it->_left;
}
return iterator(it);
}
iterator end()
{
return iterator(nullptr);
}
const_iterator end()const
{
return iterator(nullptr);
}然后实现Key不可修改的问题。在set和map头文件里传递要存储的类型的时候加上const就行。
就是在insert的基础之上进行实现,是insert的复用。
set:
#pragma once
#include "RBTree.h"
namespace mosheng {
template<class K>
class identity
{
public:
const K& operator()(const K& key)
{
return key;
}
};
template<class Key>
class set
{
typedef RBTree<Key, const Key, identity<Key>> rbType;
public:
typedef typename RBTree<Key, const Key, identity<Key>>::Iterator iterator;
typedef typename RBTree<Key, Key, identity<Key>>::const_Iterator const_iterator;
iterator begin()
{
return _t.begin();
}
const_iterator begin()const
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
const_iterator end()const
{
return _t.end();
}
pair<iterator, bool> insert(const Key& key)
{
return _t.Insert(key);
}
private:
rbType _t;
};
}map:
#pragma once
#include "RBTree.h"
namespace mosheng {
template<class K, class KV>
class select1st
{
public:
const K& operator()(const KV& kv)
{
return kv.first;
}
};
template<class Key, class Value>
class map
{
typedef RBTree<Key, pair<const Key, Value>, select1st<Key, pair<const Key, Value>>> rbType;
public:
typedef typename RBTree<Key, pair<const Key, Value>, select1st<Key, pair<const Key, Value>>>::Iterator iterator;
typedef typename RBTree < Key, pair<const Key, Value>, select1st<Key, pair<const Key, Value>>>::const_Iterator const_iterator;
iterator begin()
{
return _t.begin();
}
const_iterator begin()const
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
const_iterator end()const
{
return _t.end();
}
pair<iterator, bool> insert(const pair<Key, Value>& kv)
{
return _t.Insert(kv);
}
Value& operator[](const Key& key)
{
pair<iterator, bool> ret = _t.Insert({key, Value()});
return ret.first->second;
}
private:
rbType _t;
};
}Myset.h
#pragma once
#include "RBTree.h"
namespace mosheng {
template<class K>
class identity
{
public:
const K& operator()(const K& key)
{
return key;
}
};
template<class Key>
class set
{
typedef RBTree<Key, const Key, identity<Key>> rbType;
public:
typedef typename RBTree<Key, const Key, identity<Key>>::Iterator iterator;
typedef typename RBTree<Key, const Key, identity<Key>>::const_Iterator const_iterator;
iterator begin()
{
return _t.begin();
}
const_iterator begin()const
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
const_iterator end()const
{
return _t.end();
}
pair<iterator, bool> insert(const Key& key)
{
return _t.Insert(key);
}
private:
rbType _t;
};
}Mymap.h
#pragma once
#include "RBTree.h"
namespace mosheng {
template<class K, class KV>
class select1st
{
public:
const K& operator()(const KV& kv)
{
return kv.first;
}
};
template<class Key, class Value>
class map
{
typedef RBTree<Key, pair<const Key, Value>, select1st<Key, pair<Key, Value>>> rbType;
public:
typedef typename RBTree<Key, pair<const Key, Value>, select1st<Key, pair<const Key, Value>>>::Iterator iterator;
typedef typename RBTree < Key, pair<const Key, Value>, select1st<Key, pair<const Key, Value>>>::const_Iterator const_iterator;
iterator begin()
{
return _t.begin();
}
const_iterator begin()const
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
const_iterator end()const
{
return _t.end();
}
pair<iterator, bool> insert(const pair<Key, Value>& kv)
{
return _t.Insert(kv);
}
Value& operator[](const Key& key)
{
pair<iterator, bool> ret = _t.Insert({key, Value()});
return ret.first->second;
}
private:
rbType _t;
};
}RBTree.h
#pragma once
enum Colour
{
RED,
BLACK
};
template<class Value>
struct RBTreeNode
{
RBTreeNode(const Value& kv)
:_kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
{
};
Value _kv;
RBTreeNode<Value>* _left;
RBTreeNode<Value>* _right;
RBTreeNode<Value>* _parent;
Colour _col = RED;
};
template<class Value, class Ref, class Ptr>
class TreeIterator
{
typedef RBTreeNode<Value> Node;
typedef TreeIterator<Value, Ref, Ptr> Self;
public:
TreeIterator(Node* node = nullptr)
:_node(node)
{}
Ref operator* ()
{
return _node->_kv;
}
Ref operator* ()const
{
return _node->_kv;
}
Ptr operator->()
{
return &(_node->_kv);
}
Ptr operator->()const
{
return &(_node->_kv);
}
bool operator==(const Self& s)const
{
return _node == s._node;
}
bool operator!=(const Self& s)const
{
return _node != s._node;
}
//++重载
Self& operator++()
{
if (_node->_right)
{
Node* min = _node->_right;
while (min->_left)
{
min = min->_left;
}
_node = min;
}
else
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && parent->_right == cur)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
private:
Node* _node;
};
template<class Key, class Value, class KeyOfValue>
class RBTree
{
typedef RBTreeNode<Value> Node;
public:
typedef TreeIterator<Value, Value&, Value*> Iterator;
typedef TreeIterator<Value, const Value&, const Value*> const_Iterator;
Iterator begin()
{
Node* it = _root;
while (it->_left)
{
it = it->_left;
}
return Iterator(it);
}
const_Iterator begin()const
{
Node* it = _root;
while (it->_left)
{
it = it->_left;
}
return Iterator(it);
}
Iterator end()
{
return Iterator(nullptr);
}
const_Iterator end()const
{
return Iterator(nullptr);
}
pair<Iterator, bool> Insert(const Value& kv)
{
//处理空树的情况
if (_root == nullptr)
{
_root = new Node(kv);
_root->_col = BLACK;
return { Iterator(_root), true };
}
//找到要插入的位置
else
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (KeyOfValue()(kv) < KeyOfValue()(cur->_kv))
{
parent = cur;
cur = cur->_left;
}
else if (KeyOfValue()(kv) > KeyOfValue()(cur->_kv))
{
parent = cur;
cur = cur->_right;
}
else
{
return { Iterator(cur), false };
}
}
//插入新节点
cur = new Node(kv);
Node* newnode = cur;
if (KeyOfValue()(parent->_kv) > KeyOfValue()(kv))
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
//父节点为红色的情况下需要进行处理
if (parent->_col == RED)
{
while (parent && parent->_col == RED)
{
//记录节点
Node* grandfather = parent->_parent;
Node* uncle = nullptr;
if (grandfather->_left == parent)
{
uncle = grandfather->_right;
}
else
{
uncle = grandfather->_left;
}
//uncle为红色的情况
//仅变色
if (uncle && uncle->_col == RED)
{
uncle->_col = parent->_col = BLACK;
grandfather->_col = RED;
//更新
cur = grandfather;
parent = cur->_parent;
}
//uncle为黑色或为空的情况
else
{
//右旋转
if (grandfather->_left == parent && parent->_left == cur)
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
break;
}
//左旋转
else if (grandfather->_right == parent && parent->_right == cur)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
break;
}//左右双旋
else if (grandfather->_left == parent && parent->_right == cur)
{
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
break;
}
//右左双旋
else
{
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
break;
}
}
}
}
//处理根节点颜色
_root->_col = BLACK;
return { Iterator(newnode), true };
}
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool IsBalance()
{
if (_root && _root->_col == RED)
{
return false;
}
int leftMost = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLACK)
{
leftMost++;
}
cur = cur->_left;
}
return _check(_root, 0, leftMost);
}
int Height()
{
return _Height(_root);
}
int Size()
{
return _Size(_root);
}
Node* Find(const Key& key)
{
Node* cur = _root;
while (cur)
{
if (KeyOfValue()(cur->_kv) < key)
{
cur = cur->_right;
}
else if (KeyOfValue()(cur->_kv) > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}
private:
int _Size(Node* root)
{
return root == nullptr ? 0 : _Size(root->_left) + _Size(root->_right) + 1;
}
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;
}
bool _check(Node* cur, int BlackNum, const int leftMost)
{
if (cur == nullptr)
{
if (BlackNum != leftMost)
{
return false;
cout << "黑色节点的数量不相等" << endl;
}
else
return true;
}
if (cur->_col == RED && cur->_parent && cur->_parent->_col == RED)
{
cout << cur->_kv.first << "->" << "连续的红色节点" << endl;
return false;
}
if (cur->_col == BLACK)
{
BlackNum++;
}
return _check(cur->_left, BlackNum, leftMost) && _check(cur->_right, BlackNum, leftMost);
}
void _InOrder(const Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.first << " ";
_InOrder(root->_right);
}
void RotateR(Node* parent)
{
//记录节点
Node* subL = parent->_left;
Node* subLR = subL->_right;
Node* parentParent = parent->_parent;
//改变指针
parent->_left = subLR;
subL->_right = parent;
if (parentParent == nullptr)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subL;
}
else
{
parentParent->_right = subL;
}
subL->_parent = parentParent;
}
//避免subLR = nullptr出现空指针解引用的情况
if (subLR)
{
subLR->_parent = parent;
}
parent->_parent = subL;
}
void RotateL(Node* parent)
{
//记录节点
Node* subR = parent->_right;
Node* subRL = subR->_left;
Node* parentParent = parent->_parent;
//改变指针指向
subR->_left = parent;
parent->_right = subRL;
if (parentParent == nullptr)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subR;
}
else
{
parentParent->_right = subR;
}
subR->_parent = parentParent;
}
if (subRL)
{
subRL->_parent = parent;
}
parent->_parent = subR;
}
Node* _root = nullptr;
};今天的分享就到此结束啦,如果对读者朋友们有所帮助的话,可否留下宝贵的三连呢~~ 让我们共同努力, 一起走下去!