Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tree.h 20KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : Freyja
  5. * Author : Mongoose
  6. * Website : http://gooseegg.sourceforge.net/
  7. * Email : mongoose@users.sourceforge.net
  8. * Object : Tree
  9. * License : GPL See file COPYING, also (C) 2000 Mongoose
  10. * Comments: Template tree class, which is also the iterator
  11. *
  12. *
  13. * Red-Black trees are a type of binary search trees
  14. * with the properities:
  15. *
  16. * 1. Every node is red or black.
  17. * 2. The root node must be black.
  18. * 3. Every leaf node is black. (null pointers)
  19. * 4. If a node is red, then both its children are black.
  20. * 5. Every simple path from a node to a descendant
  21. * leaf contains the same number of black nodes.
  22. * 6. Any path from the root to a leaf must not have
  23. * adjacent red nodes.
  24. *
  25. *
  26. * This file was generated using Mongoose's C++
  27. * template generator script. <stu7440@westga.edu>
  28. *
  29. *-- History ------------------------------------------------
  30. *
  31. * 2002.02.17:
  32. * Mongoose - Rewritten as a Red-Black tree
  33. *
  34. * 2002.02.16:
  35. * Mongoose - Dug out of cobwebs and fixed up
  36. * Yes, I believe in code reuse! =)
  37. *
  38. * 2000.10.26:
  39. * Mongoose - Created
  40. ================================================================*/
  41. #ifndef __FREYJA_MONGOOSE_TREE_H_
  42. #define __FREYJA_MONGOOSE_TREE_H_
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #ifdef USE_IOSTREAM
  46. #include <iostream.h>
  47. #endif
  48. #ifdef DEBUG_MEMORY
  49. #include <memory_test.h>
  50. #endif
  51. typedef enum
  52. {
  53. _tree_h_black,
  54. _tree_h_red
  55. } _tree_h_color_t;
  56. template <class Key, class Data> class TreeNode
  57. {
  58. public:
  59. TreeNode(Key key, Data data)
  60. {
  61. SetColor(_tree_h_red);
  62. SetData(data);
  63. SetKey(key);
  64. SetParent(NULL);
  65. SetLeft(NULL);
  66. SetRight(NULL);
  67. }
  68. ~TreeNode()
  69. {
  70. TreeNode<Key, Data> *left;
  71. TreeNode<Key, Data> *right;
  72. left = GetLeft();
  73. right = GetRight();
  74. SetParent(NULL);
  75. SetLeft(NULL);
  76. SetRight(NULL);
  77. if (left)
  78. {
  79. left->SetParent(NULL);
  80. delete left;
  81. }
  82. if (right)
  83. {
  84. right->SetParent(NULL);
  85. delete right;
  86. }
  87. }
  88. /// Color /////////////////////////////////////
  89. void SetColor(_tree_h_color_t color)
  90. {
  91. _color = color;
  92. }
  93. _tree_h_color_t GetColor()
  94. {
  95. return _color;
  96. }
  97. /// Left, child ///////////////////////////////
  98. TreeNode<Key, Data> *GetChild()
  99. {
  100. return GetLeft();
  101. }
  102. void SetChild(TreeNode<Key, Data> *tree)
  103. {
  104. Left(tree);
  105. }
  106. TreeNode<Key, Data> *GetLeft()
  107. {
  108. return _left;
  109. }
  110. void SetLeft(TreeNode<Key, Data> *tree)
  111. {
  112. if (tree == this)
  113. {
  114. return;
  115. }
  116. _left = tree;
  117. if (tree)
  118. {
  119. tree->SetParent(this);
  120. }
  121. }
  122. /// Right, sibling ///////////////////////////
  123. TreeNode<Key, Data> *GetSibling()
  124. {
  125. return GetRight();
  126. }
  127. void SetSibling(TreeNode<Key, Data> *tree)
  128. {
  129. SetRight(tree);
  130. }
  131. TreeNode<Key, Data> *GetRight()
  132. {
  133. return _right;
  134. }
  135. void SetRight(TreeNode<Key, Data> *tree)
  136. {
  137. if (tree == this)
  138. {
  139. return;
  140. }
  141. _right = tree;
  142. if (tree)
  143. {
  144. tree->SetParent(this);
  145. }
  146. }
  147. /// Data //////////////////////////////////////
  148. Data GetData()
  149. {
  150. return _data;
  151. }
  152. void SetData(Data data)
  153. {
  154. _data = data;
  155. }
  156. /// Key ////////////////////////////////////////
  157. Key GetKey()
  158. {
  159. return _key;
  160. }
  161. void SetKey(Key key)
  162. {
  163. _key = key;
  164. }
  165. /// Parent /////////////////////////////////////
  166. TreeNode<Key, Data> *GetParent()
  167. {
  168. return _parent;
  169. }
  170. void SetParent(TreeNode<Key, Data> *parent)
  171. {
  172. _parent = parent;
  173. }
  174. /// Misc ///////////////////////////////////////
  175. #ifdef USE_IOSTREAM
  176. void PrintNode()
  177. {
  178. cout << "(" << _key << ", " << _data << ", "
  179. << ((GetColor() == _tree_h_red) ? "Red" : "Black")
  180. << ")";
  181. }
  182. void PrintInorder()
  183. {
  184. if (_left)
  185. {
  186. _left->PrintInorder();
  187. //cout << ", ";
  188. cout << endl;
  189. }
  190. PrintNode();
  191. if (_right)
  192. {
  193. //cout << ", ";
  194. cout << endl;
  195. _right->PrintInorder();
  196. }
  197. }
  198. #endif
  199. void PrintNodeSpecial(void (*print_func_k)(Key), void (*print_func_d)(Data))
  200. {
  201. printf("(");
  202. if (print_func_k)
  203. (*print_func_k)(_key);
  204. printf(", ");
  205. if (print_func_d)
  206. (*print_func_d)(_data);
  207. printf(", %s)", ((GetColor() == _tree_h_red) ? "Red" : "Black"));
  208. }
  209. void PrintInorderSpecial(void (*print_func_k)(Key), void (*print_func_d)(Data))
  210. {
  211. if (_left)
  212. {
  213. _left->PrintInorderSpecial(print_func_k, print_func_d);
  214. printf(",\n");
  215. // printf(", ");
  216. }
  217. PrintNodeSpecial(print_func_k, print_func_d);
  218. if (_right)
  219. {
  220. printf(",\n");
  221. // printf(", ");
  222. _right->PrintInorderSpecial(print_func_k, print_func_d);
  223. }
  224. }
  225. TreeNode<Key, Data> *SearchByData(Data data, bool *error)
  226. {
  227. TreeNode<Key, Data> *tree = NULL;
  228. *error = true;
  229. if (_data == data)
  230. {
  231. *error = false;
  232. return this;
  233. }
  234. if (_left)
  235. {
  236. tree = _left->SearchByData(data, error);
  237. }
  238. if (_right && !tree)
  239. {
  240. tree = _right->SearchByData(data, error);
  241. }
  242. return tree;
  243. }
  244. TreeNode<Key, Data> *SearchByKey(Key key, bool *error)
  245. {
  246. *error = false;
  247. if (_key == key)
  248. {
  249. return this;
  250. }
  251. else if (_left && key < _key)
  252. {
  253. return _left->SearchByKey(key, error);
  254. }
  255. else if (_right)
  256. {
  257. return _right->SearchByKey(key, error);
  258. }
  259. else
  260. {
  261. *error = true;
  262. return 0; //NULL;
  263. }
  264. }
  265. void Insert(TreeNode<Key, Data> *tree)
  266. {
  267. if (!tree || tree == this)
  268. {
  269. return;
  270. }
  271. if (tree->GetKey() < _key)
  272. {
  273. if (!_left)
  274. {
  275. SetLeft(tree);
  276. }
  277. else
  278. {
  279. _left->Insert(tree);
  280. }
  281. }
  282. else
  283. {
  284. if (!_right)
  285. {
  286. SetRight(tree);
  287. }
  288. else
  289. {
  290. _right->Insert(tree);
  291. }
  292. }
  293. }
  294. private:
  295. _tree_h_color_t _color; /* Color of tree node */
  296. Key _key; /* Unique identifer? */
  297. Data _data; /* Data for this tree */
  298. TreeNode<Key, Data> *_left; /* Left or child node */
  299. TreeNode<Key, Data> *_right; /* Right or sibling node */
  300. TreeNode<Key, Data> *_parent; /* Parent of the tree node */
  301. };
  302. ////////////////////////////////////////////////////////////////
  303. // Iterator
  304. ////////////////////////////////////////////////////////////////
  305. template <class Key, class Data> class Tree
  306. {
  307. public:
  308. Tree()
  309. {
  310. _error = false;
  311. _num_elements = 0;
  312. _root = 0;
  313. }
  314. ~Tree()
  315. {
  316. Clear();
  317. }
  318. unsigned int NumElements()
  319. {
  320. return _num_elements;
  321. }
  322. Data SearchByKey(Key key, bool *error)
  323. {
  324. TreeNode<Key, Data> *seeking;
  325. *error = true;
  326. // Mongoose 2002.02.16, Nothing to search
  327. if (!_root)
  328. {
  329. return 0;
  330. }
  331. seeking = _root->SearchByKey(key, error);
  332. if (seeking)
  333. {
  334. return seeking->GetData();
  335. }
  336. return 0;
  337. }
  338. Key SearchByData(Data data, bool *error)
  339. {
  340. TreeNode<Key, Data> *seeking;
  341. *error = true;
  342. // Mongoose 2002.02.16, Nothing to search
  343. if (!_root)
  344. {
  345. return 0;
  346. }
  347. seeking = _root->SearchByData(data, error);
  348. if (seeking)
  349. {
  350. return seeking->GetKey();
  351. }
  352. return 0;
  353. }
  354. void Insert(Key key, Data data)
  355. {
  356. TreeNode<Key, Data> *tree;
  357. tree = new TreeNode<Key, Data>(key, data);
  358. ++_num_elements;
  359. if (_root)
  360. {
  361. _root->Insert(tree);
  362. RestoreRedBlackAfterInsert(tree);
  363. }
  364. else
  365. {
  366. _root = tree;
  367. _root->SetColor(_tree_h_black);
  368. }
  369. }
  370. bool RemoveByData(Data data)
  371. {
  372. bool error;
  373. if (_root)
  374. {
  375. Remove(_root->SearchByData(data, &error));
  376. }
  377. return error;
  378. }
  379. bool RemoveByKey(Key key)
  380. {
  381. bool error;
  382. if (_root)
  383. {
  384. #ifdef OBSOLETE
  385. // Mongoose 2002.02.18, To remove duplicates
  386. erorr = false;
  387. while (!error)
  388. {
  389. #endif
  390. Remove(_root->SearchByKey(key, &error));
  391. #ifdef OBSOLETE
  392. }
  393. #endif
  394. }
  395. return error;
  396. }
  397. void Erase()
  398. {
  399. Clear();
  400. }
  401. void Clear()
  402. {
  403. if (_root)
  404. {
  405. delete _root;
  406. }
  407. _num_elements = 0;
  408. _error = false;
  409. _root = 0;
  410. }
  411. /// Misc //////////////////////////////////////
  412. Data operator [] (Key key)
  413. {
  414. _error = false;
  415. if (_root)
  416. {
  417. return SearchByKey(key, &_error);
  418. }
  419. _error = true;
  420. return 0;
  421. }
  422. #ifdef USE_IOSTREAM
  423. void PrintTree(TreeNode<Key, Data> *tree, unsigned int height,
  424. unsigned int seek, bool rightmost)
  425. {
  426. TreeNode<Key, Data> *left, *right, *parent;
  427. if (!tree)
  428. {
  429. return;
  430. }
  431. parent = tree->GetParent();
  432. if (height == seek)
  433. {
  434. if (!parent)
  435. {
  436. cout << endl << "[height " << height << "] " << endl;
  437. if (tree->GetColor() == _tree_h_red)
  438. {
  439. cout << "*";
  440. }
  441. }
  442. else
  443. {
  444. if (parent->GetColor() == _tree_h_red &&
  445. tree->GetColor() == _tree_h_red)
  446. {
  447. cout << "*";
  448. }
  449. }
  450. cout << "(" << tree->GetKey() << ", "
  451. << ((tree->GetColor() == _tree_h_red) ? "red" : "blk")
  452. << ")";
  453. if (rightmost)
  454. {
  455. cout << endl << "[height " << (height+1) << "] " << endl;
  456. PrintTree(_root, 0, ++seek, true);
  457. }
  458. else
  459. {
  460. cout << " ";
  461. }
  462. return;
  463. }
  464. else if (seek < height)
  465. {
  466. return;
  467. }
  468. left = tree->GetLeft();
  469. right = tree->GetRight();
  470. ++height;
  471. if (left)
  472. {
  473. PrintTree(left, height, seek, false);
  474. }
  475. else
  476. {
  477. cout << "(-, blk) ";
  478. }
  479. if (right)
  480. {
  481. PrintTree(right, height, seek, rightmost);
  482. }
  483. else
  484. {
  485. cout << "(-, blk) ";
  486. }
  487. if (parent)
  488. {
  489. if (parent->GetRight() != tree)
  490. {
  491. cout << " | ";
  492. }
  493. }
  494. }
  495. void PrintAsTree()
  496. {
  497. PrintTree(_root, 0, 0, true);
  498. cout << endl << "Nodes marked with * are in error" << endl;
  499. }
  500. void Print()
  501. {
  502. cout << "Tree: " << _num_elements <<" elements {" << endl;
  503. if (_root)
  504. {
  505. cout << "Root: ";
  506. _root->PrintNode();
  507. cout << endl;
  508. _root->PrintInorder();
  509. }
  510. cout << endl << "}" << endl;
  511. }
  512. #endif
  513. void PrintSpecial(void (*print_func_k)(Key), void (*print_func_d)(Data))
  514. {
  515. printf("Tree: %i elements {\n", _num_elements);
  516. if (_root && print_func_k && print_func_d)
  517. {
  518. printf("Root: ");
  519. _root->PrintNodeSpecial(print_func_k, print_func_d);
  520. printf("\n");
  521. _root->PrintInorderSpecial(print_func_k, print_func_d);
  522. }
  523. printf("\n}\n");
  524. }
  525. Key Root()
  526. {
  527. if (_root)
  528. {
  529. return _root->GetKey();
  530. }
  531. return 0;
  532. }
  533. bool Error()
  534. {
  535. return _error;
  536. }
  537. bool IsValidRedBlackTree()
  538. {
  539. return IsValidRedBlackTreeCheck(_root, true);
  540. }
  541. private:
  542. TreeNode<Key, Data> *GetSuccessor(TreeNode<Key, Data> *tree)
  543. {
  544. TreeNode<Key, Data> *successor;
  545. successor = tree->GetRight();
  546. if (successor)
  547. {
  548. while (successor->GetLeft())
  549. {
  550. successor = successor->GetLeft();
  551. }
  552. return successor;
  553. }
  554. else
  555. {
  556. successor = tree->GetParent();
  557. while (tree == successor->GetRight())
  558. {
  559. tree = successor;
  560. successor = successor->GetParent();
  561. }
  562. if (successor == _root)
  563. {
  564. return NULL;
  565. }
  566. return successor;
  567. }
  568. }
  569. TreeNode<Key, Data> *GetPredecessor(TreeNode<Key, Data> *tree)
  570. {
  571. TreeNode<Key, Data> *predecessor;
  572. predecessor = tree->GetLeft();
  573. if (predecessor)
  574. {
  575. while (predecessor->GetRight())
  576. {
  577. predecessor = predecessor->GetRight();
  578. }
  579. return predecessor;
  580. }
  581. else
  582. {
  583. predecessor = tree->GetParent();
  584. while (tree == predecessor->GetLeft())
  585. {
  586. if (predecessor == _root)
  587. {
  588. return NULL;
  589. }
  590. tree = predecessor;
  591. predecessor = predecessor->GetParent();
  592. }
  593. return predecessor;
  594. }
  595. }
  596. bool IsValidRedBlackTreeCheck(TreeNode<Key, Data> *current, bool valid)
  597. {
  598. TreeNode<Key, Data> *right, *left;
  599. _tree_h_color_t color_red;
  600. if (!current)
  601. {
  602. return valid;
  603. }
  604. // Mongoose 2002.02.19, Check for a red root
  605. if (!current->GetParent() && current->GetColor() == _tree_h_red)
  606. {
  607. return false;
  608. }
  609. color_red = (current->GetColor() == _tree_h_red);
  610. left = current->GetLeft();
  611. right = current->GetRight();
  612. // Mongoose 2002.02.19, Check for adj red nodes
  613. if (left)
  614. {
  615. if (color_red && left->GetColor() == _tree_h_red)
  616. {
  617. return false;
  618. }
  619. if (!IsValidRedBlackTreeCheck(left, valid))
  620. return false;
  621. }
  622. if (right)
  623. {
  624. if (color_red && right->GetColor() == _tree_h_red)
  625. {
  626. return false;
  627. }
  628. if (!IsValidRedBlackTreeCheck(right, valid))
  629. return false;
  630. }
  631. return true;
  632. }
  633. void RotateLeft(TreeNode<Key, Data> *tree)
  634. {
  635. TreeNode<Key, Data> *right, *right_leftchild, *parent, *uncle;
  636. if (!tree || !_root)
  637. {
  638. return;
  639. }
  640. // Get tree's right node
  641. right = tree->GetRight();
  642. // Get right node's left child
  643. right_leftchild = NULL;
  644. if (right)
  645. {
  646. right_leftchild = right->GetLeft();
  647. }
  648. // Set tree's right node to right's left child
  649. tree->SetRight(right_leftchild);
  650. // Child now has a new parent
  651. if (right_leftchild)
  652. {
  653. right_leftchild->SetParent(tree);
  654. }
  655. // Right also has a new parent
  656. if (right)
  657. {
  658. right->SetParent(tree->GetParent());
  659. }
  660. // Get parent
  661. parent = tree->GetParent();
  662. if (parent) // Not root
  663. {
  664. uncle = parent->GetLeft();
  665. // Mix up at hosptial, switch parent's children!
  666. if (tree == uncle)
  667. {
  668. parent->SetLeft(right);
  669. }
  670. else
  671. {
  672. parent->SetRight(right);
  673. }
  674. }
  675. else // TreeNode 'tree' was root, so now right is root
  676. {
  677. _root = right;
  678. }
  679. if (right)
  680. {
  681. // TreeNode 'tree' is now right's left child
  682. right->SetLeft(tree);
  683. if (tree)
  684. {
  685. tree->SetParent(right);
  686. }
  687. }
  688. }
  689. void RotateRight(TreeNode<Key, Data> *tree)
  690. {
  691. TreeNode<Key, Data> *left, *left_rightchild, *parent, *uncle;
  692. if (!tree || !_root)
  693. {
  694. return;
  695. }
  696. left = tree->GetLeft();
  697. left_rightchild = NULL;
  698. if (left)
  699. {
  700. left_rightchild = left->GetRight();
  701. }
  702. tree->SetLeft(left_rightchild);
  703. if (left_rightchild)
  704. {
  705. left_rightchild->SetParent(tree);
  706. }
  707. if (left)
  708. {
  709. left->SetParent(tree->GetParent());
  710. }
  711. parent = tree->GetParent();
  712. if (parent) //if node is not the root
  713. {
  714. uncle = parent->GetRight();
  715. if (tree == uncle)
  716. {
  717. parent->SetRight(left);
  718. }
  719. else
  720. {
  721. parent->SetLeft(left);
  722. }
  723. }
  724. else
  725. {
  726. _root = left;
  727. }
  728. left->SetRight(tree);
  729. if (tree)
  730. {
  731. tree->SetParent(left);
  732. }
  733. }
  734. void TreeNodeShallowCopy(TreeNode<Key, Data> *src,
  735. TreeNode<Key, Data> *dest, bool no_links)
  736. {
  737. if (!src || !dest)
  738. {
  739. return;
  740. }
  741. dest->SetKey(src->GetKey());
  742. dest->SetData(src->GetData());
  743. dest->SetColor(src->GetColor());
  744. if (!no_links)
  745. {
  746. dest->SetRight(src->GetRight());
  747. dest->SetLeft(src->GetLeft());
  748. dest->SetParent(src->GetParent());
  749. }
  750. }
  751. void Remove(TreeNode<Key, Data> *tree)
  752. {
  753. TreeNode<Key, Data> *left, *right, *parent, *prev, *cur;
  754. // Mongoose 2002.02.16, Nothing to remove
  755. if (!tree || !_root)
  756. {
  757. return;
  758. }
  759. left = tree->GetLeft();
  760. right = tree->GetRight();
  761. parent = tree->GetParent();
  762. if (!left || !right)
  763. {
  764. prev = tree;
  765. }
  766. else
  767. {
  768. prev = GetSuccessor(tree);
  769. }
  770. if (prev->GetLeft())
  771. {
  772. cur = prev->GetLeft();
  773. }
  774. else
  775. {
  776. cur = prev->GetRight();
  777. }
  778. if (cur)
  779. {
  780. cur->SetParent(prev->GetParent());
  781. }
  782. if (!prev->GetParent())
  783. {
  784. _root = cur;
  785. }
  786. else
  787. {
  788. parent = prev->GetParent();
  789. if (prev == parent->GetLeft())
  790. {
  791. parent->SetLeft(cur);
  792. }
  793. else
  794. {
  795. parent->SetRight(cur);
  796. }
  797. }
  798. if (prev != tree)
  799. {
  800. TreeNodeShallowCopy(prev, tree, true);
  801. if (prev->GetParent())
  802. {
  803. if (prev == (prev->GetParent())->GetLeft())
  804. (prev->GetParent())->SetLeft(tree);
  805. else if (prev == (prev->GetParent())->GetRight())
  806. (prev->GetParent())->SetRight(tree);
  807. }
  808. }
  809. --_num_elements;
  810. if (prev)
  811. {
  812. prev->SetRight(NULL);
  813. prev->SetParent(NULL);
  814. prev->SetLeft(NULL);
  815. delete prev;
  816. }
  817. if (tree->GetColor() == _tree_h_black)
  818. {
  819. RestoreRedBlackAfterRemove(cur);
  820. }
  821. }
  822. void RestoreRedBlackAfterRemove(TreeNode<Key, Data> *tree)
  823. {
  824. TreeNode<Key, Data> *parent, *sibling, *sleft, *sright;
  825. if (!tree || !_root)
  826. {
  827. return;
  828. }
  829. parent = tree->GetParent();
  830. while ((tree != _root) && (parent->GetColor() == _tree_h_black))
  831. {
  832. if (tree == parent->GetLeft())
  833. {
  834. sibling = parent->GetRight();
  835. if (sibling && sibling->GetColor() == _tree_h_red)
  836. {
  837. sibling->SetColor(_tree_h_black);
  838. parent->SetColor(_tree_h_red);
  839. RotateLeft(parent);
  840. sibling = parent->GetRight();
  841. }
  842. if (sibling)
  843. {
  844. sleft = sibling->GetLeft();
  845. sright = sibling->GetRight();
  846. }
  847. else
  848. {
  849. sleft = sright = NULL;
  850. }
  851. if (sright && sright->GetColor() == _tree_h_black &&
  852. sleft && sleft->GetColor() ==_tree_h_black)
  853. {
  854. sibling->SetColor(_tree_h_red);
  855. tree = parent;
  856. }
  857. else
  858. {
  859. if (sright && sright->GetColor() == _tree_h_black)
  860. {
  861. sibling->SetColor(_tree_h_red);
  862. sleft->SetColor(_tree_h_black);
  863. RotateRight(sibling);
  864. sibling = parent->GetRight();
  865. }
  866. sibling->SetColor(parent->GetColor());
  867. parent->SetColor(_tree_h_black);
  868. sright->SetColor(_tree_h_black);
  869. RotateLeft(parent);
  870. tree = _root;
  871. }
  872. }
  873. else
  874. {
  875. sibling = parent->GetLeft();
  876. if (sibling && sibling->GetColor() == _tree_h_red)
  877. {
  878. sibling->SetColor(_tree_h_black);
  879. parent->SetColor(_tree_h_red);
  880. RotateLeft(parent);
  881. sibling = parent->GetLeft();
  882. }
  883. if (sibling)
  884. {
  885. sleft = sibling->GetLeft();
  886. sright = sibling->GetRight();
  887. }
  888. else
  889. {
  890. sleft = sright = NULL;
  891. }
  892. if (sright && sright->GetColor() == _tree_h_black &&
  893. sleft && sleft->GetColor() ==_tree_h_black)
  894. {
  895. sibling->SetColor(_tree_h_red);
  896. tree = parent;
  897. }
  898. else
  899. {
  900. if (sleft && sleft->GetColor() == _tree_h_black)
  901. {
  902. sibling->SetColor(_tree_h_red);
  903. sright->SetColor(_tree_h_black);
  904. RotateLeft(sibling);
  905. sibling = parent->GetLeft();
  906. }
  907. sibling->SetColor(parent->GetColor());
  908. parent->SetColor(_tree_h_black);
  909. sleft->SetColor(_tree_h_black);
  910. RotateRight(parent);
  911. tree = _root;
  912. }
  913. }
  914. parent = tree->GetParent();
  915. }
  916. tree->SetColor(_tree_h_black);
  917. }
  918. void RestoreRedBlackAfterInsert(TreeNode<Key, Data> *tree)
  919. {
  920. TreeNode<Key, Data> *parent, *grandparent, *uncle;
  921. if (!tree || !_root || tree == _root)
  922. {
  923. return;
  924. }
  925. tree->SetColor(_tree_h_red);
  926. parent = tree->GetParent();
  927. while ((tree != _root) && (parent->GetColor() == _tree_h_red))
  928. {
  929. grandparent = parent->GetParent();
  930. if (parent == grandparent->GetLeft())
  931. {
  932. uncle = grandparent->GetRight();
  933. if (uncle && uncle->GetColor() == _tree_h_red)
  934. {
  935. // Case 1 - Change the colors
  936. parent->SetColor(_tree_h_black);
  937. uncle->SetColor(_tree_h_black);
  938. grandparent->SetColor(_tree_h_red);
  939. // Move up the tree
  940. tree = grandparent;
  941. }
  942. else // Uncle is a black node
  943. {
  944. if (tree == parent->GetRight())
  945. {
  946. // Case 2 - Move up and rotate
  947. tree = parent;
  948. RotateLeft(tree);
  949. }
  950. // Case 3 - Make no changes to _root tree
  951. // Change colors for Case 2 / Case 3
  952. parent->SetColor(_tree_h_black);
  953. grandparent->SetColor(_tree_h_red);
  954. RotateRight(grandparent);
  955. }
  956. }
  957. else // TreeNode 'tree' is in right subtree
  958. {
  959. uncle = grandparent->GetLeft();
  960. if (uncle && uncle->GetColor() == _tree_h_red)
  961. {
  962. // Case 1 - Change the colors
  963. parent->SetColor(_tree_h_black);
  964. uncle->SetColor(_tree_h_black);
  965. grandparent->SetColor(_tree_h_red);
  966. // Move up the tree
  967. tree = grandparent;
  968. }
  969. else // Uncle is a black node
  970. {
  971. if (tree == parent->GetLeft())
  972. {
  973. // Case 2 - Move up and rotate
  974. tree = parent;
  975. RotateRight(tree);
  976. }
  977. // Case 3 - Make no changes to _root tree
  978. // Change colors for Case 2 / Case 3
  979. parent->SetColor(_tree_h_black);
  980. grandparent->SetColor(_tree_h_red);
  981. RotateLeft(grandparent);
  982. }
  983. }
  984. // Have to adjust parent for new tree node
  985. parent = tree->GetParent();
  986. }
  987. // Mongoose 2002.02.17, Color root black ( heh )
  988. _root->SetColor(_tree_h_black);
  989. }
  990. bool _error; /* Error reporting for operator use */
  991. unsigned int _num_elements; /* Number of nodes in this tree */
  992. TreeNode<Key, Data> *_root; /* Root node */
  993. };
  994. #endif