28 # ifndef TPL_RB_TREE_H 29 # define TPL_RB_TREE_H 31 # include <ahFunction.H> 32 # include <tpl_arrayStack.H> 33 # include <tpl_binNodeUtils.H> 36 using namespace Aleph;
59 template <
template <
typename>
class NodeType,
typename Key,
class Compare>
64 typedef NodeType<Key> Node;
74 Node * search_and_stack_rb(
const Key & key)
83 else if (cmp(
KEY(p), key))
88 while (p != Node::NullPtr);
90 return rb_stack.
top();
93 Node * search_dup_and_stack_rb(
const Key & key)
100 if (cmp(key,
KEY(p)))
105 while (p != Node::NullPtr);
107 return rb_stack.
top();
110 void fix_red_condition(Node * p)
112 assert(COLOR(p) == RED);
116 Node * pp = rb_stack.
pop();
117 if (COLOR(pp) == BLACK)
126 Node * ppp = rb_stack.
pop();
128 if (COLOR(spp) == RED)
137 Node * pppp = rb_stack.
pop();
170 void find_succ_and_swap(Node * p, Node *& pp)
172 Node *& ref_rb_stack = rb_stack.
top();
176 Node * succ =
RLINK(p);
179 while (
LLINK(succ) != Node::NullPtr)
195 LLINK(p) = Node::NullPtr;
197 if (
RLINK(p) == succ)
205 Node *succr =
RLINK(succ);
212 std::swap(COLOR(succ), COLOR(p));
215 void fix_black_condition(Node * p)
223 Node * pp = rb_stack.
popn(2);
230 if (COLOR(sp) == RED)
232 Node *& ppp = rb_stack.
top();
261 if (COLOR(np) == RED)
263 Node * ppp = rb_stack.
top();
270 COLOR(sp) = COLOR(pp);
277 if (COLOR(snp) == RED)
279 Node * ppp = rb_stack.
top();
281 if (
LLINK(sp) == snp)
292 COLOR(snp) = COLOR(pp);
298 if (COLOR(pp) == RED)
326 : head(&head_node), root(head_node.getR()),
327 rb_stack(Node::MaxHeight), cmp(__cmp)
339 std::swap(root, tree.root);
340 std::swap(cmp, tree.cmp);
351 Node * retVal = Aleph::searchInBinTree <Node, Compare> (root, key, cmp);
352 return retVal == Node::NullPtr ? nullptr : retVal;
363 assert(COLOR(p) == RED);
365 if (root == Node::NullPtr)
368 Node * q = search_and_stack_rb(
KEY(p));
371 else if (cmp(
KEY(q),
KEY(p)))
378 fix_red_condition(p);
399 assert(COLOR(p) == RED);
401 if (root == Node::NullPtr)
404 Node * q = search_and_stack_rb(
KEY(p));
407 else if (cmp(
KEY(q),
KEY(p)))
414 fix_red_condition(p);
425 assert(COLOR(p) == RED);
427 if (root == Node::NullPtr)
430 Node * q = search_dup_and_stack_rb(
KEY(p));
436 fix_red_condition(p);
441 bool verify()
const {
return is_red_black_tree(root) ; }
447 Node*
remove(
const Key & key)
449 if (root == Node::NullPtr)
452 Node * q = search_and_stack_rb(key);
453 if (no_equals<Key, Compare> (
KEY(q), key, cmp))
459 Node * pq = rb_stack.
top(1);
463 if (
LLINK(q) == Node::NullPtr)
473 if (
RLINK(q) == Node::NullPtr)
483 find_succ_and_swap(q, pq);
486 if (COLOR(q) == BLACK)
487 fix_black_condition(p);
522 template <
typename Key,
class Compare = Aleph::less<Key>>
544 template <
typename Key,
class Compare = Aleph::less<Key>>
553 # endif // TPL_RB_TREE_H Definition: tpl_binNodeUtils.H:2445
void swap(Gen_Rb_Tree &tree)
Definition: tpl_rb_tree.H:337
Key key_type
El tipo de clave que contiene el nodo.
Definition: tpl_rb_tree.H:316
Node *& getRoot()
Obtiene un puntero a la raÃz del árbol.
Definition: tpl_rb_tree.H:356
Node * rotate_to_right(Node *p) noexcept
Definition: tpl_binNodeUtils.H:1891
Definition: tpl_rb_tree.H:523
Definition: tpl_rb_tree.H:545
Definition: tpl_rb_tree.H:60
T pop() noexcept
Pop by moving the top of stack.
Definition: tpl_arrayStack.H:430
Node * search_or_insert(Node *p)
Definition: tpl_rb_tree.H:397
T popn(const int &n) noexcept
Definition: tpl_arrayStack.H:442
Node::Key_Type & KEY(Node *p) noexcept
Definition: tpl_binNode.H:318
T & top() const noexcept
Return a modifiable referemce to stack's top.
Definition: tpl_arrayStack.H:451
Gen_Rb_Tree(Compare __cmp=Compare())
Instancia un árbol rojo-negro.
Definition: tpl_rb_tree.H:325
Compare & get_compare()
Definition: tpl_rb_tree.H:322
Definition: tpl_arrayStack.H:302
Node *& LLINK(Node *p) noexcept
Definition: tpl_binNode.H:298
Compare & key_comp()
Retorna una referencia al criterio de comparación.
Definition: tpl_rb_tree.H:319
Node * insert_dup(Node *p)
Definition: tpl_rb_tree.H:423
void empty() noexcept
Empty the stack.
Definition: tpl_arrayStack.H:480
T & push(const T &data) noexcept
Definition: tpl_arrayStack.H:385
Node * rotate_to_left(Node *p) noexcept
Definition: tpl_binNodeUtils.H:1932
Definition: tpl_rb_tree.H:503
virtual ~Gen_Rb_Tree()
Destruye un árbol rojo-negro.
Definition: tpl_rb_tree.H:344
Node * search(const Key &key)
Definition: tpl_rb_tree.H:349
Node * insert(Node *p)
Definition: tpl_rb_tree.H:361
Node *& RLINK(Node *p) noexcept
Definition: tpl_binNode.H:307