目录
红黑树改造
红黑树正向迭代器
结构
operator*和operator->
operator++
operator--
operator!=和==
红黑树反向迭代器
红黑树封装源码
map封装源码
set封装源码
为了实现复用,我们将红黑树模板参数进行改造。
T表示节点中所存储的数据类型
template
struct RBNode
{RBNode* _left;RBNode* _right;RBNode* _parent;T _data;Color _col;RBNode(const T& data):_left(nullptr),_right(nullptr),_parent(nullptr),_data(data),_col(RED){}
};
K表示key的类型,而T表示节点中所存储的数据类型
template
class RBTree
{typedef RBNode Node;
private:Node* _root;
};
那么第三个模板参数又是什么呢?
我们通过KeyofT实现仿函数,对其operator()运算符进行重载
struct MapKeyofT{const K& operator()(const pair& kv){return kv.first;}};
struct SetKeyofT{const K& operator()(const K& k){return k;}};
如果是set,kot(data)取出来的就是key
如果是map,kot(data)取出来的就是pair
通过它我们可以在红黑树中实现key值的比较
map结构:
private:RBTree, MapKeyofT> _t;
set结构:
private:
RBTree _t;
template
struct RBTreeIterator
{typedef RBNode Node;typedef RBTreeIterator self;Node* _node;RBTreeIterator(Node* node):_node(node){}
};
Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}
++就是要寻找中序遍历的下一个值,如果寻找呢?
如果右子树为空,那么就找孩子是父亲左的那个祖先节点
如果右子树不为空,那么就找右子树的最左节点
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 = parent->_parent;}_node = parent;}return *this;}self operator++(int){self tmp(*this);++(*this);return tmp;}
--的思路与++正好相反
如果左子树为空,那么就找孩子是父亲右的那个祖先节点
如果左子树不为空,那么就找左子树的最右节点
self& operator--(){if (_node->_left){//找左子树的最右节点Node* max = _node->_left;while (max->_right){max = max->_right;}_node = max;}else{//找孩子是父亲右的那个祖先节点Node* cur = _node;Node* parent = cur->_parent;while (parent && parent->_left == cur){cur = parent;parent = parent->_parent;}_node = parent;}}self operator--(int){self tmp(*this);--(*this);return tmp;}
直接使用节点地址比较
bool operator!=(const self& it)const{return _node != it._node;}bool operator==(const self& it)const{return _node == it._node;}
template
class _reverse_iterator
{
public:typedef _reverse_iterator self;_reverse_iterator(Iterator it):_it(it){}Ref operator*(){return *_it;}Ptr operator->(){return &(operator*());}bool operator==(const _reverse_iterator& rit)const{return _it == rit._it;}bool operator!=(const _reverse_iterator& rit)const{return _it != rit._it;}self& operator++(){--_it;return *this;}self operator++(int){self tmp(*this);--_it;return tmp;}self& operator--(){_it++;return *this;}self operator--(int){self tmp(*this);_it++;return tmp;}
private:Iterator _it;
};
template
class RBTree
{typedef RBNode Node;
public:typedef RBTreeIterator iterator;typedef RBTreeIterator const_iterator;typedef _reverse_iterator reverse_iterator;typedef _reverse_iterator const_reverse_iterator;reverse_iterator rbegin(){//寻找最右节点Node* max = _root;while (max && max->_right){max = max->_right;}return reverse_iterator(iterator(max));}reverse_iterator rend(){return reverse_iterator(iterator(nullptr));}const_reverse_iterator rbegin()const{Node* max = _root;while (max && max->_right){max = max->_right;}return const_reverse_iterator(const_iterator(max));}const_reverse_iterator rend()const{return const_reverse_iterator(const_iterator(nullptr));}iterator begin(){Node* min = _root;while (min&&min->_left){min = min->_left;}return iterator(min);}iterator end(){return iterator(nullptr);}const_iterator begin()const{Node* min = _root;while (min&&min->_left){min = min->_left;}return const_iterator(min);}const_iterator end()const{return const_iterator(nullptr);}RBTree():_root(nullptr){}RBTree(const RBTree& t){_root = Copy(t._root);}RBTree& operator=(RBTree t){swap(_root, t._root);return *this;}~RBTree(){Destory(_root);_root = nullptr;}iterator Find(const K& key){Node* cur = _root;KeyofT kot;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();}const_iterator Find(const K& key)const{Node* cur = _root;KeyofT kot;while (cur){if (kot(cur->_data) < key){cur = cur->_right;}else if (kot(cur->_data) > key){cur = cur->_left;}else{return const_iterator(cur);}}return end();}pair 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* ret = cur;if (kot(parent->_data) < kot(data)){parent->_right = cur;cur->_parent = parent;}else{parent->_left = cur;cur->_parent = parent;}//开始调整while (parent && parent->_col==RED)//当父亲为红才需要调整{Node* grandfather = parent->_parent;//grandfather一定存在if (grandfather->_left == parent){Node* uncle = grandfather->_right;//1.uncle存在且为红,p,u变黑,g变红,向上调整if (uncle && uncle->_col == RED){parent->_col = uncle->_col = BLACK;grandfather->_col = RED;cur = grandfather;parent = cur->_parent;}//2.uncle不存在或为黑,此时进行旋转else{Node* uncle = grandfather->_right;//单纯左边高// g// p//cur//此时对g进行右旋,p变黑,g变红if (parent->_left == cur){RoateR(grandfather);parent->_col = BLACK;grandfather->_col = RED;}// g//p// cur//此时先对p进行左旋,再对g进行右旋//cur变黑,g变红else{RoateL(parent);RoateR(grandfather);cur->_col = BLACK;grandfather->_col = RED;}break;}}else{Node* uncle = grandfather->_left;//1.uncle存在且为红,p,u变黑,g变红,向上调整if (uncle && uncle->_col == RED){parent->_col = uncle->_col = BLACK;grandfather->_col = RED;cur = grandfather;parent = cur->_parent;}//2.uncle不存在或为黑else{//单纯右边高// g// p// cur//对g进行左旋,g变红,p变黑if (parent->_right == cur){RoateL(grandfather);parent->_col = BLACK;grandfather->_col = RED;}//// g// p// cur// 此时先对p进行右旋,再对g进行左旋,cur变黑,g变红else{RoateR(parent);RoateL(grandfather);cur->_col = BLACK;grandfather->_col = RED;}break;}}}_root->_col = BLACK;return make_pair(iterator(ret), true);}
private:Node* Copy(Node* root){if (root == nullptr){return nullptr;}Node* newroot = new Node(root->_data);newroot->_left = Copy(root->_left);newroot->_right = Copy(root->_right);if (newroot->_left){newroot->_left->_parent = newroot;}if (newroot->_right){newroot->_right->_parent = newroot;}return newroot;}void Destory(Node* root){if (root == nullptr){return;}Destory(root->_left);Destory(root->_right);delete root;}void RoateL(Node* parent){//更改链接关系Node* parentparent = parent->_parent;Node* subR = parent->_right;Node* subRL = subR->_left;parent->_right = subRL;if (subRL){subRL->_parent = parent;}subR->_left = parent;parent->_parent = subR;if (parentparent == nullptr){_root = subR;subR->_parent = nullptr;}else{if (parentparent->_left == parent){parentparent->_left = subR;subR->_parent = parentparent;}else{parentparent->_right = subR;subR->_parent = parentparent;}}}void RoateR(Node* parent){//更改链接关系Node* parentparent = parent->_parent;Node* subL = parent->_left;Node* subLR = subL->_right;parent->_left = subLR;if (subLR){subLR->_parent = parent;}subL->_right = parent;parent->_parent = subL;if (parentparent == nullptr){_root = subL;subL->_parent = nullptr;}else{if (parentparent->_left == parent){parentparent->_left = subL;subL->_parent = parentparent;}else{parentparent->_right = subL;subL->_parent = parentparent;}}}Node* _root;
};
templateclass map{public:struct MapKeyofT{const K& operator()(const pair& kv){return kv.first;}};typedef typename RBTree, MapKeyofT>::iterator iterator;typedef typename RBTree, MapKeyofT>::const_iterator const_iterator;typedef typename RBTree, MapKeyofT>::reverse_iterator reverse_iterator;typedef typename RBTree, MapKeyofT>::const_reverse_iterator const_reverse_iterator;reverse_iterator rbegin(){return reverse_iterator(end());}reverse_iterator rend(){return reverse_iterator(begin());}const_reverse_iterator rbegin()const{return const_reverse_iterator(end());}const_reverse_iterator rend()const{return const_reverse_iterator(begin());}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 insert(const pair kv){return _t.Insert(kv);}V& operator[](const K& key){auto ret = _t.Insert(make_pair(key, V()));return ret.first->second;}iterator Find(const K& key){return _t.Find(key);}const_iterator Find(const K& key)const{return _t.Find(key);}private:RBTree, MapKeyofT> _t;};
set的key值不能改变,所以使用const_iterator作为set的iterator
templateclass set{public:struct SetKeyofT{const K& operator()(const K& k){return k;}};typedef typename RBTree::const_iterator iterator;typedef typename RBTree::const_reverse_iterator reverse_iterator;reverse_iterator rbegin()const{return _t.rbegin();}reverse_iterator rend()const{return _t.rend();}iterator begin()const{return _t.begin();}iterator end()const{return _t.end();}pair Insert(const K& key){auto ret= _t.Insert(key);return pair(iterator(ret.first._node), ret.second);}iterator Find(const K& key)const{return _t.Find(key);}private:RBTree _t;};