Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testLuka.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32# include <ctime>
33# include <cstdlib>
34# include <cassert>
35# include <ah-random.H>
36# include <cstring>
37# include <iostream>
38# include <ah-errors.H>
39# include <tpl_binNode.H>
40# include <tpl_binNodeUtils.H>
41
42using namespace std;
43
45
46
51
52
53size_t get_rand(const size_t & n)
54{
55 return r.uniform_int(n) + 1;
56}
57
58
59 template <class Node>
60Node * random_tree(const size_t & n)
61{
62 if (n == 0)
63 return Node::NullPtr;
64
65 Node * root = new Node; // apartar memoria nodo
66
67 /* sorteo que define aleatoriamente la posición infija de la raíz */
68 const size_t i = get_rand(n);
69
70 LLINK(root) = random_tree<Node>(i - 1); // aleatorio de r - 1 nodos
71 RLINK(root) = random_tree<Node>(n - i); // aleatorio de n - r nodos
72
73 return root;
74}
75
76
77 template <class Node>
78string luka(Node * p)
79{
80 if (p == Node::NullPtr)
81 return string("b");
82
83 return string("a") + luka(LLINK(p)) + luka(RLINK(p));
84}
85
86 template <class Node>
87Node * luka_to_tree(char *& cod)
88{
89 if (*cod == '\0')
90 return Node::NullPtr;
91
92 if (*cod++ == 'b')
93 return Node::NullPtr;
94
95 Node * p = new Node;
96
97 LLINK(p) = luka_to_tree<Node>(cod);
98 RLINK(p) = luka_to_tree<Node>(cod);
99
100 return p;
101}
102
103int main(int argc, char *argv[])
104{
105 const size_t n = argc > 1 ? atol(argv[1]) : 10;
106
107 int t = std::time(NULL);
108
109 if (argc > 2)
110 t = atol(argv[2]);
111
112 cout << "testLuka " << n << " " << t << endl;
113
114 init_random(t);
115
116 BinNode<int> * tree_root = random_tree< BinNode<int> >(n);
117
118 string s = luka(tree_root);
119 cout << "luka(tree_root) = " << s << endl;
120
121 char * cod_orig = strdup(s.c_str());
122 char * cod = cod_orig;
123
124 BinNode<int> * aux = luka_to_tree< BinNode<int> >(cod);
125
126 assert(areSimilar(aux, tree_root));
127
128 destroyRec(aux);
129 destroyRec(tree_root);
130 free(cod_orig);
131}
Exception handling system with formatted messages for Aleph-w.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
WeightedDigraph::Node Node
int main()
Node for binary search tree.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
Definition ah-random.H:277
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Definition ah-random.H:302
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
bool areSimilar(Node *t1, Node *t2) noexcept
Return true if both trees are similar.
STL namespace.
#define RLINK(i, n)
#define LLINK(i, n)
ValueArg< size_t > seed
Definition testHash.C:53
void init_random(int seed)
Definition testLuka.C:47
size_t get_rand(const size_t &n)
Definition testLuka.C:53
Node * random_tree(const size_t &n)
Definition testLuka.C:60
Gsl_Compatible_Rng r
Definition testLuka.C:44
Node * luka_to_tree(char *&cod)
Definition testLuka.C:87
string luka(Node *p)
Definition testLuka.C:78
Utility functions for binary tree operations.
Basic binary tree node definitions.