1.map设计图
2.set设计图
map和set其实都是将 红黑树 和 迭代器 进行了封装,所以,想要实现map和set只需要实现红黑树和迭代器,让后将其组合封装,对外提供统一的接口即可。
3.迭代器的设计
map和set的迭代器是封装了红黑树的迭代器,所以我们只需实现红黑树的迭代器即可;map和set的迭代器底层封装的是结点的指针。所以我们在RBTreeIterator中,主要封装一些操作结点的指针的接口。
判断迭代器相等or不相等:
迭代器封装的是红黑树的结点的指针,判断想不相等,只需要判断结点的指针是否相等。
bool operator!=(const Self& s)
{
return _node != s._node;
}
bool operato == (const Self & s)
{
return _node == s._node;
}
迭代器的++操作:
红黑树中的遍历是按照二叉树的中序遍历来走的,迭代器的++操作也是一样的,那么迭代器++之后应该到达哪一个位置呢?
按照中序遍历的规则,左子树->根->右子树,访问完根之后,++应该去右子树中,那应该是右子树的哪个结点呢?按照二叉搜索树的规则,应该是与根结点的差值最小的那一个,不就是右子树的最左结点吗?
上面讨论的是右子树存在的情况,如果右子树不存在呢?右子树不存在,也就是右子树为空的情况,意味着这个节点的子树中序访问完了,下一个节点找祖先里面,孩子是父亲左的那个祖先。
代码如下:
Self& operator++()
{
if (_node->_right)
{
// 右子树的中序第一个(最左节点)
Node* subLeft = _node->_right;
while (subLeft->_left)
{
subLeft = subLeft->_left;
}
_node = subLeft;
}
else
{
// 祖先里面孩子是父亲左的那个
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
重载operator->:
迭代器的->操作主要是用来操作结点中的数据的,操作结点中的数据,需要数据的指针,所以,返回数据的地址即可。
T* operator->()
{
return &_node->_data;
}
重载operator*:
迭代器的 * 操作主要是用来获取数据的,所以我们只需要返回结点中的数据即可。
T& operator*()
{
return _node->_data;
}
迭代器完整代码如下:
template<class T>
struct RBTreeIterator
{
typedef RBTreeNode<T> Node;
typedef RBTreeIterator<T> Self;
Node* _node;
RBTreeIterator(Node* node)
:_node(node)
{}
T& operator*()
{
return _node->_data;
}
T* operator->()
{
return &_node->_data;
}
Self& operator++()
{
if (_node->_right)
{
// 右子树的中序第一个(最左节点)
Node* subLeft = _node->_right;
while (subLeft->_left)
{
subLeft = subLeft->_left;
}
_node = subLeft;
}
else
{
// 祖先里面孩子是父亲左的那个
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
bool operato == (const Self & s)
{
return _node == s._node;
}
};
3.红黑树的设计
在使用STL中的map时,我们希望它存储的是一个个的键值对数据,所以我们通常指定两个类型,比如:map<int,int>;而在使用set时,我们希望它里面存储的是一个个的元素,所以我们通常指定一个类型,比如:set<int>;但是map和set的底层数据结构都是红黑树,那是不是意味着要实现两份红黑树,一份Key结构的,一份Key-Value结构的?如果这样实现的话,就会造成STL库中代码重复和冗余,前辈大佬们绝不允许这种事情发生。
大佬们想出了下面这种做法:
将底层的红黑树实现成Key-Value结构的,使用set的时候传入Key类型,用Key类型定义出key_type和value_type;使用map的时候传入Key类型和T类型,用Key类型定义出key_type,用Key类型和T类型定义出value_type。二者都将key_type传给红黑树的Key类型,将value_type传给红黑树的Value类型,到了红黑树定义结点的时候,只关注自己的Value类型;这时,如果使用的是set,结点中的数据就是一个个k类型的数据,如果使用的是map,结点中的数据就是<k,v>类型的键值对。通过这种方式,就实现了set和map复用同一个类模板的红黑树。
红黑树的设计推荐阅读如何实现一棵红黑树,这篇文章中的红黑树是纯粹的红黑树,和用于实现map和set的红黑树有点不同:1.用于实现map和set的红黑树中还需要提供操作迭代器的接口。2.获取map和set中的Key值。
迭代器操作
迭代器的begin操作:begin() 用于返回红黑树走中序遍历的第一个结点的迭代器。也就是红黑树中最左节点的迭代器,代码如下所示:
iterator begin()
{
Node* subLeft = _root;
while (subLeft && subLeft->_left)
{
subLeft = subLeft->_left;
}
return iterator(subLeft);
}
迭代器的end()操作:end() 用来返回红黑树走中序遍历的最后一个节点的下一个位置,其实就是空。代码如下所示:
iterator end()
{
return iterator(nullptr);
}
获取map和set中的Key值
在红黑树的操作中,我们经常要比较两个值的大小,map中存储的是键值对,我们希望比较的时候按照Key值来比较,set中存储的是单个元素,我们也希望按照Key值来比较,但是存储键值对的map和存储单个元素的set都是封装了同一棵红黑树,所以我们获取map和set中Key的方式不同,所以我们可以提供第三个模板参数,用来获取map和set中的Key;这个模板参数以仿函数的形式实现,以缺省参数的形式提供给类模板红黑树使用。
获取map中的Key代码如下:
struct MapKeyOfT
{
const K& operator()(const pair<K, V>& kv)
{
return kv.first;
}
};
获取set中的Key代码如下:
struct SetKeyOfT
{
const K& operator()(const K& key)
{
return key;
}
};
4.附录 —— 各部分完整代码
迭代器和红黑树代码:
#include<vector>
enum Colour
{
RED,
BLACK
};
template<class T>
struct RBTreeNode
{
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
Colour _col;
T _data;
RBTreeNode(const T& data)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _data(data)
, _col(RED)
{}
};
template<class T, class Ptr, class Ref>
struct RBTreeIterator
{
typedef RBTreeNode<T> Node;
typedef RBTreeIterator<T, Ptr, Ref> Self;
Node* _node;
RBTreeIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
Self& operator++()
{
if (_node->_right)
{
// 右子树的中序第一个(最左节点)
Node* subLeft = _node->_right;
while (subLeft->_left)
{
subLeft = subLeft->_left;
}
_node = subLeft;
}
else
{
// 祖先里面孩子是父亲左的那个
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
Self& operator--()
{
// 跟++逻辑相反
return *this;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
bool operator==(const Self& s)
{
return _node == s._node;
}
};
// KeyOfT仿函数 取出T对象中的key
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&> const_iterator;
const_iterator begin() const
{
Node* subLeft = _root;
while (subLeft && subLeft->_left)
{
subLeft = subLeft->_left;
}
return const_iterator(subLeft);
}
iterator begin()
{
Node* subLeft = _root;
while (subLeft && subLeft->_left)
{
subLeft = subLeft->_left;
}
return iterator(subLeft);
}
iterator end()
{
return iterator(nullptr);
}
const_iterator end() const
{
return const_iterator(nullptr);
}
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();
}
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)
{
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;
if (kot(parent->_data) < kot(data))
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
cur->_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)
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
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
{
// 情况二:叔叔不存在或者存在且为黑
// 旋转+变色
if (cur == parent->_right)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return make_pair(iterator(newnode), true);
}
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;
subR->_parent = nullptr;
}
else
{
if (ppnode->_left == parent)
{
ppnode->_left = subR;
}
else
{
ppnode->_right = subR;
}
subR->_parent = ppnode;
}
}
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;
subL->_parent = nullptr;
}
else
{
if (ppnode->_left == parent)
{
ppnode->_left = subL;
}
else
{
ppnode->_right = subL;
}
subL->_parent = ppnode;
}
}
private:
Node* _root = nullptr;
};
my_set代码:
#include"RBTree.h"
namespace wall
{
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>::const_iterator const_iterator;
iterator begin()
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
pair<iterator, bool> insert(const K& key)
{
return _t.Insert(key);
}
iterator find(const K& key)
{
return _t.Find(key);
}
private:
RBTree<K, const K, SetKeyOfT> _t;
};
}
my_map代码:
#include"RBTree.h"
namespace wall
{
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, const K, MapKeyOfT>::const_iterator 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);
}
iterator find(const K& key)
{
return _t.Find(key);
}
V& operator[](const K& key)
{
pair<iterator, bool> ret = insert(make_pair(key, V()));
return ret.first->second;
}
private:
RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};
}