Aleph-w 3.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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <ctime>
28# include <cstdlib>
29# include <cassert>
30# include <gsl/gsl_rng.h>
31# include <cstring>
32# include <iostream>
33# include <ah-errors.H>
34# include <tpl_binNode.H>
35# include <tpl_binNodeUtils.H>
36
37using namespace std;
38
40
41
43{
45
46 if (r == NULL)
48
50}
51
52
53size_t get_rand(const size_t & n)
54{
55 return gsl_rng_uniform_int(r, 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>
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
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
125
126 assert(areSimilar(aux, tree_root));
127
128 destroyRec(aux);
129 destroyRec(tree_root);
130 free(cod_orig);
132}
Exception handling system with formatted messages for Aleph-w.
#define ah_bad_alloc()
Throws std::bad_alloc unconditionally (no message stream)
Definition ah-errors.H:421
WeightedDigraph::Node Node
Node for binary search tree.
__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:4060
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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:48
void init_random(int seed)
Definition testLuka.C:42
size_t get_rand(const size_t &n)
Definition testLuka.C:53
gsl_rng * r
Definition testLuka.C:39
Node * random_tree(const size_t &n)
Definition testLuka.C:60
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.