封装map & set
目录
[封装map & set](#封装map & set)
一、源码及框架分析
map和set的实现结构框架核心部分
cpp
// set
#ifndef __SGI_STL_INTERNAL_TREE_H
#include <stl_tree.h>
#endif
#include <stl_set.h>
#include <stl_multiset.h>
// map
#ifndef __SGI_STL_INTERNAL_TREE_H
#include <stl_tree.h>
#endif
#include <stl_map.h>
#include <stl_multimap.h>
// stl_set.h
template <class Key, class Compare = less<Key>, class Alloc = alloc>
class set
{
public:
// typedefs:
typedef Key key_type;
typedef Key value_type;
private:
typedef rb_tree<key_type, value_type,identity<value_type>, key_compare, Alloc> rep_type;
rep_type t; // red-black tree representing set
};
//stl_map.h
template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
class map
{
public:
// typedefs:
typedef Key key_type;
typedef T mapped_type;
typedef pair<const Key, T> value_type;
private:
typedef rb_tree<key_type, value_type, select1st<value_type>, key_compare, Alloc> rep_type;
rep_type t; // red-black tree representing map
};
//RBTree的基类
//stl_tree.h
struct __rb_tree_node_base
{
typedef __rb_tree_color_type color_type;
typedef __rb_tree_node_base* base_ptr;
color_type color;
base_ptr parent;
base_ptr left;
base_ptr right;
};
//stl_tree.h
template <class Key, class Value, class KeyOfValue, class Compare, class Alloc = alloc>
class rb_tree
{
protected:
typedef void* void_pointer;
typedef __rb_tree_node_base* base_ptr;
typedef __rb_tree_node<Value> rb_tree_node;
typedef rb_tree_node* link_type;
typedef Key key_type;
typedef Value value_type;
public:
// insert用的是第二个模板参数左形参
pair<iterator, bool> insert_unique(const value_type& x);
// erase和find用第一个模板参数做形参
size_type erase(const key_type & x);
iterator find(const key_type & x);
protected:
size_type node_count; // keeps track of size of tree
link_type header;
};
//RBTree的派生类
template <class Value>
struct __rb_tree_node : public __rb_tree_node_base
{
typedef __rb_tree_node<Value>* link_type;
Value value_field;
};

rb_tree使用了泛型思想,由第二个模板参数Value决定_rb_tree_node中存储的数据类型
- set实例化rb_tree时,第二个模板参数给的是key
- map实例化rb_tree时,第二个模板参数给的是pair<const key,T>
这样一颗红黑树既可以实现key,也可以实现key/value
源码中T代表value,value_type并非key_value中的value,而是节点中存储的数据类型
**问:**rb_tree第二个模板参数Value已经决定了节点中存储数据的类型,为什么还要传Key?
RBTree中insert可以用Value作模板参数:bool insert(const Value& v)
但是find与erase不能使用Value,只能用Key:iterator find(const Key& key)
二、模拟实现map与set
2.1.实现红黑树
cpp
#pragma once
#include <iostream>
using namespace std;
#include "assert.h"
#include "stdbool.h"
//枚举值表示颜色
//0-RED 1-BLACK
enum Colour
{
RED,
BLACK
};
template<class T>
struct RBTreeNode
{
T _data;
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
Colour _col;
RBTreeNode(const T& data)
: _data(data)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
{}
};
template <class K, class T,class KeyOfT>
class RBTree
{
typedef RBTreeNode<T> Node;
public:
//右单旋
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
Node* pParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (pParent->_left == parent)
{
pParent->_left = subL;
}
else
{
pParent->_right = subL;
}
subL->_parent = pParent;
}
}
//左单旋
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node* pParent = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (pParent == nullptr)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parent == pParent->_left)
{
pParent->_left = subR;
}
else
{
pParent->_right = subR;
}
subR->_parent = pParent;
}
}
bool Insert(const T& data)
{
//如果为空树
if (_root == nullptr)
{
//将根节点赋为新节点
_root = new Node(data);
//根节点颜色设置为黑色
_root->_col = BLACK;
//返回插入成功
return true;
}
KeyOfT kot;
//父节点为空
Node* parent = nullptr;
//当前节点为根节点
Node* cur = _root;
//遍历二叉树
//循环条件为cur不为空
while (cur)
{
//如果当前节点的key值小于新节点的key值
if (kot(cur->_data) < kot(data))
{
//更新父节点
parent = cur;
//更新当前节点(指向右子树)
cur = cur->_right;
}
else if (kot(cur->_data) > kot(data))//如果当前节点的key值大于新节点的key值
{
//更新父节点
parent = cur;
//更新当前节点(指向左子树)
cur = cur->_left;
}
else//当前节点的key值等于新节点的key值
{
//存在重复值,返回插入失败
return false;
}
}
//将当前节点(指向空)赋为新节点
cur = new Node(data);
//将插入节点设置为红色
cur->_col = RED;
//如果父节点的key值小于新节点的key值
if (kot(parent->_data) < kot(data))
{
//将新节点赋为父节点的右子树
parent->_right = cur;
}
else//如果父节点的key值大于新节点的key值
{
//将新节点赋为父节点的左子树
parent->_left = cur;
}
//将当前节点与父节点链接
cur->_parent = parent;
//循环条件:1、父亲节点存在 2、父亲节点颜色为红色
while (parent && parent->_col == RED)
{
//记录祖父节点
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)//如果父亲节点在祖父节点左边
{
// g
//p u
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//如果父亲节点在祖父节点右边
{
// g
//u p
Node* uncle = grandfather->_left;//叔父节点在祖父节点左边
if (uncle && uncle->_col == RED)//如果叔父节点存在,且为红色
{
//将父亲节点与叔父节点设置为黑色
parent->_col = uncle->_col = BLACK;
//祖父节点设置为红色
grandfather->_col = RED;
//继续向上处理
//将祖父节点更新为当前节点
cur = grandfather;
//祖先节点的父亲节点更新为父亲节点
parent = cur->_parent;
}
else//如果叔父节点不存在,或者为黑色
{
if (cur == parent->_right)//如果新增节点在父亲节点的右边(左单旋)
{
// g
// (u) p
// c
//祖父节点左单旋
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 true;
}
//高度计算
int Height()
{
return _Height(_root);
}
//节点计算
int Size()
{
return _Size(_root);
}
//查找
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_kv.first < key)
{
cur = cur->_right;
}
else if (cur->_kv.first > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}
private:
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;
}
int _Size(Node* root)
{
if (root == nullptr)
{
return 0;
}
return _Size(root->_left) + _Size(root->_right) + 1;
}
Node* _root = nullptr;
};
2.2.封装map和set框架
Myset.h
cpp
#pragma once
#include "RBTree.h"
namespace bit
{
template<class K>
class set
{
struct SetKeyOfT
{
const K& operator()(const K& key)
{
return key;
}
};
public:
bool insert(const K& key)
{
return _t.Insert(key);
}
private:
RBTree<K, K, SetKeyOfT> _t;
};
}
Mymap.h
cpp
#pragma once
#include "RBTree.h"
namespace bit
{
template<class K,class V>
class map
{
struct MapKeyOfT
{
const K& operator()(const pair<K,V>& kv)
{
return kv.first;
}
};
private:
RBTree<K, pair<K, V>, MapKeyOfT> _t;
};
}
2.3.迭代器实现
用一个类型封装节点的指针,再通过重载运算符实现迭代器像指针一样的行为
(与list的iterator思路一样)
map与set的迭代器走的是中序遍历(左子树 -> 根节点 -> 右子树)
所以begin()会返回第一个节点的iterator,即10(最左节点)的迭代器
迭代器++ 时,如果it指向节点的右子树不为空
代表当前节点已经访问完成
要访问下一个节点是右子树中序第一个,即右子树的最左节点
迭代器++ 时,如果it指向节点的右子树为空
代表当前节点与当前节点所在的子树已经访问完成
要访问的下一个节点在当前节点的祖先里面,沿当前节点到根的祖先路径向上找


set的iterator不支持修改,把set的第二个模板参数改为const K
RBTree<K,const K,SetKeyOfT> _t
map的iterator不支持修改key,但可以修改value,把map的第二个模板参数pair
的第一个参数改成const K即可
RBTree<K,pair<const K,V>,MapKeyOfT> _t
2.4.map与set封装总代码
RBTree.h
cpp
#pragma once
#include <iostream>
using namespace std;
#include "assert.h"
#include "stdbool.h"
enum Colour
{
RED,
BLACK
};
template<class T>
struct RBTreeNode
{
T _data;
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
Colour _col;
RBTreeNode(const T& data)
: _data(data)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
{}
};
template <class T, class Ref, class Ptr>
struct RBTreeIterator
{
typedef RBTreeNode<T> Node;
typedef RBTreeIterator<T, Ref, Ptr> Self;
Node * _node;
Node * _root;
RBTreeIterator(Node * node, Node * root)
:_node(node)
,_root(root)
{}
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 && cur == parent->_right)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
Self & operator--()
{
if (_node == nullptr)//--end()
{
//--end(),特殊处理,走到中序最后一个结点,整棵树的最右结点
Node * rightMost = _root;
while (rightMost && rightMost->_right)
{
rightMost = rightMost->_right;
}
_node = rightMost;
}
else if (_node->_left)
{
//左子树不为空,中序左子树最后一个
Node * rightMost = _node->_left;
while (rightMost->_right)
{
rightMost = rightMost->_right;
}
_node = rightMost;
}
else
{
//孩子是父亲右的那个祖先
Node * cur = _node;
Node * parent = cur->_parent;
while (parent && cur == parent->_left)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator!= (const Self & s) const
{
return _node != s._node;
}
bool operator== (const Self & s) const
{
return _node == s._node;
}
};
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;
Iterator Begin()
{
Node* cur = _root;
while (cur && cur->_left)
{
cur = cur->_left;
}
return Iterator(cur,_root);
}
Iterator End()
{
return Iterator(nullptr,_root);
}
ConstIterator Begin() const
{
Node* cur = _root;
while (cur && cur->_left)
{
cur = cur->_left;
}
return ConstIterator(cur,_root);
}
ConstIterator End() const
{
return ConstIterator(nullptr,_root);
}
//右单旋
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
Node* pParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (pParent->_left == parent)
{
pParent->_left = subL;
}
else
{
pParent->_right = subL;
}
subL->_parent = pParent;
}
}
//左单旋
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node* pParent = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (pParent == nullptr)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parent == pParent->_left)
{
pParent->_left = subR;
}
else
{
pParent->_right = subR;
}
subR->_parent = pParent;
}
}
pair<Iterator,bool> Insert(const T& data)
{
//如果为空树
if (_root == nullptr)
{
//将根节点赋为新节点
_root = new Node(data);
//根节点颜色设置为黑色
_root->_col = BLACK;
//返回插入成功
return pair<Iterator, bool>(Iterator(_root,_root),true);
//写法2:return {Iterator(_root,_root),true}
}
KeyOfT kot;
//父节点为空
Node* parent = nullptr;
//当前节点为根节点
Node* cur = _root;
//遍历二叉树
//循环条件为cur不为空
while (cur)
{
//如果当前节点的key值小于新节点的key值
if (kot(cur->_data) < kot(data))
{
//更新父节点
parent = cur;
//更新当前节点(指向右子树)
cur = cur->_right;
}
else if (kot(cur->_data) > kot(data))//如果当前节点的key值大于新节点的key值
{
//更新父节点
parent = cur;
//更新当前节点(指向左子树)
cur = cur->_left;
}
else//当前节点的key值等于新节点的key值
{
//存在重复值,返回插入失败
return pair<Iterator, bool>(Iterator(cur, _root), false);
}
}
//将当前节点(指向空)赋为新节点
cur = new Node(data);
//保存当前节点位置
Node* newnode = cur;
//将插入节点设置为红色
cur->_col = RED;
//如果父节点的key值小于新节点的key值
if (kot(parent->_data) < kot(data))
{
//将新节点赋为父节点的右子树
parent->_right = cur;
}
else//如果父节点的key值大于新节点的key值
{
//将新节点赋为父节点的左子树
parent->_left = cur;
}
//将当前节点与父节点链接
cur->_parent = parent;
//循环条件:1、父亲节点存在 2、父亲节点颜色为红色
while (parent && parent->_col == RED)
{
//记录祖父节点
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)//如果父亲节点在祖父节点左边
{
// g
//p u
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//如果父亲节点在祖父节点右边
{
// g
//u p
Node* uncle = grandfather->_left;//叔父节点在祖父节点左边
if (uncle && uncle->_col == RED)//如果叔父节点存在,且为红色
{
//将父亲节点与叔父节点设置为黑色
parent->_col = uncle->_col = BLACK;
//祖父节点设置为红色
grandfather->_col = RED;
//继续向上处理
//将祖父节点更新为当前节点
cur = grandfather;
//祖先节点的父亲节点更新为父亲节点
parent = cur->_parent;
}
else//如果叔父节点不存在,或者为黑色
{
if (cur == parent->_right)//如果新增节点在父亲节点的右边(左单旋)
{
// g
// (u) p
// c
//祖父节点左单旋
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 pair<Iterator, bool>(Iterator(newnode, _root), true);
}
//高度计算
int Height()
{
return _Height(_root);
}
//节点计算
int Size()
{
return _Size(_root);
}
//查找
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_kv.first < key)
{
cur = cur->_right;
}
else if (cur->_kv.first > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}
private:
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;
}
int _Size(Node* root)
{
if (root == nullptr)
{
return 0;
}
return _Size(root->_left) + _Size(root->_right) + 1;
}
Node* _root = nullptr;
};
Myset.h
cpp
#pragma once
#include "RBTree.h"
namespace bit
{
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;
iterator begin()
{
return _t.Begin();
}
iterator end()
{
return _t.End();
}
const_iterator begin() const
{
return _t.Begin();
}
const_iterator end() const
{
return _t.End();
}
pair<iterator, bool> insert(const K& key)
{
return _t.Insert(key);
}
private:
RBTree<K,const K, SetKeyOfT> _t;
};
}
Mymap.h
cpp
#pragma once
#include "RBTree.h"
namespace bit
{
template<class K,class V>
class map
{
struct MapKeyOfT
{
const K& operator()(const pair<K,V>& kv)
{
return kv.first;
}
};
public:
typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::Iterator iterator;
typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::ConstIterator const_iterator;
iterator begin()
{
return _t.Begin();
}
iterator end()
{
return _t.End();
}
const_iterator begin() const
{
return _t.Begin();
}
const_iterator end() const
{
return _t.End();
}
pair<iterator, bool> insert(const pair<K, V>& kv)
{
return _t.Insert(kv);
}
V& operator[](const K& key)
{
pair<iterator, bool> ret = insert({ key,V() });
return ret.first->second;
}
private:
RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};
}
test.c
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "Myset.h"
#include "Mymap.h"
int main()
{
bit::set<int> s;
s.insert(5);
s.insert(1);
s.insert(2);
s.insert(6);
s.insert(3);
s.insert(4);
bit::set<int>::iterator sit = s.begin();
while (sit != s.end())
{
//key值不可修改
//报错:*sit += 1;
cout << *sit << " ";
++sit;
}
cout << endl;
bit::map<string, string> dict;
dict.insert({ "sort","排序" });
dict.insert({ "left","左边" });
dict.insert({ "right","右边" });
dict["left"] = "左边,剩余";
dict["insert"] = "插入";
dict["string"];
bit::map<string, string>::iterator it = dict.begin();
while (it != dict.end())
{
//不能修改first,可以修改second
//报错:it->first += 'x';
it->second += 'x';
cout << it->first << ":" << it->second << " ";
++it;
}
cout << endl;
for (auto& kv : dict)
{
cout << it->first << ":" << it->second << " ";
}
return 0;
}