Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Tree.h 28KB

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