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 29KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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;
  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;
  424. if (_root) {
  425. #ifdef OBSOLETE
  426. // Mongoose 2002.02.18, To remove duplicates
  427. erorr = 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: %i 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. {
  587. TreeNode<Key, Data> *successor;
  588. successor = tree->GetRight();
  589. if (successor)
  590. {
  591. while (successor->GetLeft())
  592. {
  593. successor = successor->GetLeft();
  594. }
  595. return successor;
  596. }
  597. else
  598. {
  599. successor = tree->GetParent();
  600. while (tree == successor->GetRight())
  601. {
  602. tree = successor;
  603. successor = successor->GetParent();
  604. }
  605. if (successor == _root)
  606. {
  607. return NULL;
  608. }
  609. return successor;
  610. }
  611. }
  612. TreeNode<Key, Data> *GetPredecessor(TreeNode<Key, Data> *tree)
  613. {
  614. TreeNode<Key, Data> *predecessor;
  615. predecessor = tree->GetLeft();
  616. if (predecessor)
  617. {
  618. while (predecessor->GetRight())
  619. {
  620. predecessor = predecessor->GetRight();
  621. }
  622. return predecessor;
  623. }
  624. else
  625. {
  626. predecessor = tree->GetParent();
  627. while (tree == predecessor->GetLeft())
  628. {
  629. if (predecessor == _root)
  630. {
  631. return NULL;
  632. }
  633. tree = predecessor;
  634. predecessor = predecessor->GetParent();
  635. }
  636. return predecessor;
  637. }
  638. }
  639. bool IsValidRedBlackTreeCheck(TreeNode<Key, Data> *current, bool valid)
  640. {
  641. TreeNode<Key, Data> *right, *left;
  642. _tree_h_color_t color_red;
  643. if (!current)
  644. {
  645. return valid;
  646. }
  647. // Mongoose 2002.02.19, Check for a red root
  648. if (!current->GetParent() && current->GetColor() == _tree_h_red)
  649. {
  650. return false;
  651. }
  652. color_red = (current->GetColor() == _tree_h_red);
  653. left = current->GetLeft();
  654. right = current->GetRight();
  655. // Mongoose 2002.02.19, Check for adj red nodes
  656. if (left)
  657. {
  658. if (color_red && left->GetColor() == _tree_h_red)
  659. {
  660. return false;
  661. }
  662. if (!IsValidRedBlackTreeCheck(left, valid))
  663. return false;
  664. }
  665. if (right)
  666. {
  667. if (color_red && right->GetColor() == _tree_h_red)
  668. {
  669. return false;
  670. }
  671. if (!IsValidRedBlackTreeCheck(right, valid))
  672. return false;
  673. }
  674. return true;
  675. }
  676. void RotateLeft(TreeNode<Key, Data> *tree)
  677. {
  678. TreeNode<Key, Data> *right, *right_leftchild, *parent, *uncle;
  679. if (!tree || !_root)
  680. {
  681. return;
  682. }
  683. // Get tree's right node
  684. right = tree->GetRight();
  685. // Get right node's left child
  686. right_leftchild = NULL;
  687. if (right)
  688. {
  689. right_leftchild = right->GetLeft();
  690. }
  691. // Set tree's right node to right's left child
  692. tree->SetRight(right_leftchild);
  693. // Child now has a new parent
  694. if (right_leftchild)
  695. {
  696. right_leftchild->SetParent(tree);
  697. }
  698. // Right also has a new parent
  699. if (right)
  700. {
  701. right->SetParent(tree->GetParent());
  702. }
  703. // Get parent
  704. parent = tree->GetParent();
  705. if (parent) // Not root
  706. {
  707. uncle = parent->GetLeft();
  708. // Mix up at hosptial, switch parent's children!
  709. if (tree == uncle)
  710. {
  711. parent->SetLeft(right);
  712. }
  713. else
  714. {
  715. parent->SetRight(right);
  716. }
  717. }
  718. else // TreeNode 'tree' was root, so now right is root
  719. {
  720. _root = right;
  721. }
  722. if (right)
  723. {
  724. // TreeNode 'tree' is now right's left child
  725. right->SetLeft(tree);
  726. if (tree)
  727. {
  728. tree->SetParent(right);
  729. }
  730. }
  731. }
  732. void RotateRight(TreeNode<Key, Data> *tree)
  733. {
  734. TreeNode<Key, Data> *left, *left_rightchild, *parent, *uncle;
  735. if (!tree || !_root)
  736. {
  737. return;
  738. }
  739. left = tree->GetLeft();
  740. left_rightchild = NULL;
  741. if (left)
  742. {
  743. left_rightchild = left->GetRight();
  744. }
  745. tree->SetLeft(left_rightchild);
  746. if (left_rightchild)
  747. {
  748. left_rightchild->SetParent(tree);
  749. }
  750. if (left)
  751. {
  752. left->SetParent(tree->GetParent());
  753. }
  754. parent = tree->GetParent();
  755. if (parent) //if node is not the root
  756. {
  757. uncle = parent->GetRight();
  758. if (tree == uncle)
  759. {
  760. parent->SetRight(left);
  761. }
  762. else
  763. {
  764. parent->SetLeft(left);
  765. }
  766. }
  767. else
  768. {
  769. _root = left;
  770. }
  771. left->SetRight(tree);
  772. if (tree)
  773. {
  774. tree->SetParent(left);
  775. }
  776. }
  777. void TreeNodeShallowCopy(TreeNode<Key, Data> *src,
  778. TreeNode<Key, Data> *dest, bool no_links)
  779. {
  780. if (!src || !dest)
  781. {
  782. return;
  783. }
  784. dest->SetKey(src->GetKey());
  785. dest->SetData(src->GetData());
  786. dest->SetColor(src->GetColor());
  787. if (!no_links)
  788. {
  789. dest->SetRight(src->GetRight());
  790. dest->SetLeft(src->GetLeft());
  791. dest->SetParent(src->GetParent());
  792. }
  793. }
  794. void Remove(TreeNode<Key, Data> *tree)
  795. {
  796. TreeNode<Key, Data> *left, *right, *parent, *prev, *cur;
  797. // Mongoose 2002.02.16, Nothing to remove
  798. if (!tree || !_root)
  799. {
  800. return;
  801. }
  802. left = tree->GetLeft();
  803. right = tree->GetRight();
  804. parent = tree->GetParent();
  805. if (!left || !right)
  806. {
  807. prev = tree;
  808. }
  809. else
  810. {
  811. prev = GetSuccessor(tree);
  812. }
  813. if (prev->GetLeft())
  814. {
  815. cur = prev->GetLeft();
  816. }
  817. else
  818. {
  819. cur = prev->GetRight();
  820. }
  821. if (cur)
  822. {
  823. cur->SetParent(prev->GetParent());
  824. }
  825. if (!prev->GetParent())
  826. {
  827. _root = cur;
  828. }
  829. else
  830. {
  831. parent = prev->GetParent();
  832. if (prev == parent->GetLeft())
  833. {
  834. parent->SetLeft(cur);
  835. }
  836. else
  837. {
  838. parent->SetRight(cur);
  839. }
  840. }
  841. if (prev != tree)
  842. {
  843. TreeNodeShallowCopy(prev, tree, true);
  844. if (prev->GetParent())
  845. {
  846. if (prev == (prev->GetParent())->GetLeft())
  847. (prev->GetParent())->SetLeft(tree);
  848. else if (prev == (prev->GetParent())->GetRight())
  849. (prev->GetParent())->SetRight(tree);
  850. }
  851. }
  852. --_num_elements;
  853. if (prev)
  854. {
  855. prev->SetRight(NULL);
  856. prev->SetParent(NULL);
  857. prev->SetLeft(NULL);
  858. delete prev;
  859. }
  860. if (tree->GetColor() == _tree_h_black)
  861. {
  862. RestoreRedBlackAfterRemove(cur);
  863. }
  864. }
  865. void RestoreRedBlackAfterRemove(TreeNode<Key, Data> *tree)
  866. {
  867. TreeNode<Key, Data> *parent, *sibling, *sleft, *sright;
  868. if (!tree || !_root)
  869. {
  870. return;
  871. }
  872. parent = tree->GetParent();
  873. while ((tree != _root) && (parent->GetColor() == _tree_h_black))
  874. {
  875. if (tree == parent->GetLeft())
  876. {
  877. sibling = parent->GetRight();
  878. if (sibling && sibling->GetColor() == _tree_h_red)
  879. {
  880. sibling->SetColor(_tree_h_black);
  881. parent->SetColor(_tree_h_red);
  882. RotateLeft(parent);
  883. sibling = parent->GetRight();
  884. }
  885. if (sibling)
  886. {
  887. sleft = sibling->GetLeft();
  888. sright = sibling->GetRight();
  889. }
  890. else
  891. {
  892. sleft = sright = NULL;
  893. }
  894. if (sright && sright->GetColor() == _tree_h_black &&
  895. sleft && sleft->GetColor() ==_tree_h_black)
  896. {
  897. sibling->SetColor(_tree_h_red);
  898. tree = parent;
  899. }
  900. else
  901. {
  902. if (sright && sright->GetColor() == _tree_h_black)
  903. {
  904. sibling->SetColor(_tree_h_red);
  905. sleft->SetColor(_tree_h_black);
  906. RotateRight(sibling);
  907. sibling = parent->GetRight();
  908. }
  909. sibling->SetColor(parent->GetColor());
  910. parent->SetColor(_tree_h_black);
  911. sright->SetColor(_tree_h_black);
  912. RotateLeft(parent);
  913. tree = _root;
  914. }
  915. }
  916. else
  917. {
  918. sibling = parent->GetLeft();
  919. if (sibling && sibling->GetColor() == _tree_h_red)
  920. {
  921. sibling->SetColor(_tree_h_black);
  922. parent->SetColor(_tree_h_red);
  923. RotateLeft(parent);
  924. sibling = parent->GetLeft();
  925. }
  926. if (sibling)
  927. {
  928. sleft = sibling->GetLeft();
  929. sright = sibling->GetRight();
  930. }
  931. else
  932. {
  933. sleft = sright = NULL;
  934. }
  935. if (sright && sright->GetColor() == _tree_h_black &&
  936. sleft && sleft->GetColor() ==_tree_h_black)
  937. {
  938. sibling->SetColor(_tree_h_red);
  939. tree = parent;
  940. }
  941. else
  942. {
  943. if (sleft && sleft->GetColor() == _tree_h_black)
  944. {
  945. sibling->SetColor(_tree_h_red);
  946. sright->SetColor(_tree_h_black);
  947. RotateLeft(sibling);
  948. sibling = parent->GetLeft();
  949. }
  950. sibling->SetColor(parent->GetColor());
  951. parent->SetColor(_tree_h_black);
  952. sleft->SetColor(_tree_h_black);
  953. RotateRight(parent);
  954. tree = _root;
  955. }
  956. }
  957. parent = tree->GetParent();
  958. }
  959. tree->SetColor(_tree_h_black);
  960. }
  961. void RestoreRedBlackAfterInsert(TreeNode<Key, Data> *tree)
  962. {
  963. TreeNode<Key, Data> *parent, *grandparent, *uncle;
  964. if (!tree || !_root || tree == _root)
  965. {
  966. return;
  967. }
  968. tree->SetColor(_tree_h_red);
  969. parent = tree->GetParent();
  970. while ((tree != _root) && (parent->GetColor() == _tree_h_red))
  971. {
  972. grandparent = parent->GetParent();
  973. if (parent == grandparent->GetLeft())
  974. {
  975. uncle = grandparent->GetRight();
  976. if (uncle && uncle->GetColor() == _tree_h_red)
  977. {
  978. // Case 1 - Change the colors
  979. parent->SetColor(_tree_h_black);
  980. uncle->SetColor(_tree_h_black);
  981. grandparent->SetColor(_tree_h_red);
  982. // Move up the tree
  983. tree = grandparent;
  984. }
  985. else // Uncle is a black node
  986. {
  987. if (tree == parent->GetRight())
  988. {
  989. // Case 2 - Move up and rotate
  990. tree = parent;
  991. RotateLeft(tree);
  992. }
  993. // Case 3 - Make no changes to _root tree
  994. // Change colors for Case 2 / Case 3
  995. parent->SetColor(_tree_h_black);
  996. grandparent->SetColor(_tree_h_red);
  997. RotateRight(grandparent);
  998. }
  999. }
  1000. else // TreeNode 'tree' is in right subtree
  1001. {
  1002. uncle = grandparent->GetLeft();
  1003. if (uncle && uncle->GetColor() == _tree_h_red)
  1004. {
  1005. // Case 1 - Change the colors
  1006. parent->SetColor(_tree_h_black);
  1007. uncle->SetColor(_tree_h_black);
  1008. grandparent->SetColor(_tree_h_red);
  1009. // Move up the tree
  1010. tree = grandparent;
  1011. }
  1012. else // Uncle is a black node
  1013. {
  1014. if (tree == parent->GetLeft())
  1015. {
  1016. // Case 2 - Move up and rotate
  1017. tree = parent;
  1018. RotateRight(tree);
  1019. }
  1020. // Case 3 - Make no changes to _root tree
  1021. // Change colors for Case 2 / Case 3
  1022. parent->SetColor(_tree_h_black);
  1023. grandparent->SetColor(_tree_h_red);
  1024. RotateLeft(grandparent);
  1025. }
  1026. }
  1027. // Have to adjust parent for new tree node
  1028. parent = tree->GetParent();
  1029. }
  1030. // Mongoose 2002.02.17, Color root black ( heh )
  1031. _root->SetColor(_tree_h_black);
  1032. }
  1033. bool _error; //!< Error reporting for operator use
  1034. unsigned int _num_elements; //!< Number of nodes in this tree
  1035. TreeNode<Key, Data> *_root; //!< Root node
  1036. };
  1037. #endif