目录
0.前言
关于map和set的封装,底层都是红黑树。
所以我们要先实现红黑树:C++ 红黑树-CSDN博客
enum Colour { RED, BLACK }; template<class K, class V> struct RBTreeNode { RBTreeNode<K, V>* _left; RBTreeNode<K, V>* _right; RBTreeNode<K, V>* _parent; pair<K, V> _kv; Colour _col; RBTreeNode(const pair<K, V>& kv) :_left(nullptr) , _right(nullptr) , _parent(nullptr) , _kv(kv) , _col(RED) {} }; template<class K, class V> class RBTree { typedef RBTreeNode<K, V> Node; public: bool Insert(const pair<K, V>& kv) { if (_root == nullptr) { _root = new Node(kv); _root->_col = BLACK; return true; } Node* parent = nullptr; Node* cur = _root; while (cur) { if (cur->_kv.first < kv.first) { parent = cur; cur = cur->_right; } else if (cur->_kv.first > kv.first) { parent = cur; cur = cur->_left; } else { return false; } } cur = new Node(kv); cur->_col = RED; // 新增节点给红色 if (parent->_kv.first < kv.first) { parent->_right = cur; } else { parent->_left = cur; } cur->_parent = 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) { // 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 { Node* uncle = grandfather->_left; // 叔叔存在且为红,-》变色即可 if (uncle && uncle->_col == RED) { parent->_col = uncle->_col = BLACK; grandfather->_col = RED; // 继续往上处理 cur = grandfather; parent = cur->_parent; } else // 叔叔不存在,或者存在且为黑 { // 情况二:叔叔不存在或者存在且为黑 // 旋转+变色 // g // u p // c if (cur == parent->_right) { 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; } 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; _root->_parent = nullptr; } else { if (ppNode->_left == parent) { ppNode->_left = subL; } else { ppNode->_right = subL; } subL->_parent = ppNode; } } 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; _root->_parent = nullptr; } else { if (ppNode->_right == parent) { ppNode->_right = subR; } else { ppNode->_left = subR; } subR->_parent = ppNode; } } void InOrder() { _InOrder(_root); cout << endl; } bool IsBalance() { if (_root->_col == RED) { return false; } int refNum = 0; Node* cur = _root; while (cur) { if (cur->_col == BLACK) { ++refNum; } cur = cur->_left; } return Check(_root, 0, refNum); } private: bool Check(Node* root, int blackNum, const int refNum) { if (root == nullptr) { //cout << blackNum << endl; if (refNum != blackNum) { cout << "存在黑色节点的数量不相等的路径" << endl; return false; } return true; } if (root->_col == RED && root->_parent->_col == RED) { cout << root->_kv.first << "存在连续的红色节点" << endl; return false; } if (root->_col == BLACK) { blackNum++; } return Check(root->_left, blackNum, refNum) && Check(root->_right, blackNum, refNum); } void _InOrder(Node* root) { if (root == nullptr) { return; } _InOrder(root->_left); cout << root->_kv.first << ":" << root->_kv.second << endl; _InOrder(root->_right); } private: Node* _root = nullptr; };
1.整体框架
初步框架:
template<class T> struct RBTreeNode { RBTreeNode<T>* _left; RBTreeNode<T>* _right; RBTreeNode<T>* _parent; T _data; Colour _col; }; template<class K,class T> struct __RBTreeIterator { typedef RBTreeNode<T> Node; private: Node* _root = nullptr; }; template<class K,class V> class map { private: RBTree<K, pair<K, V>>_t; }; template<class K> class map { private: RBTree<K, K>_t; };
我们要实现map,set底层都是红黑树,但map是KV搜索模型,set则是K搜索模型;那么我们只好将两者都设计成模板参数,传K就是K搜索模型,传KV那就是KV搜索模型。
那么模板参数K是否没有必要传了,因为根本没有用到。当然不是,我们在写Find接口的时候,使用键(key)去寻找,而不是用值。
接下来实现插入逻辑,当然也是根据模板参数去实例化,但这里就有个问题了,在进行key的比较逻辑的时候,红黑树怎么知道data是set的key,还是map的pair呢?所以我们需要传第三个模板参数,让data区分,是set的key,还是map的pair,这样才能正确的执行比较逻辑。如果是set,那只需把key提供,如果是map,那就需要把pair.frist提供。
完整框架:
template<class K> class set { struct SetKeyOfT { const K& operator()(const K& key) { return key; } }; private: RBTree<K, const K, SetKeyOfT> _t; }; template<class K, class V> class map { struct MapKeyOfT { const K& operator()(const pair<K, V>& kv) { return kv.first; } }; private: RBTree<K, pair<const K, V>, MapKeyOfT> _t; }; struct RBTreeNode { RBTreeNode<T>* _left; RBTreeNode<T>* _right; RBTreeNode<T>* _parent; T _data; Colour _col; }; template<class K, class T, class KeyOfT> class RBTree { typedef RBTreeNode<T> Node; public: 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; while (cur) { // K // pair<K, V> // kot对象,是用来取T类型的data对象中的key 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 false; } } cur = new Node(data); cur->_col = RED; // 新增节点给红色 if (kot(parent->_data) < kot(data)) { parent->_right = cur; } else { parent->_left = cur; } cur->_parent = 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 true; } };
2.实现map和set的迭代器
namespace mymap { 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>::ConstIterator const_iterator; const_iterator begin() const { return _t.Begin(); } const_iterator end() const { return _t.End(); } iterator begin() { return _t.Begin(); } iterator end() { return _t.End(); } iterator find(const K& key) { return _t.Find(key); } pair<iterator, bool> insert(const pair<K, V>& kv) { return _t.Insert(kv); } V& operator[](const K& key) { pair<iterator, bool> ret = _t.Insert(make_pair(key, V())); return ret.first->second; } private: RBTree<K, pair<const K, V>, MapKeyOfT> _t; };
在这里以map为例,map和set都是复用同一份代码:
这些方法用于获取指向树中第一个和最后一个元素的迭代器。但是,由于红黑树不是顺序存储结构,所以“最后一个元素”的概念并不直接。在这里,
End
方法返回指向nullptr
的迭代器,表示序列的结束。而Begin
方法返回指向树中最左边节点的迭代器,因为在红黑树中,最左边的节点是最小的。typedef __RBTreeIterator<T, T&, T*> Iterator; typedef __RBTreeIterator<T, const T&, const T*> ConstIterator; RBTree() = default; RBTree(const RBTree<K, T, KeyOfT>& t) { _root = Copy(t._root); } // t2 = t1 RBTree<K, T, KeyOfT>& operator=(RBTree<K, T, KeyOfT> t) { swap(_root, t._root); return *this; } ~RBTree() { Destroy(_root); _root = nullptr; } Iterator Begin() { Node* leftMin = _root; while (leftMin && leftMin->_left) { leftMin = leftMin->_left; } return Iterator(leftMin); } Iterator End() { return Iterator(nullptr); } ConstIterator End() const { return ConstIterator(nullptr); } ConstIterator Begin() const { Node* leftMin = _root; while (leftMin && leftMin->_left) { leftMin = leftMin->_left; } rand return ConstIterator(leftMin); } Iterator Find(const K& key) { Node* cur = _root; while (cur) { if (cur->_key < key) { cur = cur->_right; } else if (cur->_key > key) { cur = cur->_left; } else { return Iterator(cur); } } return End(); }
这时对mapV& operator[](const K& key)的实现,允许你通过键(key)来访问或修改与键相关联的值(value)。
Insert
函数尝试将一个新的键值对(key
和默认构造的V()
)插入到红黑树中。如果key
已经存在于树中,Insert
函数不会插入新的键值对,但会返回已存在节点的迭代器。Insert
函数返回一个pair<iterator, bool>
,其中iterator
指向插入或找到的节点,bool
值表示是否成功插入了新的键值对(true
表示插入,false
表示键已存在)。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; cur->_col = RED; // 新增节点给红色 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); }
大体逻辑:
- 如果键
key
不存在于树中,则创建一个新的键值对(key
和默认构造的V
)并插入到树中,然后返回与该键相关联的值的引用。- 如果键
key
已经存在于树中,则直接返回与该键相关联的值的引用,而不进行任何插入操作。
3.完整代码
RBTree.h
#pragma once #include<vector> enum Colour { RED, BLACK }; 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 __RBTreeIterator { typedef RBTreeNode<T> Node; typedef __RBTreeIterator<T, Ref, Ptr> Self; Node* _node; __RBTreeIterator(Node* node) :_node(node) {} Ref operator*() { return _node->_data; } Ptr operator->() { return &_node->_data; } bool operator!=(const Self& s) { return _node != s._node; } Self& operator++() { if (_node->_right) { // 下一个,右树最左节点 Node* leftMin = _node->_right; while (leftMin->_left) { leftMin = leftMin->_left; } _node = leftMin; } else { // 下一个,孩子等于父亲左的那个祖先 Node* cur = _node; Node* parent = cur->_parent; while (parent && cur == parent->_right) { cur = parent; parent = parent->_parent; } _node = parent; } return *this; } }; 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; RBTree() = default; RBTree(const RBTree<K, T, KeyOfT>& t) { _root = Copy(t._root); } // t2 = t1 RBTree<K, T, KeyOfT>& operator=(RBTree<K, T, KeyOfT> t) { swap(_root, t._root); return *this; } ~RBTree() { Destroy(_root); _root = nullptr; } Iterator Begin() { Node* leftMin = _root; while (leftMin && leftMin->_left) { leftMin = leftMin->_left; } return Iterator(leftMin); } Iterator End() { return Iterator(nullptr); } ConstIterator End() const { return ConstIterator(nullptr); } ConstIterator Begin() const { Node* leftMin = _root; while (leftMin && leftMin->_left) { leftMin = leftMin->_left; } return ConstIterator(leftMin); } Iterator Find(const K& key) { Node* cur = _root; while (cur) { if (cur->_key < key) { cur = cur->_right; } else if (cur->_key > 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; cur->_col = RED; // 新增节点给红色 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 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; _root->_parent = nullptr; } else { if (ppNode->_left == parent) { ppNode->_left = subL; } else { ppNode->_right = subL; } subL->_parent = ppNode; } } 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; _root->_parent = nullptr; } else { if (ppNode->_right == parent) { ppNode->_right = subR; } else { ppNode->_left = subR; } subR->_parent = ppNode; } } void InOrder() { _InOrder(_root); cout << endl; } bool IsBalance() { if (_root->_col == RED) { return false; } int refNum = 0; Node* cur = _root; while (cur) { if (cur->_col == BLACK) { ++refNum; } cur = cur->_left; } return Check(_root, 0, refNum); } private: Node* Copy(Node* root) { if (root == nullptr) return nullptr; Node* newroot = new Node(root->_data); newroot->_col = root->_col; newroot->_left = Copy(root->_left); if (newroot->_left) newroot->_left->_parent = newroot; newroot->_right = Copy(root->_right); if (newroot->_right) newroot->_right->_parent = newroot; return newroot; } void Destroy(Node* root) { if (root == nullptr) return; Destroy(root->_left); Destroy(root->_right); delete root; root = nullptr; } bool Check(Node* root, int blackNum, const int refNum) { if (root == nullptr) { //cout << blackNum << endl; if (refNum != blackNum) { cout << "存在黑色节点的数量不相等的路径" << endl; return false; } return true; } if (root->_col == RED && root->_parent->_col == RED) { //cout << root->_kv.first << "存在连续的红色节点" << endl; return false; } if (root->_col == BLACK) { blackNum++; } return Check(root->_left, blackNum, refNum) && Check(root->_right, blackNum, refNum); } void _InOrder(Node* root) { if (root == nullptr) { return; } _InOrder(root->_left); cout << root->_kv.first << ":" << root->_kv.second << endl; _InOrder(root->_right); } private: Node* _root = nullptr; };
Mymap.h
#pragma once namespace mymap { 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>::ConstIterator const_iterator; const_iterator begin() const { return _t.Begin(); } const_iterator end() const { return _t.End(); } iterator begin() { return _t.Begin(); } iterator end() { return _t.End(); } iterator find(const K& key) { return _t.Find(key); } pair<iterator, bool> insert(const pair<K, V>& kv) { return _t.Insert(kv); } V& operator[](const K& key) { pair<iterator, bool> ret = _t.Insert(make_pair(key, V())); return ret.first->second; } private: RBTree<K, pair<const K, V>, MapKeyOfT> _t; };
Myset.h
#pragma once namespace myset { 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; const_iterator begin() const { return _t.Begin(); } const_iterator end() const { return _t.End(); } iterator begin() { return _t.Begin(); } iterator end() { return _t.End(); } iterator find(const K& key) { return _t.Find(key); } pair<iterator, bool> insert(const K& key) { return _t.Insert(key); } private: RBTree<K, const K, SetKeyOfT> _t; };
4.数据测试
set:
void PrintSet(const set<int>& s) { for (auto e : s) { cout << e << endl; } } void test_set() { set<int> s; s.insert(4); s.insert(2); s.insert(5); s.insert(15); s.insert(7); s.insert(1); s.insert(5); s.insert(7); PrintSet(s); }
map:
void test_map1() { map<string, int> m; m.insert({ "苹果",1 }); m.insert({ "鸭梨",1 }); m.insert({ "草莓",1 }); m.insert({ "西瓜",3 }); map<string, int>::iterator it = m.begin(); while (it != m.end()) { it->second += 1; cout << it->first << ":" << it->second << endl; ++it; } cout << endl; }