目录
引言
1.迭代器
2.map的[]重载
3.KeyOfValue模板参数
4.整体代码展示
//改造后的红黑树代码
#include <iostream>
using namespace std;enum Colour {RED = 0,BLACK,
};//为了实现map与set封装使用同一个模板红黑树,前者的value是pair,后者的value为key
//因此我们需要对红黑树的模板进行改造
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 __RBtree_iterator
{typedef RBTreeNode<T> Node;typedef __RBtree_iterator<T, Ref, Ptr> self;Node* _node;__RBtree_iterator() {}__RBtree_iterator(Node* node):_node(node){}// 1、typedef __RBTreeIterator<T, T&, T*> iterator; 拷贝构造// 2、 typedef __RBTreeIterator<T, const T&, const T*> const_iterator;// 支持普通迭代器构造const迭代器的构造函数__RBtree_iterator(const __RBtree_iterator<T, T&, T*>& it):_node(it._node){}//实现前置++,保证指针往后移动,能够实现树的中序遍历self& operator++(){//如果当前节点的右子树不为空if (_node->_right){// 1、右不为空,下一个就是右子树的最左节点Node* subLeft = _node->_right;while (subLeft->_left){subLeft = subLeft->_left;}_node = subLeft;}
//如果当前节点的右子树为空,由于按照左子树,根,右子树遍历,说明此时该子树已经全部遍历//需要向上找孩子为父亲左孩子的祖先节点else{Node* parent = _node->_parent;Node* cur = _node;while (parent && cur == parent->_right){cur = parent;parent = parent->_parent;}_node = parent;}return *this;}//实现前置--self& operator--(){//如果当前节点的左子树不为空if (_node->_left){// 1、左不为空,下一个就是左子树的最右节点Node* subRight = _node->_left;while (subRight->_right){subRight = subRight->_right;}_node = subRight;}// 2、左为空,孩子是父亲的右的那个祖先else{Node* parent = _node->_parent;Node* cur = _node;while (parent && cur == parent->_right){cur = parent;parent = parent->_parent;}_node = parent;}return *this;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}bool operator!=(const self& s){return _node != s._node;}
};//仿函数
template<class K, class T, class KeyOfValue>
class RBTree
{typedef RBTreeNode<T> Node;
public:~RBTree(){_Destroy(_root);_root = nullptr;}public:typedef __RBtree_iterator<T, T&, T*> iterator;typedef __RBtree_iterator<T, const T&, const T*> const_iterator;iterator begin(){Node* cur = _root;//找树的最左节点while (cur && cur->_left){cur = cur->_left;}return iterator(cur);}const_iterator begin() const{Node* cur = _root;//找树的最左节点while (cur && cur->_left){cur = cur->_left;}return iterator(cur);}iterator end(){return iterator(nullptr);}const_iterator end() const{return const_iterator(nullptr);}//对于set类型而言,其value比较的是key,所以增加了一个模板参数Node* Find(const K& key){Node* cur = _root;KeyOfValue kot;while (cur){ if (kot(cur-> _data) > key){cur = cur->_left;}else if (kot(cur-> _data) < key){cur = cur->_right;}else{return cur;}}return nullptr;}pair<iterator,bool> Insert(const T& data){//假如刚开始没有节点,直接使其成为根即可//满足规则,根节点必须为黑色if (_root == nullptr){_root = new Node(data);_root->_col = BLACK;return make_pair(iterator(_root), true);}//同样满足二叉树的搜索规则,先找到新节点的正确位置Node* parent = nullptr;Node* cur = _root;KeyOfValue kot;while (cur){if (kot(cur-> _data) > kot(data)){parent = cur;cur = cur->_left;}else if (kot(cur-> _data) < kot(data)){parent = cur;cur = cur->_right;}else{return make_pair(iterator(cur),false);}}//建新节点cur = new Node(data);Node* newnode = cur;if (kot(parent->_data) > kot(data)){parent->_left = cur;}else{parent->_right = cur;}cur->_parent = parent;//调整节点颜色while (parent && parent->_col == RED){//找爷爷Node* grandfather = parent->_parent;//父亲为爷爷的左节点if (grandfather->_left == parent){//则叔叔是爷爷的右节点Node* uncle = grandfather->_right;// 情况1:u存在且为红,变色处理,并继续往上处理if (uncle && uncle->_col == RED){//父亲和叔叔节点都调节为黑色parent->_col = BLACK;uncle->_col = BLACK;//爷爷调节为红色grandfather->_col = RED;//往上调节cur = grandfather;parent = cur->_parent;}else // 情况2+3:u不存在/u存在且为黑,旋转+变色{// g// p u// c//右单旋if (cur == parent->_left){RotateR(grandfather);grandfather->_col = RED;parent->_col = BLACK;}else{// g// p u// c//LR双旋RotateL(parent);RotateR(grandfather);cur->_col = BLACK;//parent->_col = RED;grandfather->_col = RED;}break;}}//父亲为爷爷的右节点else{//则叔叔是爷爷的左节点Node* uncle = grandfather->_left;// 情况1:u存在且为红,变色处理,并继续往上处理if (uncle && uncle->_col == RED){//父亲和叔叔节点都调节为黑色parent->_col = BLACK;uncle->_col = BLACK;//爷爷调节为红色grandfather->_col = RED;//往上调节cur = grandfather;parent = cur->_parent;}else // 情况2+3:u不存在/u存在且为黑,旋转+变色{// g// u p// c//左单旋if (cur == parent->_right){RotateL(grandfather);grandfather->_col = RED;parent->_col = BLACK;}else{// g// u p// c//RL双旋RotateR(parent);RotateL(grandfather);cur->_col = BLACK;//parent->_col = RED;grandfather->_col = RED;}break;}}}_root->_col = BLACK;return make_pair(iterator(newnode),true);}int Height(){return _Height(_root);}bool IsRBTree(){//假如根节点存在,但颜色不是黑色,则不是红黑树if (_root && _root->_col == RED){cout << "根节点颜色是红色" << endl;return false;}//随便选一条路径作为黑色节点参考点int benchmark = 0;Node* cur = _root;while (cur){if (cur->_col == BLACK)benchmark++;cur = cur->_left;}// 连续红色节点return _Check(_root, 0, benchmark);}private:void _Destroy(Node* root){if (root == nullptr){return;}_Destroy(root->_left);_Destroy(root->_right);delete root;}int _Height(Node* root){if (root == NULL)return 0;int leftH = _Height(root->_left);int rightH = _Height(root->_right);return leftH > rightH ? leftH + 1 : rightH + 1;}bool _Check(Node* root, int blackNum, int benchmark){//假如到空节点(叶子节点),说明已经走完一条路径,可以开始判断if (root == nullptr){//假如统计出的黑色节点个数和参考黑色节点个数不同,则一定不是红黑树if (blackNum != benchmark){cout << "某条路径黑色节点的数量不相等" << endl;return false;}return true;}//递归遇到黑色节点时,则blackNum可以加1if (root->_col == BLACK)blackNum++;//假如连续存在两个红色节点,则也不是红黑树,注意还需要判断父节点是否存在if (root->_col == RED && root->_parent && root->_parent->_col == RED){cout << "存在连续的红色节点" << endl;return false;}//递归判断是否是红黑树,左子树和右子树都为红黑树,则为红黑树return _Check(root->_left, blackNum, benchmark)&& _Check(root->_right, blackNum, benchmark);}//左旋void RotateL(Node* parent){Node* subR = parent->_right;Node* subRL = subR->_left;//b变成30的右parent->_right = subRL;//父节点也需要调整,但subRL可能为空if (subRL)subRL->_parent = parent;//调整时未必是整棵树的调整,所以还需要考虑parent的链接问题,因此需要先记录ppNodeNode* ppNode = parent->_parent;subR->_left = parent;parent->_parent = subR;if (ppNode == nullptr){_root = subR;_root->_parent = nullptr;}else{//在调整爷爷节点指向的时候,还需要考虑原来parent是爷爷的左还是右//subR重新链接回爷爷的左或者右if (ppNode->_right == parent){ppNode->_right = subR;}else{ppNode->_left = subR;}subR->_parent = ppNode;}}//右旋void RotateR(Node* parent){Node* subL = parent->_left;Node* subLR = subL->_right;//b变成60的左parent->_left = subLR;//父节点也需要调整,但subRL可能为空if (subLR)subLR->_parent = parent;//调整时未必是整棵树的调整,所以还需要考虑parent的链接问题,因此需要先记录ppNodeNode* ppNode = parent->_parent;subL->_right = parent;parent->_parent = subL;if (ppNode == nullptr){_root = subL;_root->_parent = nullptr;}else{//在调整爷爷节点指向的时候,还需要考虑原来parent是爷爷的左还是右//subL重新链接回爷爷的左或者右if (ppNode->_right == parent){ppNode->_right = subL;}else{ppNode->_left = subL;}subL->_parent = ppNode;}}void _Inorder(Node* root){if (root == nullptr){return;}_Inorder(root->_left);cout << root->_kv.first << " ";_Inorder(root->_right);}
private:Node* _root = nullptr;
};
引言
在上节,我们根据源码,对红黑树代码进行了改造,在这基础上,我们便可以利用同一份红黑树代
码,模拟实现map,set的封装
1.迭代器
set对应key模型,即在不在
一旦插入后,我们是不能够通过迭代器对它的key随意进行修改的
//源码中set的代码
typedef typename rep_type::const_iterator iterator;
typedef typename rep_type::const_iterator const_iterator;
源码实现不能通过迭代器修改key值得方式也非常直接明了,即无论是const迭代器或者普通迭代
器,对于set来说,实际上都是const迭代器,我们无法通过const迭代器对key进行修改
与之对应,map则可以通过迭代器修改value值
//源码中map的代码
typedef typename rep_type::iterator iterator;
typedef typename rep_type::const_iterator const_iterator;
对于map来说,是存在const迭代器和普通迭代器的区别的
2.map的[]重载
在map中,有一个元素访问操作非常特殊,也非常方便,我们模拟实现不能错过,那就是[]重载
它主要能发挥四大作用
第一.(插入)如果红黑树中没有对应的值Key,它可以直接插入
第二.(插入+修改)如果红黑树中不存在对应的键值Key,它可以直接插入,并修改对应的Value
第三.(查找)直接判断红黑树中是否存在对应的键值Key
第四.(修改)如果红黑树中存在对应的Key,可以直接修改对应的Value
using namespace std;
int main()
{ std::map<char, std::string> mymap;mymap['a'] = "an element"; //插入+修改mymap['b'] = "another element"; mymap['a'] = "an element!"; //修改mymap['c'] = mymap['b']; //赋值std::cout << "mymap['a'] is " << mymap['a'] << '\n';std::cout << "mymap['b'] is " << mymap['b'] << '\n';std::cout << "mymap['c'] is " << mymap['c'] << '\n';std::cout << "mymap['d'] is " << mymap['d'] << '\n'; //插入std::cout << mymap['a'] << endl; //查找std::cout << "mymap now contains " << mymap.size() << " elements.\n";return 0;
}
对应的代码允许结果如下:
想要实现相应[ ]运算符重载,首先需要对insert进行改造
insert函数的返回值,不再是bool值,而是pair,包含迭代器和bool两大部分
假如插入失败,除了返回false外,还需要返回树中已经存在相同键值的迭代器
假如插入成功,除了返回true外,还需要返回插入的新节点的迭代器
通过迭代器,我们就可以相应访问节点的值,如果加一个引用,就可以对其进行修改
//方括号重载
V& operator[](const K& key)
{pair<iterator,bool> ret = _t.Insert(make_pair(key,V()));//插入后,返回该位置对应的迭代器,我们需要返回的是return ret.first->second;
}pair<iterator, bool> insert(const pair<const K, V>& kv)
{return _t.Insert(kv);
}
3.KeyOfValue模板参数
在红黑树插入insert,查找Find中,我们都需要比较两个键值的大小,从而找到对应节点的位置
但是set,map的data值可并不相同,前者是key,后者则是pair
如果直接和key比较,假如是set则没有问题,但是假如是map,则会出现毛病
原因在于pair的比较规则不符合我们的要求,pair 在first member(第一个参数)相同的情况下,还会
去比较second member(第二个参数),这显然不太符合我们要求,因为我们红黑树建树,是只比较
Key的
所以,我们引入了KeyOfValue这个模板参数,然后在map,set封装时,提供对应的仿函数,取出对
应应该比较的值
class map {struct KeyOfValue{const K& operator()(const pair<const K,V>& kv){return kv.first;}};
...
}class set {struct KeyOfValue {const K& operator()(const K& key){return key;}};
...
}
4.整体代码展示
//set模拟封装
#include "Tree.h"
namespace zzq
{template<class K>class set {struct KeyOfValue {const K& operator()(const K& key){return key;}};public:typedef typename RBTree<K, K, KeyOfValue>::const_iterator iterator;typedef typename RBTree<K, K, KeyOfValue>::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 K& key){return _t.Insert(key);}private:RBTree<K, K, KeyOfValue> _t;};
}
//map模拟封装
#include "Tree.h"
namespace zzq
{template<class K,class V>class map {struct KeyOfValue{const K& operator()(const pair<const K,V>& kv){return kv.first;}};public:typedef typename RBTree<K, pair<const K, V>, KeyOfValue>::iterator 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();}//方括号重载V& operator[](const K& key){pair<iterator,bool> ret = _t.Insert(make_pair(key,V()));//插入后,返回该位置对应的迭代器,我们需要返回的是return ret.first->second;}pair<iterator, bool> insert(const pair<const K, V>& kv){return _t.Insert(kv);}private:RBTree<K, pair<const K, V>, KeyOfValue> _t;};}