• 【C++ 学习 ㉔】- 详解 map 和 set(下)- map 和 set 的模拟实现



    一、RBT.h

    1. #pragma once
    2. #include
    3. namespace yzz
    4. {
    5. enum Color
    6. {
    7. RED,
    8. BLACK
    9. };
    10. template<class T>
    11. struct RBTNode
    12. {
    13. RBTNode* _left;
    14. RBTNode* _right;
    15. RBTNode* _parent;
    16. T _data;
    17. Color _clr;
    18. RBTNode(const T& data = T(), Color clr = RED)
    19. : _left(nullptr), _right(nullptr), _parent(nullptr)
    20. , _data(data), _clr(clr)
    21. { }
    22. };
    23. template<class T, class Ref, class Ptr>
    24. struct RBTIterator
    25. {
    26. typedef RBTNode Node;
    27. typedef RBTIterator iterator;
    28. typedef RBTIteratorconst T&, const T*> const_iterator;
    29. typedef RBTIterator self;
    30. Node* _pnode;
    31. RBTIterator(Node* p) : _pnode(p) { }
    32. // 可以通过 iterator 迭代器构造 const_iterator 迭代器
    33. RBTIterator(const iterator& it) : _pnode(it._pnode) { }
    34. Ref operator*() const
    35. {
    36. return _pnode->_data;
    37. }
    38. T* operator->() const
    39. {
    40. return &_pnode->_data;
    41. }
    42. void increment()
    43. {
    44. if (_pnode->_right)
    45. {
    46. // 若右子树存在,则找到右子树中值最小的节点(即右子树中最左侧的节点)
    47. Node* rightMin = _pnode->_right;
    48. while (rightMin->_left)
    49. {
    50. rightMin = rightMin->_left;
    51. }
    52. _pnode = rightMin;
    53. }
    54. else
    55. {
    56. // 若右子树不存在,表明以 *_pnode 为根的子树已经访问完了
    57. Node* parent = _pnode->_parent;
    58. while (parent)
    59. {
    60. if (_pnode == parent->_left)
    61. {
    62. // 若 *_pnode 是 *parent 的左孩子,
    63. // 则下一个该访问的节点就是 *parent
    64. break;
    65. }
    66. else
    67. {
    68. // 若 *_pnode 是 *parent 的右孩子,
    69. // 则以 *parent 为根的子树也已经访问了
    70. _pnode = parent;
    71. parent = parent->_parent;
    72. }
    73. }
    74. _pnode = parent;
    75. }
    76. }
    77. void decrement()
    78. {
    79. if (_pnode->_left)
    80. {
    81. // 若左子树存在,则找到左子树中值最大的节点(即左子树中最右侧的节点)
    82. Node* leftMax = _pnode->_left;
    83. while (leftMax->_right)
    84. {
    85. leftMax = leftMax->_right;
    86. }
    87. _pnode = leftMax;
    88. }
    89. else
    90. {
    91. Node* parent = _pnode->_parent;
    92. while (parent)
    93. {
    94. if (_pnode == parent->_right)
    95. {
    96. // 若 *_pnode 是 *parent 的右孩子,
    97. // 则上一个已访问的节点就是 *parent
    98. break;
    99. }
    100. else
    101. {
    102. // 若 *_pnode 是 *parent 的左孩子
    103. _pnode = parent;
    104. parent = parent->_parent;
    105. }
    106. }
    107. _pnode = parent;
    108. }
    109. }
    110. self& operator++()
    111. {
    112. increment();
    113. return *this;
    114. }
    115. self operator++(int)
    116. {
    117. self tmp = *this;
    118. increment();
    119. return tmp;
    120. }
    121. self& operator--()
    122. {
    123. decrement();
    124. return *this;
    125. }
    126. self operator--(int)
    127. {
    128. self tmp = *this;
    129. decrement();
    130. return tmp;
    131. }
    132. bool operator==(const self& it) const
    133. {
    134. return _pnode == it._pnode;
    135. }
    136. bool operator!=(const self& it) const
    137. {
    138. return _pnode != it._pnode;
    139. }
    140. };
    141. template<class K, class T, class KOfT>
    142. class RBT
    143. {
    144. typedef RBTNode Node;
    145. public:
    146. /*---------- 构造函数和析构函数 ----------*/
    147. RBT() : _root(nullptr) { }
    148. ~RBT()
    149. {
    150. Destroy(_root);
    151. }
    152. /*---------- 迭代器 ----------*/
    153. typedef RBTIterator iterator;
    154. typedef RBTIteratorconst T&, const T*> const_iterator;
    155. iterator begin()
    156. {
    157. Node* leftMin = _root;
    158. while (leftMin->_left)
    159. {
    160. leftMin = leftMin->_left;
    161. }
    162. return iterator(leftMin);
    163. }
    164. iterator end()
    165. {
    166. return iterator(nullptr);
    167. }
    168. const_iterator begin() const
    169. {
    170. Node* leftMin = _root;
    171. while (leftMin->_left)
    172. {
    173. leftMin = leftMin->_left;
    174. }
    175. return const_iterator(leftMin);
    176. }
    177. const_iterator end() const
    178. {
    179. return const_iterator(nullptr);
    180. }
    181. /*---------- 插入 ----------*/
    182. std::pairbool> insert(const T& data)
    183. {
    184. if (_root == nullptr)
    185. {
    186. _root = new Node(data, BLACK);
    187. return std::make_pair(iterator(_root), true);
    188. }
    189. Node* parent = nullptr;
    190. Node* cur = _root;
    191. KOfT kot;
    192. while (cur)
    193. {
    194. parent = cur;
    195. if (kot(data) < kot(cur->_data))
    196. cur = cur->_left;
    197. else if (kot(data) > kot(cur->_data))
    198. cur = cur->_right;
    199. else
    200. return std::make_pair(iterator(cur), false);
    201. }
    202. // 如果插入前树非空,新插入的节点应该是红色节点
    203. cur = new Node(data, RED);
    204. Node* tmp = cur;
    205. if (kot(data) < kot(parent->_data))
    206. parent->_left = cur;
    207. else
    208. parent->_right = cur;
    209. cur->_parent = parent;
    210. // 出现连续两个红色节点的情形
    211. while (parent && parent->_clr == RED)
    212. {
    213. Node* grandparent = parent->_parent;
    214. Node* uncle;
    215. if (grandparent->_left == parent)
    216. uncle = grandparent->_right;
    217. else
    218. uncle = grandparent->_left;
    219. // 如果 *uncle 是红色节点
    220. if (uncle && uncle->_clr == RED)
    221. {
    222. parent->_clr = uncle->_clr = BLACK;
    223. grandparent->_clr = RED;
    224. cur = grandparent;
    225. parent = cur->_parent;
    226. }
    227. else  // 如果 *uncle 不存在或者是黑色节点
    228. {
    229. if (grandparent->_left == parent && parent->_left == cur)
    230. {
    231. LL(grandparent);
    232. parent->_clr = BLACK;
    233. grandparent->_clr = RED;
    234. }
    235. else if (grandparent->_right == parent && parent->_right == cur)
    236. {
    237. RR(grandparent);
    238. parent->_clr = BLACK;
    239. grandparent->_clr = RED;
    240. }
    241. else if (grandparent->_left == parent && parent->_right == cur)
    242. {
    243. LR(grandparent);
    244. cur->_clr = BLACK;
    245. grandparent->_clr = RED;
    246. }
    247. else if (grandparent->_right == parent && parent->_left == cur)
    248. {
    249. RL(grandparent);
    250. cur->_clr = BLACK;
    251. grandparent->_clr = RED;
    252. }
    253. break;
    254. }
    255. }
    256. _root->_clr = BLACK;
    257. return std::make_pair(iterator(tmp), true);
    258. }
    259. private:
    260. void Destroy(Node*& root)
    261. {
    262. // 【注意:root 为 _root 或者某个节点的左或右指针的引用】
    263. if (root == nullptr)
    264. return;
    265. Destroy(root->_left);
    266. Destroy(root->_right);
    267. delete root;
    268. root = nullptr;
    269. }
    270. void LL(Node* parent)
    271. {
    272. Node* cur = parent->_left;
    273. Node* curRight = cur->_right;
    274. parent->_left = curRight;
    275. if (curRight)
    276. curRight->_parent = parent;
    277. cur->_right = parent;
    278. Node* tmp = parent->_parent;
    279. parent->_parent = cur;
    280. cur->_parent = tmp;
    281. if (tmp == nullptr)
    282. {
    283. _root = cur;
    284. }
    285. else
    286. {
    287. if (tmp->_left == parent)
    288. tmp->_left = cur;
    289. else
    290. tmp->_right = cur;
    291. }
    292. }
    293. void RR(Node* parent)
    294. {
    295. Node* cur = parent->_right;
    296. Node* curLeft = cur->_left;
    297. parent->_right = curLeft;
    298. if (curLeft)
    299. curLeft->_parent = parent;
    300. cur->_left = parent;
    301. Node* tmp = parent->_parent;
    302. parent->_parent = cur;
    303. cur->_parent = tmp;
    304. if (tmp == nullptr)
    305. {
    306. _root = cur;
    307. }
    308. else
    309. {
    310. if (tmp->_left == parent)
    311. tmp->_left = cur;
    312. else
    313. tmp->_right = cur;
    314. }
    315. }
    316. void LR(Node* parent)
    317. {
    318. Node* cur = parent->_left;
    319. RR(cur);
    320. LL(parent);
    321. }
    322. void RL(Node* parent)
    323. {
    324. Node* cur = parent->_right;
    325. LL(cur);
    326. RR(parent);
    327. }
    328. private:
    329. Node* _root;
    330. };
    331. }


    二、set.h

    1. #pragma once
    2. #include "RBT.h"
    3. namespace yzz
    4. {
    5. template<class K>
    6. class set
    7. {
    8. struct SetKOfT
    9. {
    10. const K& operator()(const K& key)
    11. {
    12. return key;
    13. }
    14. };
    15. typedef RBT RBT;
    16. public:
    17. typedef typename RBT::const_iterator iterator;
    18. typedef typename RBT::const_iterator const_iterator;
    19. const_iterator begin() const
    20. {
    21. return _t.begin();
    22. }
    23. const_iterator end() const
    24. {
    25. return _t.end();
    26. }
    27. std::pairbool> insert(const K& key)
    28. {
    29. std::pair<typename RBT::iterator, bool> ret = _t.insert(key);
    30. return std::pairbool>(ret.first, ret.second);
    31. }
    32. private:
    33. RBT _t;
    34. };
    35. }


    三、map.h

    1. #pragma once
    2. #include "RBT.h"
    3. namespace yzz
    4. {
    5. template<class K, class V>
    6. class map
    7. {
    8. struct MapKOfT
    9. {
    10. const K& operator()(const std::pair<const K, V>& kv)
    11. {
    12. return kv.first;
    13. }
    14. };
    15. typedef RBTconst K, V>, MapKOfT> RBT;
    16. public:
    17. typedef typename RBT::iterator iterator;
    18. typedef typename RBT::const_iterator const_iterator;
    19. iterator begin()
    20. {
    21. return _t.begin();
    22. }
    23. iterator end()
    24. {
    25. return _t.end();
    26. }
    27. const_iterator begin() const
    28. {
    29. return _t.begin();
    30. }
    31. const_iterator end() const
    32. {
    33. return _t.end();
    34. }
    35. std::pairbool> insert(const std::pair<const K, V>& kv)
    36. {
    37. return _t.insert(kv);
    38. }
    39. V& operator[](const K& key)
    40. {
    41. std::pairbool> ret = _t.insert(std::make_pair(key, V()));
    42. return ret.first->second;
    43. }
    44. private:
    45. RBT _t;
    46. };
    47. }


    四、test.cpp

    1. #include "set.h"
    2. #include "map.h"
    3. #include
    4. using namespace std;
    5. void TestSet()
    6. {
    7. int arr[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
    8. yzz::set<int> s;
    9. for (const auto& e : arr)
    10. {
    11. s.insert(e);
    12. }
    13. yzz::set<int>::iterator it = s.begin();
    14. while (it != s.end())
    15. {
    16. // *it = 0; // error
    17. cout << *it << " ";
    18. ++it;
    19. }
    20. // 3 7 9 11 14 15 16 18 26
    21. cout << endl;
    22. }
    23. void PrintMap(const yzz::map<int, int>& m)
    24. {
    25. yzz::map<int, int>::const_iterator it = m.begin();
    26. while (it != m.end())
    27. {
    28. // it->first = 1; // error
    29. // it->second = 2; // error
    30. cout << it->first << " : " << it->second << endl;
    31. ++it;
    32. }
    33. }
    34. void TestMap()
    35. {
    36. int arr[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
    37. yzz::map<int, int> m;
    38. for (const auto& e : arr)
    39. {
    40. m.insert(make_pair(e, e));
    41. }
    42. yzz::map<int, int>::iterator it = m.begin();
    43. while (it != m.end())
    44. {
    45. // it->first = 1; // error
    46. it->second = 2;  // ok
    47. cout << it->first << " : " << it->second << endl;
    48. ++it;
    49. }
    50. for (const auto& kv : m)
    51. {
    52. m[kv.first] = kv.first;
    53. }
    54. PrintMap(m);
    55. }
    56. int main()
    57. {
    58. TestSet();
    59. TestMap();
    60. return 0;
    61. }

  • 相关阅读:
    【智能优化算法-蝙蝠算法】基于混合粒子群和蝙蝠算法求解单目标优化问题附matlab代码
    ComfyUI搭建
    Selenium+Python做web端自动化测试框架实战
    Android异步之旅:探索IntentService
    【RTOS学习】单片机中的C语言
    1147 Heaps
    Codeforces 1043F Make It One(DP+组合数学)
    1.jetson装jtop
    基于pig架构的OAuth2学习记录
    6.3 应用动态内存补丁
  • 原文地址:https://blog.csdn.net/melonyzzZ/article/details/133320976