5 # include <ahFunction.H>
6 # include <tpl_arrayStack.H>
7 # include <tpl_binNodeUtils.H>
10 using namespace Aleph;
33 template <
template <
typename>
class NodeType,
typename Key,
class Compare>
38 typedef NodeType<Key> Node;
48 Node * search_and_stack_rb(
const Key & key)
57 else if (cmp(
KEY(p), key))
62 while (p != Node::NullPtr);
64 return rb_stack.
top();
67 Node * search_dup_and_stack_rb(
const Key & key)
79 while (p != Node::NullPtr);
81 return rb_stack.
top();
84 void fix_red_condition(Node * p)
90 Node * pp = rb_stack.
pop();
91 if (COLOR(pp) == BLACK)
100 Node * ppp = rb_stack.
pop();
102 if (COLOR(spp) == RED)
111 Node * pppp = rb_stack.
pop();
144 void find_succ_and_swap(Node * p, Node *& pp)
146 Node *& ref_rb_stack = rb_stack.
top();
150 Node * succ =
RLINK(p);
153 while (
LLINK(succ) != Node::NullPtr)
169 LLINK(p) = Node::NullPtr;
171 if (
RLINK(p) == succ)
179 Node *succr =
RLINK(succ);
186 std::swap(COLOR(succ), COLOR(p));
189 void fix_black_condition(Node * p)
197 Node * pp = rb_stack.
popn(2);
204 if (COLOR(sp) == RED)
206 Node *& ppp = rb_stack.
top();
235 if (COLOR(np) == RED)
237 Node * ppp = rb_stack.
top();
244 COLOR(sp) = COLOR(pp);
251 if (COLOR(snp) == RED)
253 Node * ppp = rb_stack.
top();
255 if (
LLINK(sp) == snp)
266 COLOR(snp) = COLOR(pp);
272 if (COLOR(pp) == RED)
301 : rb_stack(Node::MaxHeight), head(&head_node),
302 root(head_node.getR()), cmp(__cmp)
308 : rb_stack(Node::MaxHeight), head(&head_node),
309 root(head_node.getR()), cmp(__cmp)
321 std::swap(root, tree.root);
322 std::swap(cmp, tree.cmp);
333 Node * retVal = Aleph::searchInBinTree <Node, Compare> (root, key, cmp);
334 return retVal == Node::NullPtr ? NULL : retVal;
347 if (root == Node::NullPtr)
350 Node * q = search_and_stack_rb(
KEY(p));
353 else if (cmp(
KEY(q),
KEY(p)))
360 fix_red_condition(p);
383 if (root == Node::NullPtr)
386 Node * q = search_and_stack_rb(
KEY(p));
389 else if (cmp(
KEY(q),
KEY(p)))
396 fix_red_condition(p);
409 if (root == Node::NullPtr)
412 Node * q = search_dup_and_stack_rb(
KEY(p));
418 fix_red_condition(p);
423 bool verify() {
return is_red_black_tree(root) ; }
429 Node*
remove(
const Key & key)
431 if (root == Node::NullPtr)
434 Node * q = search_and_stack_rb(key);
435 if (no_equals<Key, Compare> (
KEY(q), key, cmp))
441 Node * pq = rb_stack.
top(1);
445 if (
LLINK(q) == Node::NullPtr)
455 if (
RLINK(q) == Node::NullPtr)
465 find_succ_and_swap(q, pq);
468 if (COLOR(q) == BLACK)
469 fix_black_condition(p);
491 template <
typename Key,
class Compare = Aleph::less<Key>>
496 Rb_Tree(Compare && cmp = Compare())
524 template <
typename Key,
class Compare = Aleph::less<Key>>
544 # endif // TPL_RB_TREE_H
Node * rotate_to_left(Node *p)
Definition: tpl_binNodeUtils.H:2422
void swap(Gen_Rb_Tree &tree)
Definition: tpl_rb_tree.H:319
Key key_type
El tipo de clave que contiene el nodo.
Definition: tpl_rb_tree.H:291
#define LLINK(p)
Definition: tpl_binNode.H:196
Node *& getRoot()
Obtiene un puntero a la raíz del árbol.
Definition: tpl_rb_tree.H:338
Definition: tpl_rb_tree.H:492
Definition: tpl_rb_tree.H:525
#define RLINK(p)
Definition: tpl_binNode.H:201
Gen_Rb_Tree(Compare &&__cmp)
Instancia un árbol rojo-negro.
Definition: tpl_rb_tree.H:300
T pop()
Definition: tpl_arrayStack.H:353
Definition: tpl_rb_tree.H:34
T & push(const T &data)
Definition: tpl_arrayStack.H:312
Node * search_or_insert(Node *p)
Definition: tpl_rb_tree.H:379
T popn(const int &n)
Definition: tpl_arrayStack.H:369
Node * rotate_to_right(Node *p)
Definition: tpl_binNodeUtils.H:2375
Compare & get_compare()
Definition: tpl_rb_tree.H:297
Compare & key_comp()
Retorna una referencia al criterio de comparación.
Definition: tpl_rb_tree.H:294
Node * insert_dup(Node *p)
Definition: tpl_rb_tree.H:405
T & top()
retorna una referencia al elemento situado en el tope de la pila.
Definition: tpl_arrayStack.H:378
virtual ~Gen_Rb_Tree()
Destruye un árbol rojo-negro.
Definition: tpl_rb_tree.H:326
#define KEY(p)
Definition: tpl_binNode.H:206
Node * search(const Key &key)
Definition: tpl_rb_tree.H:331
Node * insert(Node *p)
Definition: tpl_rb_tree.H:343
void empty()
Vacía todos los elementos de la pila.
Definition: tpl_arrayStack.H:416