12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226 |
- /*!
- * \file include/Tree.h
- * \brief Template Red-Black Tree
- *
- * Red-Black trees are a type of binary search trees
- * with the properities:
- *
- * * Every node is red or black.
- * * The root node must be black.
- * * Every leaf node is black. (null pointers)
- * * If a node is red, then both its children are black.
- * * Every simple path from a node to a descendant leaf contains the same number of black nodes.
- * * Any path from the root to a leaf must not have adjacent red nodes.
- *
- * Define `USE_IOSTREAM` to get additional print methods
- *
- * \author Mongoose
- */
-
- #ifndef _TREE_H_
- #define _TREE_H_
-
- #include <stdlib.h>
- #include <stdio.h>
-
- #ifdef USE_IOSTREAM
- #include <iostream.h>
- #endif
-
- #ifdef DEBUG_MEMORY
- #include <memory_test.h>
- #endif
-
- /*!
- * \brief Color a Tree node can have
- */
- typedef enum {
- _tree_h_black, //!< Black node
- _tree_h_red //!< Red node
- } _tree_h_color_t;
-
- /*!
- * \brief Template class for a single Tree node
- * \tparam Key key type
- * \tparam Data data type
- */
- template <class Key, class Data> class TreeNode {
- public:
-
- /*!
- * \brief Construct a TreeNode
- * \param key Key for this node
- * \param data Data for this node
- */
- TreeNode(Key key, Data data) {
- SetColor(_tree_h_red);
- SetData(data);
- SetKey(key);
- SetParent(NULL);
- SetLeft(NULL);
- SetRight(NULL);
- }
-
- /*!
- * \brief Deconstruct a TreeNode. Also deletes childs.
- */
- ~TreeNode() {
- TreeNode<Key, Data> *left;
- TreeNode<Key, Data> *right;
- left = GetLeft();
- right = GetRight();
- SetParent(NULL);
- SetLeft(NULL);
- SetRight(NULL);
- if (left) {
- left->SetParent(NULL);
- delete left;
- }
- if (right) {
- right->SetParent(NULL);
- delete right;
- }
- }
-
- /*!
- * \brief Set the color
- * \param color new color
- */
- void SetColor(_tree_h_color_t color) {
- _color = color;
- }
-
- /*!
- * \brief Get the color
- * \returns current color
- */
- _tree_h_color_t GetColor() {
- return _color;
- }
-
- /*!
- * \brief Get the child
- * \returns the left child
- */
- TreeNode<Key, Data> *GetChild() {
- return GetLeft();
- }
-
- /*!
- * \brief Set the child
- * \param tree new left child
- */
- void SetChild(TreeNode<Key, Data> *tree) {
- Left(tree);
- }
-
- /*!
- * \brief Get the child
- * \returns the left child
- */
- TreeNode<Key, Data> *GetLeft() {
- return _left;
- }
-
- /*!
- * \brief Set the child
- * \param tree new left child
- */
- void SetLeft(TreeNode<Key, Data> *tree) {
- if (tree == this)
- return;
- _left = tree;
- if (tree)
- tree->SetParent(this);
- }
-
- /*!
- * \brief Get the sibling
- * \returns the right child
- */
- TreeNode<Key, Data> *GetSibling() {
- return GetRight();
- }
-
- /*!
- * \brief Set the sibling
- * \param tree new right child
- */
- void SetSibling(TreeNode<Key, Data> *tree) {
- SetRight(tree);
- }
-
- /*!
- * \brief Get the sibling
- * \returns the right child
- */
- TreeNode<Key, Data> *GetRight() {
- return _right;
- }
-
- /*!
- * \brief Set the sibling
- * \param tree new right child
- */
- void SetRight(TreeNode<Key, Data> *tree) {
- if (tree == this)
- return;
- _right = tree;
- if (tree)
- tree->SetParent(this);
- }
-
- /*!
- * \brief Get the data
- * \returns current data
- */
- Data GetData() {
- return _data;
- }
-
- /*!
- * \brief Set the data
- * \param data new data
- */
- void SetData(Data data) {
- _data = data;
- }
-
- /*!
- * \brief Get the key
- * \returns current key
- */
- Key GetKey() {
- return _key;
- }
-
- /*!
- * \brief Set the key
- * \param key new key
- */
- void SetKey(Key key) {
- _key = key;
- }
-
- /*!
- * \brief Get the parent
- * \returns current parent
- */
- TreeNode<Key, Data> *GetParent() {
- return _parent;
- }
-
- /*!
- * \brief Set the parent
- * \param parent new parent
- */
- void SetParent(TreeNode<Key, Data> *parent) {
- _parent = parent;
- }
-
- #ifdef USE_IOSTREAM
- /*!
- * \brief Print this node
- */
- void PrintNode() {
- cout << "(" << _key << ", " << _data << ", "
- << ((GetColor() == _tree_h_red) ? "Red" : "Black")
- << ")";
- }
-
- /*!
- * \brief Print this node and its children in the correct order
- */
- void PrintInorder() {
- if (_left) {
- _left->PrintInorder();
- cout << endl;
- }
- PrintNode();
- if (_right) {
- cout << endl;
- _right->PrintInorder();
- }
- }
- #endif
-
- /*!
- * \brief Print this node with custom methods to print key and data
- * \param print_func_k key printing function
- * \param print_func_d data printing function
- */
- void PrintNodeSpecial(void (*print_func_k)(Key), void (*print_func_d)(Data)) {
- printf("(");
- if (print_func_k)
- (*print_func_k)(_key);
- printf(", ");
- if (print_func_d)
- (*print_func_d)(_data);
- printf(", %s)", ((GetColor() == _tree_h_red) ? "Red" : "Black"));
- }
-
- /*!
- * \brief Print this node and its children in the correct order, with custom methods to print key and data
- * \param print_func_k key printing function
- * \param print_func_d data printing function
- */
- void PrintInorderSpecial(void (*print_func_k)(Key), void (*print_func_d)(Data)) {
- if (_left) {
- _left->PrintInorderSpecial(print_func_k, print_func_d);
- printf(",\n");
- }
- PrintNodeSpecial(print_func_k, print_func_d);
- if (_right) {
- printf(",\n");
- _right->PrintInorderSpecial(print_func_k, print_func_d);
- }
- }
-
- /*!
- * \brief Search this node and its children for specific data
- * \param data data to search for
- * \param error will be true if nothing was found
- * \returns the TreeNode containing the data, or NULL
- */
- TreeNode<Key, Data> *SearchByData(Data data, bool *error) {
- TreeNode<Key, Data> *tree = NULL;
- *error = true;
- if (_data == data) {
- *error = false;
- return this;
- }
- if (_left)
- tree = _left->SearchByData(data, error);
- if (_right && !tree)
- tree = _right->SearchByData(data, error);
- return tree;
- }
-
- /*!
- * \brief Search this node and its children for a specific key
- * \param key key to search for
- * \param error will be true if nothing was found
- * \returns the TreeNode containing the key, or NULL
- */
- TreeNode<Key, Data> *SearchByKey(Key key, bool *error) {
- *error = false;
- if (_key == key) {
- return this;
- } else if (_left && key < _key) {
- return _left->SearchByKey(key, error);
- } else if (_right) {
- return _right->SearchByKey(key, error);
- } else {
- *error = true;
- return 0; //NULL;
- }
- }
-
- /*!
- * \brief Insert a TreeNode after this one.
- * If the key of the TreeNode to be inserted is smaller than the key
- * in this TreeNode, it will be added as left child, else as right child.
- * \param tree TreeNode to insert
- */
- void Insert(TreeNode<Key, Data> *tree) {
- if (!tree || tree == this) {
- return;
- }
- if (tree->GetKey() < _key) {
- if (!_left) {
- SetLeft(tree);
- } else {
- _left->Insert(tree);
- }
- } else {
- if (!_right) {
- SetRight(tree);
- } else {
- _right->Insert(tree);
- }
- }
- }
-
- private:
- _tree_h_color_t _color; //!< Color of tree node
- Key _key; //!< Unique identifer?
- Data _data; //!< Data for this tree
- TreeNode<Key, Data> *_left; //!< Left or child node
- TreeNode<Key, Data> *_right; //!< Right or sibling node
- TreeNode<Key, Data> *_parent; //!< Parent of the tree node
- };
-
- /*!
- * \brief Template class for a Red-Black Tree
- * \tparam Key key datatype
- * \tparam Data data datatype
- */
- template <class Key, class Data> class Tree {
- public:
-
- /*!
- * \brief Construct an object of Tree
- */
- Tree() {
- _error = false;
- _num_elements = 0;
- _root = 0;
- }
-
- /*!
- * \brief Deconstruct an object of Tree
- */
- ~Tree() {
- Clear();
- }
-
- /*!
- * \brief Get the number of elements
- * \returns number of elements in this tree
- */
- unsigned int NumElements() {
- return _num_elements;
- }
-
- /*!
- * \brief Search for data with a key
- * \param key key to search for
- * \param error will be true if nothing is found
- * \returns Data matching Key or NULL
- * \sa TreeNode::SearchByKey()
- */
- Data SearchByKey(Key key, bool *error) {
- TreeNode<Key, Data> *seeking;
- *error = true;
-
- // Mongoose 2002.02.16, Nothing to search
- if (!_root)
- return 0;
-
- seeking = _root->SearchByKey(key, error);
-
- if (seeking)
- return seeking->GetData();
-
- return 0;
- }
-
- /*!
- * \brief Search for a key with data
- * \param data data to search for
- * \param error will be true if nothing is found
- * \returns Key matching Data or NULL
- * \sa TreeNode::SearchByData()
- */
- Key SearchByData(Data data, bool *error) {
- TreeNode<Key, Data> *seeking;
- *error = true;
-
- // Mongoose 2002.02.16, Nothing to search
- if (!_root)
- return 0;
-
- seeking = _root->SearchByData(data, error);
-
- if (seeking)
- return seeking->GetKey();
-
- return 0;
- }
-
- /*!
- * \brief Insert a key-data pair into the tree.
- * \param key key to insert
- * \param data corresponding to key to insert
- * \sa TreeNode::Insert()
- * \sa Tree::RestoreRedBlackAfterInsert()
- */
- void Insert(Key key, Data data) {
- TreeNode<Key, Data> *tree = new TreeNode<Key, Data>(key, data);
- ++_num_elements;
-
- if (_root) {
- _root->Insert(tree);
- RestoreRedBlackAfterInsert(tree);
- } else {
- _root = tree;
- _root->SetColor(_tree_h_black);
- }
- }
-
- /*!
- * \brief Search for data and remove, if found
- * \param data data to remove
- * \returns true if nothing was deleted
- * \sa TreeNode::SearchByData()
- * \sa Tree::Remove()
- */
- bool RemoveByData(Data data) {
- bool error;
- if (_root)
- Remove(_root->SearchByData(data, &error));
- return error;
- }
-
- /*!
- * \brief Search for a key and remove, if found
- * \param key key to remove
- * \returns true if nothing was deleted
- * \sa TreeNode::SearchByKey()
- * \sa Tree::Remove()
- */
- bool RemoveByKey(Key key) {
- bool error;
- if (_root) {
- #ifdef OBSOLETE
- // Mongoose 2002.02.18, To remove duplicates
- erorr = false;
- while (!error) {
- #endif
- Remove(_root->SearchByKey(key, &error));
- #ifdef OBSOLETE
- }
- #endif
- }
- return error;
- }
-
- /*!
- * \brief Clear the list, deleting all TreeNodes
- */
- void Erase() {
- Clear();
- }
-
- /*!
- * \brief Clear the list, deleting all TreeNodes
- */
- void Clear() {
- if (_root)
- delete _root;
-
- _num_elements = 0;
- _error = false;
- _root = 0;
- }
-
- /*!
- * \brief Search for data with a key
- * \param key key to search for
- * \returns data corresponding to key, or 0
- * \sa TreeNode::SearchByKey()
- */
- Data operator [] (Key key) {
- _error = false;
-
- if (_root)
- return SearchByKey(key, &_error);
-
- _error = true;
- return 0;
- }
-
- #ifdef USE_IOSTREAM
- /*!
- * \brief Print a/this Tree?
- * \param tree TreeNode from which to start printing?
- * \param height ?
- * \param seek ?
- * \param rightmost ?
- * \fixme Fix documentation
- * \sa Tree::PrintAsTree()
- */
- void PrintTree(TreeNode<Key, Data> *tree, unsigned int height,
- unsigned int seek, bool rightmost) {
- TreeNode<Key, Data> *left, *right, *parent;
-
- if (!tree)
- return;
-
- parent = tree->GetParent();
-
- if (height == seek) {
- if (!parent) {
- cout << endl << "[height " << height << "] " << endl;
-
- if (tree->GetColor() == _tree_h_red)
- cout << "*";
- } else {
- if ((parent->GetColor() == _tree_h_red) && (tree->GetColor() == _tree_h_red))
- cout << "*";
- }
-
- cout << "(" << tree->GetKey() << ", "
- << ((tree->GetColor() == _tree_h_red) ? "red" : "blk")
- << ")";
-
- if (rightmost) {
- cout << endl << "[height " << (height+1) << "] " << endl;
- PrintTree(_root, 0, ++seek, true);
- } else {
- cout << " ";
- }
-
- return;
- } else if (seek < height) {
- return;
- }
-
- left = tree->GetLeft();
- right = tree->GetRight();
- ++height;
-
- if (left) {
- PrintTree(left, height, seek, false);
- } else {
- cout << "(-, blk) ";
- }
-
- if (right) {
- PrintTree(right, height, seek, rightmost);
- } else {
- cout << "(-, blk) ";
- }
-
- if (parent) {
- if (parent->GetRight() != tree) {
- cout << " | ";
- }
- }
- }
-
- /*!
- * \brief Print this tree as tree
- * \sa Tree::PrintTree()
- */
- void PrintAsTree() {
- PrintTree(_root, 0, 0, true);
- cout << endl << "Nodes marked with * are in error" << endl;
- }
-
- /*!
- * \brief Print this tree
- * \sa TreeNode::PrintNode()
- * \sa TreeNode::PrintInorder()
- */
- void Print() {
- cout << "Tree: " << _num_elements <<" elements {" << endl;
- if (_root) {
- cout << "Root: ";
- _root->PrintNode();
- cout << endl;
- _root->PrintInorder();
- }
- cout << endl << "}" << endl;
- }
- #endif
-
- /*!
- * \brief Print this tree with it's keys and data
- * \param print_func_k key printing function
- * \param print_func_d data printing function
- * \sa TreeNode::PrintNodeSpecial()
- * \sa TreeNode::PrintInorderSpecial()
- */
- void PrintSpecial(void (*print_func_k)(Key), void (*print_func_d)(Data)) {
- printf("Tree: %i elements {\n", _num_elements);
- if (_root && print_func_k && print_func_d) {
- printf("Root: ");
- _root->PrintNodeSpecial(print_func_k, print_func_d);
- printf("\n");
- _root->PrintInorderSpecial(print_func_k, print_func_d);
- }
- printf("\n}\n");
- }
-
- /*!
- * \brief Get the key of the root node
- * \returns key of root node or 0
- */
- Key Root() {
- if (_root) {
- return _root->GetKey();
- }
- return 0;
- }
-
- /*!
- * \brief Get the error flag
- * \returns error flag
- */
- bool Error() {
- return _error;
- }
-
- bool IsValidRedBlackTree() {
- return IsValidRedBlackTreeCheck(_root, true);
- }
-
- private:
-
- TreeNode<Key, Data> *GetSuccessor(TreeNode<Key, Data> *tree)
- {
- TreeNode<Key, Data> *successor;
-
-
- successor = tree->GetRight();
-
- if (successor)
- {
- while (successor->GetLeft())
- {
- successor = successor->GetLeft();
- }
-
- return successor;
- }
- else
- {
- successor = tree->GetParent();
-
- while (tree == successor->GetRight())
- {
- tree = successor;
- successor = successor->GetParent();
- }
-
- if (successor == _root)
- {
- return NULL;
- }
-
- return successor;
- }
- }
-
-
- TreeNode<Key, Data> *GetPredecessor(TreeNode<Key, Data> *tree)
- {
- TreeNode<Key, Data> *predecessor;
-
-
- predecessor = tree->GetLeft();
-
- if (predecessor)
- {
- while (predecessor->GetRight())
- {
- predecessor = predecessor->GetRight();
- }
-
- return predecessor;
- }
- else
- {
- predecessor = tree->GetParent();
-
- while (tree == predecessor->GetLeft())
- {
- if (predecessor == _root)
- {
- return NULL;
- }
-
- tree = predecessor;
- predecessor = predecessor->GetParent();
- }
-
- return predecessor;
- }
- }
-
-
- bool IsValidRedBlackTreeCheck(TreeNode<Key, Data> *current, bool valid)
- {
- TreeNode<Key, Data> *right, *left;
- _tree_h_color_t color_red;
-
- if (!current)
- {
- return valid;
- }
-
- // Mongoose 2002.02.19, Check for a red root
- if (!current->GetParent() && current->GetColor() == _tree_h_red)
- {
- return false;
- }
-
- color_red = (current->GetColor() == _tree_h_red);
- left = current->GetLeft();
- right = current->GetRight();
-
- // Mongoose 2002.02.19, Check for adj red nodes
- if (left)
- {
- if (color_red && left->GetColor() == _tree_h_red)
- {
- return false;
- }
-
- if (!IsValidRedBlackTreeCheck(left, valid))
- return false;
- }
-
- if (right)
- {
- if (color_red && right->GetColor() == _tree_h_red)
- {
- return false;
- }
-
- if (!IsValidRedBlackTreeCheck(right, valid))
- return false;
- }
-
- return true;
- }
-
-
- void RotateLeft(TreeNode<Key, Data> *tree)
- {
- TreeNode<Key, Data> *right, *right_leftchild, *parent, *uncle;
-
-
- if (!tree || !_root)
- {
- return;
- }
-
- // Get tree's right node
- right = tree->GetRight();
-
- // Get right node's left child
- right_leftchild = NULL;
-
- if (right)
- {
- right_leftchild = right->GetLeft();
- }
-
- // Set tree's right node to right's left child
- tree->SetRight(right_leftchild);
-
- // Child now has a new parent
- if (right_leftchild)
- {
- right_leftchild->SetParent(tree);
- }
-
- // Right also has a new parent
- if (right)
- {
- right->SetParent(tree->GetParent());
- }
-
- // Get parent
- parent = tree->GetParent();
-
- if (parent) // Not root
- {
- uncle = parent->GetLeft();
-
- // Mix up at hosptial, switch parent's children!
- if (tree == uncle)
- {
- parent->SetLeft(right);
- }
- else
- {
- parent->SetRight(right);
- }
- }
- else // TreeNode 'tree' was root, so now right is root
- {
- _root = right;
- }
-
- if (right)
- {
- // TreeNode 'tree' is now right's left child
- right->SetLeft(tree);
-
- if (tree)
- {
- tree->SetParent(right);
- }
- }
- }
-
-
- void RotateRight(TreeNode<Key, Data> *tree)
- {
- TreeNode<Key, Data> *left, *left_rightchild, *parent, *uncle;
-
-
- if (!tree || !_root)
- {
- return;
- }
-
- left = tree->GetLeft();
-
- left_rightchild = NULL;
-
- if (left)
- {
- left_rightchild = left->GetRight();
- }
-
- tree->SetLeft(left_rightchild);
-
- if (left_rightchild)
- {
- left_rightchild->SetParent(tree);
- }
-
- if (left)
- {
- left->SetParent(tree->GetParent());
- }
-
- parent = tree->GetParent();
-
- if (parent) //if node is not the root
- {
- uncle = parent->GetRight();
-
- if (tree == uncle)
- {
- parent->SetRight(left);
- }
- else
- {
- parent->SetLeft(left);
- }
- }
- else
- {
- _root = left;
- }
-
- left->SetRight(tree);
-
- if (tree)
- {
- tree->SetParent(left);
- }
- }
-
-
- void TreeNodeShallowCopy(TreeNode<Key, Data> *src,
- TreeNode<Key, Data> *dest, bool no_links)
- {
- if (!src || !dest)
- {
- return;
- }
-
- dest->SetKey(src->GetKey());
- dest->SetData(src->GetData());
- dest->SetColor(src->GetColor());
-
- if (!no_links)
- {
- dest->SetRight(src->GetRight());
- dest->SetLeft(src->GetLeft());
- dest->SetParent(src->GetParent());
- }
- }
-
-
- void Remove(TreeNode<Key, Data> *tree)
- {
- TreeNode<Key, Data> *left, *right, *parent, *prev, *cur;
-
-
- // Mongoose 2002.02.16, Nothing to remove
- if (!tree || !_root)
- {
- return;
- }
-
- left = tree->GetLeft();
- right = tree->GetRight();
- parent = tree->GetParent();
-
-
- if (!left || !right)
- {
- prev = tree;
- }
- else
- {
- prev = GetSuccessor(tree);
- }
-
- if (prev->GetLeft())
- {
- cur = prev->GetLeft();
- }
- else
- {
- cur = prev->GetRight();
- }
-
- if (cur)
- {
- cur->SetParent(prev->GetParent());
- }
-
- if (!prev->GetParent())
- {
- _root = cur;
- }
- else
- {
- parent = prev->GetParent();
-
- if (prev == parent->GetLeft())
- {
- parent->SetLeft(cur);
- }
- else
- {
- parent->SetRight(cur);
- }
- }
-
- if (prev != tree)
- {
- TreeNodeShallowCopy(prev, tree, true);
-
- if (prev->GetParent())
- {
- if (prev == (prev->GetParent())->GetLeft())
- (prev->GetParent())->SetLeft(tree);
- else if (prev == (prev->GetParent())->GetRight())
- (prev->GetParent())->SetRight(tree);
- }
- }
-
- --_num_elements;
-
- if (prev)
- {
- prev->SetRight(NULL);
- prev->SetParent(NULL);
- prev->SetLeft(NULL);
-
- delete prev;
- }
-
- if (tree->GetColor() == _tree_h_black)
- {
- RestoreRedBlackAfterRemove(cur);
- }
- }
-
-
- void RestoreRedBlackAfterRemove(TreeNode<Key, Data> *tree)
- {
- TreeNode<Key, Data> *parent, *sibling, *sleft, *sright;
-
-
- if (!tree || !_root)
- {
- return;
- }
-
- parent = tree->GetParent();
-
- while ((tree != _root) && (parent->GetColor() == _tree_h_black))
- {
- if (tree == parent->GetLeft())
- {
- sibling = parent->GetRight();
-
- if (sibling && sibling->GetColor() == _tree_h_red)
- {
- sibling->SetColor(_tree_h_black);
- parent->SetColor(_tree_h_red);
- RotateLeft(parent);
- sibling = parent->GetRight();
- }
-
- if (sibling)
- {
- sleft = sibling->GetLeft();
- sright = sibling->GetRight();
- }
- else
- {
- sleft = sright = NULL;
- }
-
- if (sright && sright->GetColor() == _tree_h_black &&
- sleft && sleft->GetColor() ==_tree_h_black)
- {
- sibling->SetColor(_tree_h_red);
- tree = parent;
- }
- else
- {
- if (sright && sright->GetColor() == _tree_h_black)
- {
- sibling->SetColor(_tree_h_red);
- sleft->SetColor(_tree_h_black);
- RotateRight(sibling);
- sibling = parent->GetRight();
- }
-
- sibling->SetColor(parent->GetColor());
- parent->SetColor(_tree_h_black);
- sright->SetColor(_tree_h_black);
- RotateLeft(parent);
- tree = _root;
- }
- }
- else
- {
- sibling = parent->GetLeft();
-
- if (sibling && sibling->GetColor() == _tree_h_red)
- {
- sibling->SetColor(_tree_h_black);
- parent->SetColor(_tree_h_red);
- RotateLeft(parent);
- sibling = parent->GetLeft();
- }
-
- if (sibling)
- {
- sleft = sibling->GetLeft();
- sright = sibling->GetRight();
- }
- else
- {
- sleft = sright = NULL;
- }
-
- if (sright && sright->GetColor() == _tree_h_black &&
- sleft && sleft->GetColor() ==_tree_h_black)
- {
- sibling->SetColor(_tree_h_red);
- tree = parent;
- }
- else
- {
- if (sleft && sleft->GetColor() == _tree_h_black)
- {
- sibling->SetColor(_tree_h_red);
- sright->SetColor(_tree_h_black);
- RotateLeft(sibling);
- sibling = parent->GetLeft();
- }
-
- sibling->SetColor(parent->GetColor());
- parent->SetColor(_tree_h_black);
- sleft->SetColor(_tree_h_black);
- RotateRight(parent);
- tree = _root;
- }
- }
-
- parent = tree->GetParent();
- }
-
- tree->SetColor(_tree_h_black);
- }
-
-
- void RestoreRedBlackAfterInsert(TreeNode<Key, Data> *tree)
- {
- TreeNode<Key, Data> *parent, *grandparent, *uncle;
-
-
- if (!tree || !_root || tree == _root)
- {
- return;
- }
-
- tree->SetColor(_tree_h_red);
-
- parent = tree->GetParent();
-
- while ((tree != _root) && (parent->GetColor() == _tree_h_red))
- {
- grandparent = parent->GetParent();
-
- if (parent == grandparent->GetLeft())
- {
- uncle = grandparent->GetRight();
-
- if (uncle && uncle->GetColor() == _tree_h_red)
- {
- // Case 1 - Change the colors
- parent->SetColor(_tree_h_black);
- uncle->SetColor(_tree_h_black);
- grandparent->SetColor(_tree_h_red);
-
- // Move up the tree
- tree = grandparent;
- }
- else // Uncle is a black node
- {
- if (tree == parent->GetRight())
- {
- // Case 2 - Move up and rotate
- tree = parent;
- RotateLeft(tree);
- }
-
- // Case 3 - Make no changes to _root tree
-
- // Change colors for Case 2 / Case 3
- parent->SetColor(_tree_h_black);
- grandparent->SetColor(_tree_h_red);
- RotateRight(grandparent);
- }
- }
- else // TreeNode 'tree' is in right subtree
- {
- uncle = grandparent->GetLeft();
-
- if (uncle && uncle->GetColor() == _tree_h_red)
- {
- // Case 1 - Change the colors
- parent->SetColor(_tree_h_black);
- uncle->SetColor(_tree_h_black);
- grandparent->SetColor(_tree_h_red);
-
- // Move up the tree
- tree = grandparent;
- }
- else // Uncle is a black node
- {
- if (tree == parent->GetLeft())
- {
- // Case 2 - Move up and rotate
- tree = parent;
- RotateRight(tree);
- }
-
- // Case 3 - Make no changes to _root tree
-
- // Change colors for Case 2 / Case 3
- parent->SetColor(_tree_h_black);
- grandparent->SetColor(_tree_h_red);
- RotateLeft(grandparent);
- }
- }
-
- // Have to adjust parent for new tree node
- parent = tree->GetParent();
- }
-
- // Mongoose 2002.02.17, Color root black ( heh )
- _root->SetColor(_tree_h_black);
- }
-
- bool _error; //!< Error reporting for operator use
- unsigned int _num_elements; //!< Number of nodes in this tree
- TreeNode<Key, Data> *_root; //!< Root node
- };
-
- #endif
|