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 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 <gsl/gsl_rng.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
48{
50
51 if (r == NULL)
53
55}
56
57
58size_t get_rand(const size_t & n)
59{
60 return gsl_rng_uniform_int(r, n) + 1;
61}
62
63
64 template <class Node>
65Node * random_tree(const size_t & n)
66{
67 if (n == 0)
68 return Node::NullPtr;
69
70 Node * root = new Node; // apartar memoria nodo
71
72 /* sorteo que define aleatoriamente la posición infija de la raíz */
73 const size_t i = get_rand(n);
74
75 LLINK(root) = random_tree<Node>(i - 1); // aleatorio de r - 1 nodos
76 RLINK(root) = random_tree<Node>(n - i); // aleatorio de n - r nodos
77
78 return root;
79}
80
81
82 template <class Node>
83string luka(Node * p)
84{
85 if (p == Node::NullPtr)
86 return string("b");
87
88 return string("a") + luka(LLINK(p)) + luka(RLINK(p));
89}
90
91 template <class Node>
93{
94 if (*cod == '\0')
95 return Node::NullPtr;
96
97 if (*cod++ == 'b')
98 return Node::NullPtr;
99
100 Node * p = new Node;
101
104
105 return p;
106}
107
108int main(int argc, char *argv[])
109{
110 const size_t n = argc > 1 ? atol(argv[1]) : 10;
111
112 int t = std::time(NULL);
113
114 if (argc > 2)
115 t = atol(argv[2]);
116
117 cout << "testLuka " << n << " " << t << endl;
118
119 init_random(t);
120
121 BinNode<int> * tree_root = random_tree< BinNode<int> >(n);
122
123 string s = luka(tree_root);
124 cout << "luka(tree_root) = " << s << endl;
125
126 char * cod_orig = strdup(s.c_str());
127 char * cod = cod_orig;
128
130
131 assert(areSimilar(aux, tree_root));
132
133 destroyRec(aux);
134 destroyRec(tree_root);
135 free(cod_orig);
137}
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:426
WeightedDigraph::Node Node
int main()
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:4071
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
void destroyRec(Node *&root) noexcept
Free recursively 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:58
gsl_rng * r
Definition testLuka.C:44
Node * random_tree(const size_t &n)
Definition testLuka.C:65
Node * luka_to_tree(char *&cod)
Definition testLuka.C:92
string luka(Node *p)
Definition testLuka.C:83
Utility functions for binary tree operations.
Basic binary tree node definitions.