RBTree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #undef min
  25. #undef max
  26. #include <cstdlib>
  27. #include "Assert.h"
  28. #include "Types.h"
  29. #include "Allocator.h"
  30. namespace crown
  31. {
  32. enum RBTreeNodeColor { RED, BLACK };
  33. template<typename TKey, typename TValue>
  34. struct RBTreePair
  35. {
  36. public:
  37. RBTreePair(const TKey& k, const TValue& v):
  38. key(k), value(v)
  39. { }
  40. RBTreePair(const RBTreePair& p):
  41. key(p.key), value(p.value)
  42. { }
  43. TKey key;
  44. TValue value;
  45. };
  46. template<typename TKey, typename TValue>
  47. struct RBTreeNode
  48. {
  49. RBTreeNode(const TKey& key, const TValue& value):
  50. item(key, value)
  51. {
  52. left = NULL;
  53. right = NULL;
  54. parent = NULL;
  55. color = BLACK;
  56. }
  57. RBTreePair<TKey, TValue> item;
  58. struct RBTreeNode<TKey, TValue>* left;
  59. struct RBTreeNode<TKey, TValue>* right;
  60. struct RBTreeNode<TKey, TValue>* parent;
  61. enum RBTreeNodeColor color;
  62. };
  63. template<typename TKey, typename TValue>
  64. class RBTree
  65. {
  66. protected:
  67. typedef RBTreeNode<TKey, TValue> Node;
  68. typedef RBTreePair<TKey, TValue> Pair;
  69. public:
  70. RBTree(Allocator& allocator);
  71. ~RBTree();
  72. Pair& add(const TKey& key, const TValue& value);
  73. void remove(const TKey& key);
  74. bool contains(const TKey& key) const;
  75. void clear();
  76. inline int32_t size() const
  77. {
  78. return m_size;
  79. }
  80. protected:
  81. Node* find_or_add(TKey key);
  82. private:
  83. Allocator& m_allocator;
  84. Node* m_root;
  85. Node* m_sentinel;
  86. int32_t m_size;
  87. Node* predecessor(Node* n) const;
  88. Node* successor(Node* n) const;
  89. Node* min(Node* n) const;
  90. Node* max(Node* n) const;
  91. inline void rotate_left(Node* n);
  92. inline void rotate_right(Node* n);
  93. Node* inner_find(TKey key) const;
  94. void add_fixup(Node* n);
  95. void inner_clear(Node* n);
  96. #ifdef RBTREE_VERIFY
  97. int32_t dbg_verify(Node* n) const;
  98. #endif
  99. };
  100. template<typename TKey, typename TValue>
  101. RBTree<TKey, TValue>::RBTree(Allocator& allocator) : m_allocator(allocator)
  102. {
  103. m_sentinel = CE_NEW(m_allocator, Node)(TKey(), TValue());
  104. m_root = m_sentinel;
  105. m_size = 0;
  106. }
  107. template<typename TKey, typename TValue>
  108. RBTree<TKey, TValue>::~RBTree()
  109. {
  110. clear();
  111. CE_DELETE(m_allocator, m_sentinel);
  112. }
  113. template<typename TKey, typename TValue>
  114. RBTreePair<TKey, TValue>& RBTree<TKey, TValue>::add(const TKey& key, const TValue& value)
  115. {
  116. Node* n = CE_NEW(m_allocator, Node)(key, value);
  117. n->color = RED;
  118. n->left = m_sentinel;
  119. n->right = m_sentinel;
  120. Pair& pair = n->item;
  121. Node* x = m_root;
  122. Node* y = NULL;
  123. if (x == m_sentinel)
  124. {
  125. m_root = n;
  126. }
  127. else
  128. {
  129. while (x != m_sentinel)
  130. {
  131. y = x;
  132. if (key < x->item.key)
  133. {
  134. x = x->left;
  135. }
  136. else
  137. {
  138. x = x->right;
  139. }
  140. }
  141. if (key < y->item.key)
  142. {
  143. y->left = n;
  144. }
  145. else
  146. {
  147. y->right = n;
  148. }
  149. n->parent = y;
  150. }
  151. add_fixup(n);
  152. m_root->color = BLACK;
  153. m_size++;
  154. #ifdef RBTREE_VERIFY
  155. dbg_verify(m_root);
  156. #endif
  157. return pair;
  158. }
  159. template<typename TKey, typename TValue>
  160. void RBTree<TKey, TValue>::remove(const TKey& key)
  161. {
  162. Node* n = inner_find(key);
  163. if (!(n->item.key == key))
  164. {
  165. return;
  166. }
  167. Node* x;
  168. Node* y;
  169. if (n->left == m_sentinel || n->right == m_sentinel)
  170. {
  171. y = n;
  172. }
  173. else
  174. {
  175. y = successor(n);
  176. }
  177. if (y->left != m_sentinel)
  178. {
  179. x = y->left;
  180. }
  181. else
  182. {
  183. x = y->right;
  184. }
  185. x->parent = y->parent;
  186. if (y->parent != NULL)
  187. {
  188. if (y == y->parent->left)
  189. {
  190. y->parent->left = x;
  191. }
  192. else
  193. {
  194. y->parent->right = x;
  195. }
  196. }
  197. else
  198. {
  199. m_root = x;
  200. }
  201. if (y != n)
  202. {
  203. n->item = y->item;
  204. }
  205. //Do the fixup
  206. if (y->color == BLACK)
  207. {
  208. Node* y;
  209. while (x != m_root && x->color == BLACK)
  210. {
  211. if (x == x->parent->left)
  212. {
  213. y = x->parent->right;
  214. if (y->color == RED)
  215. {
  216. y->color = BLACK;
  217. x->parent->color = RED;
  218. rotate_left(x->parent);
  219. y = x->parent->right;
  220. }
  221. if (y->left->color == BLACK && y->right->color == BLACK)
  222. {
  223. y->color = RED;
  224. x = x->parent;
  225. }
  226. else
  227. {
  228. if (y->right->color == BLACK)
  229. {
  230. y->left->color = BLACK;
  231. y->color = RED;
  232. rotate_right(y);
  233. y = x->parent->right;
  234. }
  235. y->color = x->parent->color;
  236. x->parent->color = BLACK;
  237. y->right->color = BLACK;
  238. rotate_left(x->parent);
  239. x = m_root;
  240. }
  241. }
  242. else
  243. {
  244. y = x->parent->left;
  245. if (y->color == RED)
  246. {
  247. y->color = BLACK;
  248. x->parent->color = RED;
  249. rotate_right(x->parent);
  250. y = x->parent->left;
  251. }
  252. if (y->right->color == BLACK && y->left->color == BLACK)
  253. {
  254. y->color = RED;
  255. x = x->parent;
  256. }
  257. else
  258. {
  259. if (y->left->color == BLACK)
  260. {
  261. y->right->color = BLACK;
  262. y->color = RED;
  263. rotate_left(y);
  264. y = x->parent->left;
  265. }
  266. y->color = x->parent->color;
  267. x->parent->color = BLACK;
  268. y->left->color = BLACK;
  269. rotate_right(x->parent);
  270. x = m_root;
  271. }
  272. }
  273. }
  274. x->color = BLACK;
  275. }
  276. CE_DELETE(m_allocator, y);
  277. m_size -= 1;
  278. #ifdef RBTREE_VERIFY
  279. dbg_verify(m_root);
  280. #endif
  281. }
  282. template<typename TKey, typename TValue>
  283. RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::find_or_add(TKey key)
  284. {
  285. Node* p = inner_find(key);
  286. if (p != m_sentinel && p->item.key == key)
  287. {
  288. return p;
  289. }
  290. Node* n = CE_NEW(m_allocator, Node)(key, TValue());
  291. n->color = RED;
  292. n->left = m_sentinel;
  293. n->right = m_sentinel;
  294. if (p == m_sentinel)
  295. {
  296. m_root = n;
  297. }
  298. else
  299. {
  300. if (key < p->item.key)
  301. {
  302. p->left = n;
  303. }
  304. else
  305. {
  306. p->right = n;
  307. }
  308. n->parent = p;
  309. }
  310. add_fixup(n);
  311. m_root->color = BLACK;
  312. m_size++;
  313. #ifdef RBTREE_VERIFY
  314. dbg_verify(m_root);
  315. #endif
  316. return n;
  317. }
  318. template<typename TKey, typename TValue>
  319. void RBTree<TKey, TValue>::clear()
  320. {
  321. Node* tmp = m_root;
  322. m_root = m_sentinel;
  323. inner_clear(tmp);
  324. m_size = 0;
  325. }
  326. template<typename TKey, typename TValue>
  327. bool RBTree<TKey, TValue>::contains(const TKey& key) const
  328. {
  329. Node* n = inner_find(key);
  330. if (n == m_sentinel || !(n->item.key == key))
  331. {
  332. return false;
  333. }
  334. return true;
  335. }
  336. /* Inner utilities */
  337. template<typename TKey, typename TValue>
  338. RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::inner_find(TKey key) const
  339. {
  340. Node* x = m_root;
  341. while (x != m_sentinel)
  342. {
  343. if (key > x->item.key)
  344. {
  345. if (x->right == m_sentinel)
  346. {
  347. return x;
  348. }
  349. x = x->right;
  350. }
  351. else if (key < x->item.key)
  352. {
  353. if (x->left == m_sentinel)
  354. {
  355. return x;
  356. }
  357. x = x->left;
  358. }
  359. else
  360. {
  361. break;
  362. }
  363. }
  364. return x;
  365. }
  366. template<typename TKey, typename TValue>
  367. void RBTree<TKey, TValue>::add_fixup(Node* n)
  368. {
  369. Node* x;
  370. Node* y;
  371. while (n!=m_root && n->parent->color==RED)
  372. {
  373. x = n->parent;
  374. if (x == x->parent->left)
  375. {
  376. y = x->parent->right;
  377. if (y->color == RED)
  378. {
  379. x->color = BLACK;
  380. y->color = BLACK;
  381. x->parent->color = RED;
  382. n = x->parent;
  383. continue;
  384. }
  385. else
  386. {
  387. if (n == x->right)
  388. {
  389. n = x;
  390. rotate_left(n);
  391. x = n->parent;
  392. }
  393. x->color = BLACK;
  394. x->parent->color = RED;
  395. rotate_right(x->parent);
  396. }
  397. }
  398. else
  399. {
  400. y = x->parent->left;
  401. if (y->color == RED)
  402. {
  403. x->color = BLACK;
  404. y->color = BLACK;
  405. x->parent->color = RED;
  406. n = x->parent;
  407. continue;
  408. }
  409. else
  410. {
  411. if (n == x->left)
  412. {
  413. n = x;
  414. rotate_right(n);
  415. x = n->parent;
  416. }
  417. x->color = BLACK;
  418. x->parent->color = RED;
  419. rotate_left(x->parent);
  420. }
  421. }
  422. }
  423. }
  424. template<typename TKey, typename TValue>
  425. void RBTree<TKey, TValue>::inner_clear(Node* n)
  426. {
  427. if (n == m_sentinel)
  428. {
  429. return;
  430. }
  431. Node* tmp;
  432. tmp = n->left;
  433. n->left = NULL;
  434. inner_clear(tmp);
  435. tmp = n->right;
  436. n->right = NULL;
  437. inner_clear(tmp);
  438. CE_DELETE(m_allocator, n);
  439. }
  440. template<typename TKey, typename TValue>
  441. RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::predecessor(Node* x) const
  442. {
  443. if (x->left != m_sentinel)
  444. {
  445. return max(x->left);
  446. }
  447. Node* y = x->parent;
  448. while (y != NULL && x == y->left)
  449. {
  450. x = y;
  451. y = y->parent;
  452. }
  453. return y;
  454. }
  455. template<typename TKey, typename TValue>
  456. RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::successor(Node* x) const
  457. {
  458. if (x->right != m_sentinel)
  459. {
  460. return min(x->right);
  461. }
  462. Node* y = x->parent;
  463. while (y != NULL && x == y->right)
  464. {
  465. x = y;
  466. y = y->parent;
  467. }
  468. return y;
  469. }
  470. template<typename TKey, typename TValue>
  471. RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::min(Node* x) const
  472. {
  473. if (x == m_sentinel)
  474. {
  475. return x;
  476. }
  477. while (x->left != m_sentinel)
  478. {
  479. x = x->left;
  480. }
  481. return x;
  482. }
  483. template<typename TKey, typename TValue>
  484. RBTreeNode<TKey, TValue>* RBTree<TKey, TValue>::max(Node* x) const
  485. {
  486. if (x == m_sentinel)
  487. {
  488. return x;
  489. }
  490. while (x->right != m_sentinel)
  491. {
  492. x = x->right;
  493. }
  494. return x;
  495. }
  496. template<typename TKey, typename TValue>
  497. inline void RBTree<TKey, TValue>::rotate_left(Node* x)
  498. {
  499. Node* y = x->right;
  500. x->right = y->left;
  501. if (y->left != m_sentinel)
  502. {
  503. y->left->parent = x;
  504. }
  505. y->parent = x->parent;
  506. if (x->parent == NULL)
  507. {
  508. m_root = y;
  509. }
  510. else
  511. {
  512. if (x == x->parent->left)
  513. {
  514. x->parent->left = y;
  515. }
  516. else
  517. {
  518. x->parent->right = y;
  519. }
  520. }
  521. y->left = x;
  522. x->parent = y;
  523. }
  524. template<typename TKey, typename TValue>
  525. inline void RBTree<TKey, TValue>::rotate_right(Node* x)
  526. {
  527. Node* y = x->left;
  528. x->left = y->right;
  529. if (y->right != m_sentinel)
  530. {
  531. y->right->parent = x;
  532. }
  533. y->parent = x->parent;
  534. if (x->parent == NULL)
  535. {
  536. m_root = y;
  537. }
  538. else
  539. {
  540. if (x == x->parent->left)
  541. {
  542. x->parent->left = y;
  543. }
  544. else
  545. {
  546. x->parent->right = y;
  547. }
  548. }
  549. y->right = x;
  550. x->parent = y;
  551. }
  552. #ifdef RBTREE_VERIFY
  553. template<typename TKey, typename TValue>
  554. int32_t RBTree<TKey, TValue>::dbg_verify(Node* n) const
  555. {
  556. if (n == m_sentinel)
  557. {
  558. return 0;
  559. }
  560. if (n->left != m_sentinel)
  561. {
  562. CE_ASSERT(n->left->parent == n);
  563. CE_ASSERT(n->item.key > n->left->item.key);
  564. }
  565. if (n->right != m_sentinel)
  566. {
  567. CE_ASSERT(n->right->parent == n);
  568. CE_ASSERT(n->item.key < n->right->item.key);
  569. }
  570. int32_t bhL = dbg_verify(n->left);
  571. int32_t bhR = dbg_verify(n->right);
  572. CE_ASSERT(bhL == bhR);
  573. if (n->color == BLACK)
  574. {
  575. bhL += 1;
  576. }
  577. else
  578. {
  579. if (n->parent != NULL && n->parent->color == RED)
  580. {
  581. CE_ASSERT(false);
  582. }
  583. }
  584. return bhL;
  585. }
  586. #endif
  587. } // namespace crown