2 # ifndef TPL_RAND_TREE_H
3 # define TPL_RAND_TREE_H
6 # include <gsl/gsl_rng.h>
8 # include <ahFunction.H>
9 # include <tpl_randNode.H>
10 # include <tpl_binTreeOps.H>
12 using namespace Aleph;
35 template <
template <
typename>
class NodeType,
typename Key,
class Compare>
41 typedef NodeType<Key>
Node;
74 const long & n =
COUNT(root);
75 const size_t rn = gsl_rng_uniform_int(r, n + 1);
80 if (cmp(
KEY(p),
KEY(root)))
82 result = random_insert(
LLINK(root), p);
83 if (result != Node::NullPtr)
91 else if (cmp(
KEY(root),
KEY(p)))
93 result = random_insert(
RLINK(root), p);
94 if (result != Node::NullPtr)
103 return Node::NullPtr;
129 const long & n =
COUNT(root);
130 const size_t rn = gsl_rng_uniform_int(r, n + 1);
134 if (cmp(
KEY(p),
KEY(root)))
135 LLINK(root) = random_insert_dup(
LLINK(root), p);
137 RLINK(root) = random_insert_dup(
RLINK(root), p);
146 void init(
unsigned int seed)
148 r = gsl_rng_alloc (gsl_rng_mt19937);
151 throw std::bad_alloc();
153 gsl_rng_set(r, seed % gsl_rng_max(r));
171 : tree_root(
Node::NullPtr), r(NULL), cmp(__cmp)
177 : tree_root(
Node::NullPtr), r(NULL), cmp(__cmp)
189 std::swap(tree_root, tree.tree_root);
190 std::swap(r, tree.r);
191 std::swap(cmp, tree.cmp);
214 I(p != Node::NullPtr);
215 I((
LLINK(p) == Node::NullPtr) and (
RLINK(p) == Node::NullPtr));
218 Node * result = random_insert(tree_root, p);
219 if (result == Node::NullPtr)
222 return tree_root = result;
236 I(p != Node::NullPtr);
237 I((
LLINK(p) == Node::NullPtr) and (
RLINK(p) == Node::NullPtr));
240 return tree_root = random_insert_dup(tree_root, p);
270 if (tl == Node::NullPtr)
273 if (tr == Node::NullPtr)
276 const size_t & m =
COUNT(tl);
277 const size_t & n =
COUNT(tr);
278 const size_t rn = 1 + gsl_rng_uniform_int(r, m + n);
320 if (root == Node::NullPtr)
321 return Node::NullPtr;
324 if (cmp(key,
KEY(root)))
327 if (ret_val != Node::NullPtr)
332 else if (cmp(
KEY(root), key))
335 if (ret_val != Node::NullPtr)
363 return ret_val != Node::NullPtr ? ret_val : NULL;
368 Node * retVal = searchInBinTree<Node, Compare>(tree_root, key, cmp);
370 return retVal == Node::NullPtr ? NULL : retVal;
389 I(p != Node::NullPtr);
396 tree_root = random_insert(tree_root, p);
403 return check_rank_tree(tree_root);
442 std::pair<int,Node*> ret_val;
446 IG(
KEY(ret_val.second) == key, ret_val.first >= 0);
483 std::pair<int,Node*> r(-2, NULL);
511 t.tree_root->reset();
546 template <
typename Key,
class Compare = Aleph::less<Key>>
553 Rand_Tree(
unsigned int seed, Compare && cmp = Compare())
554 :
Gen_Rand_Tree<RandNode, Key, Compare>(seed, std::move(cmp))
560 :
Gen_Rand_Tree<RandNode, Key, Compare>(time(NULL), std::move(cmp))
571 Rand_Tree(
unsigned int seed, Compare & cmp)
598 template <
typename Key,
class Compare = Aleph::less<Key>>
606 :
Gen_Rand_Tree<RandNodeVtl, Key, Compare>(seed, std::move(cmp))
612 :
Gen_Rand_Tree<RandNodeVtl, Key, Compare>(time(NULL), std::move(cmp))
624 :
Gen_Rand_Tree<RandNodeVtl, Key, Compare>(time(NULL), std::move(cmp))
633 # endif // TPL_RAND_TREE_H
Node * search_or_insert(Node *p)
Definition: tpl_rand_tree.H:387
#define LLINK(p)
Definition: tpl_binNode.H:196
Node * random_join_exclusive(Node *tl, Node *tr)
Definition: tpl_rand_tree.H:268
#define RLINK(p)
Definition: tpl_binNode.H:201
Node * search(const Key &key)
Busca la clave key en el árbol binario de búsqueda aleatorizado.
Definition: tpl_rand_tree.H:366
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p)
Definition: tpl_binNodeXt.H:135
virtual ~Gen_Rand_Tree()
Destructor (libera generador de números aleatorios.
Definition: tpl_rand_tree.H:196
Node * insert_root(Node *&root, Node *p)
Definition: tpl_binNodeUtils.H:2236
Gen_Rand_Tree(unsigned int seed, Compare &&__cmp)
Definition: tpl_rand_tree.H:170
#define COUNT(p)
Definition: tpl_binNodeXt.H:34
void split_key_dup_rec_xt(Node *root, const typename Node::key_type &key, Node *&l, Node *&r)
Definition: tpl_binNodeXt.H:427
Definition: tpl_binTreeOps.H:632
Rand_Tree(unsigned int seed, Compare &&cmp=Compare())
Definition: tpl_rand_tree.H:553
NodeType< Key > Node
Tipo de nodo binario.
Definition: tpl_rand_tree.H:41
Node * insert_dup(Node *p)
Definition: tpl_rand_tree.H:234
Definition: tpl_rand_tree.H:599
Node * select(Node *r, const size_t &pos)
Definition: tpl_binNodeXt.H:89
Node * insert(Node *p)
Definition: tpl_rand_tree.H:211
Node * insert_dup_root(Node *&root, Node *p)
Definition: tpl_binNodeUtils.H:2274
Compare & get_compare()
Definition: tpl_rand_tree.H:162
gsl_rng * gsl_rng_object()
Obtiene un puntero al objeto gsl_rng generador de números aleatorios.
Definition: tpl_rand_tree.H:166
void swap(Gen_Rand_Tree &tree)
Definition: tpl_rand_tree.H:187
Definition: tpl_rand_tree.H:547
std::pair< int, Node * > position(const Key &key)
Definition: tpl_rand_tree.H:440
Node *& getRoot()
Definition: tpl_rand_tree.H:408
void split_pos_rec(Node *r, const size_t i, Node *&ts, Node *&tg)
Definition: tpl_binNodeXt.H:541
std::pair< int, Node * > find_position(const Key &key)
Definition: tpl_rand_tree.H:481
Node * select(const size_t &i) const
Definition: tpl_rand_tree.H:420
Node * random_remove(Node *&root, const Key &key)
Definition: tpl_rand_tree.H:318
bool split_key_rec_xt(Node *root, const typename Node::key_type &key, Node *&l, Node *&r)
Definition: tpl_binNodeXt.H:373
Rand_Tree_Vtl(unsigned int seed, Compare &&cmp=Compare())
Definition: tpl_rand_tree.H:605
#define KEY(p)
Definition: tpl_binNode.H:206
size_t size() const
Retorna la cantidad de nodos que contiene el árbol.
Definition: tpl_rand_tree.H:426
Compare & key_comp()
Retorna una referencia al criterio de comparación.
Definition: tpl_rand_tree.H:159
Definition: tpl_rand_tree.H:36