5 # include <gsl/gsl_rng.h>
6 # include <ahFunction.H>
7 # include <treapNode.H>
8 # include <tpl_binTreeOps.H>
10 using namespace Aleph;
39 template <
template <
typename>
class NodeType,
typename Key,
class Compare>
44 typedef NodeType<Key>
Node;
54 void init(
unsigned int seed)
56 PRIO(head_ptr) = Min_Priority;
57 r = gsl_rng_alloc (gsl_rng_mt19937);
60 throw std::bad_alloc();
62 gsl_rng_set(r, seed % gsl_rng_max(r));
74 std::swap(tree_root, tree.tree_root);
75 std::swap(cmp, tree.cmp);
87 : head_ptr(&head), tree_root(
RLINK(head_ptr)), r(NULL), cmp(__cmp)
92 Gen_Treap(
unsigned int seed, Compare && __cmp)
93 : head_ptr(&head), tree_root(
RLINK(head_ptr)), r(NULL), cmp(__cmp)
114 Node* ret_val = searchInBinTree <Node, Compare> (tree_root, key, cmp);
116 return ret_val == Node::NullPtr ? NULL : ret_val;
123 if (root == Node::NullPtr)
126 Node * insertion_result = NULL;
127 if (cmp(
KEY(p),
KEY(root)))
129 insertion_result = insert(
LLINK(root), p);
130 if (insertion_result == Node::NullPtr)
131 return Node::NullPtr;
133 LLINK(root) = insertion_result;
134 if (PRIO(insertion_result) < PRIO(root))
139 else if (cmp(
KEY(root),
KEY(p)))
141 insertion_result = insert(
RLINK(root), p);
142 if (insertion_result == Node::NullPtr)
143 return Node::NullPtr;
145 RLINK(root) = insertion_result;
146 if (PRIO(insertion_result) < PRIO(root))
152 return Node::NullPtr;
160 if (root == Node::NullPtr)
163 if (cmp(
KEY(p),
KEY(root)))
165 Node * ret = search_or_insert(
LLINK(root), p);
168 if (PRIO(
LLINK(root)) < PRIO(root))
173 else if (cmp(
KEY(root),
KEY(p)))
175 Node * ret = search_or_insert(
RLINK(root), p);
178 if (PRIO(
RLINK(root)) < PRIO(root))
189 if (root == Node::NullPtr)
192 if (cmp(
KEY(p),
KEY(root)))
195 if (PRIO(result) < PRIO(root))
203 if (PRIO(result) < PRIO(root))
221 I(p != Node::NullPtr);
223 PRIO(p) = gsl_rng_get(r);
224 Node * result = insert(tree_root, p);
225 if (result == Node::NullPtr)
249 I(p != Node::NullPtr);
251 PRIO(p) = gsl_rng_get(r);
253 return search_or_insert(tree_root, p);
266 I(p != Node::NullPtr);
268 PRIO(p) = gsl_rng_get(r);
269 tree_root = insert_dup(tree_root, p);
274 bool verify() {
return is_treap(tree_root); }
288 Node * p = tree_root;
291 while (p != Node::NullPtr)
292 if (cmp(key,
KEY(p)))
297 else if (cmp(
KEY(p), key))
305 if (p == Node::NullPtr)
309 while (not (
LLINK(p) == Node::NullPtr and
RLINK(p) == Node::NullPtr))
351 template <
typename Key,
class Compare = Aleph::less<Key>>
356 Treap(
unsigned int seed, Compare && cmp = Compare())
362 Treap(Compare && cmp = Compare())
368 Treap(
unsigned int seed, Compare & cmp)
403 template <
typename Key,
class Compare = Aleph::less<Key>>
408 Treap_Vtl(
unsigned int seed, Compare && cmp = Compare())
420 Treap_Vtl(
unsigned int seed, Compare & cmp)
435 # endif // TPL_TREAP_H
Node * rotate_to_left(Node *p)
Definition: tpl_binNodeUtils.H:2422
#define LLINK(p)
Definition: tpl_binNode.H:196
#define RLINK(p)
Definition: tpl_binNode.H:201
Node *& getRoot()
Retorna la raíz del treap.
Definition: tpl_treap.H:109
Node * search_or_insert(Node *p)
Definition: tpl_treap.H:247
Definition: tpl_treap.H:404
Definition: tpl_treap.H:352
NodeType< Key > Node
Tipo de nodo del treap.
Definition: tpl_treap.H:44
Node * rotate_to_right(Node *p)
Definition: tpl_binNodeUtils.H:2375
Compare & get_compare()
Definition: tpl_treap.H:83
~Gen_Treap()
Destructor; libera memoria de generador de números aleatorios.
Definition: tpl_treap.H:99
Node * insert_dup(Node *p)
Definition: tpl_treap.H:264
gsl_rng * gsl_rng_object()
Obtiene un puntero al objeto gsl_rng generador de números aleatorios.
Definition: tpl_treap.H:106
Gen_Treap(unsigned int seed, Compare &__cmp)
Constructor; inicializa generador de números aleatorios.
Definition: tpl_treap.H:86
Node * search(const Key &key)
Busca la clave key en el treap.
Definition: tpl_treap.H:112
Definition: tpl_treap.H:40
Compare & key_comp()
Retorna una referencia al criterio de comparación.
Definition: tpl_treap.H:80
Node * insert(Node *p)
Definition: tpl_treap.H:219
#define KEY(p)
Definition: tpl_binNode.H:206
void swap(Gen_Treap &tree)
Definition: tpl_treap.H:72