Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testBinTree.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 <iostream>
36# include <vector>
37# include <algorithm>
38# include <random>
39# include <memory>
40
41# include <aleph.H>
42# include <tpl_binTree.H>
43# include <tpl_tree_node.H>
44# include <generate_tree.H>
45
46# include <gsl/gsl_rng.h>
47
48using namespace std;
49using namespace Aleph;
50
51struct GslRngDeleter
52{
53 void operator()(gsl_rng * r) const { gsl_rng_free(r); }
54};
55
56using GslRngHandle = std::unique_ptr<gsl_rng, GslRngDeleter>;
57
58static void printNode(BinTree<int>::Node* node, int, int)
59{
60 cout << node->get_key() << " ";
61}
62
63
64struct Write
65{
67 {
68 return to_string(p->get_key());
69 }
70};
71
73{
74 cout << "(" << p->get_key();
75 p->for_each_child([](auto c) { print_forest_par(c); });
76 cout << ")";
77}
78
79void print_forest_deway(Tree_Node<int> * p, const string & idx)
80{
81 cout << "(" << idx << ":" << p->get_key() << ")";
82 size_t counter = 1;
83 p->for_each_child([&idx, &counter](auto c) {
84 stringstream s;
85 s << idx << '.' << counter++;
86 print_forest_deway(c, s.str());
87 });
88}
89
90int main(int argc, char *argv[])
91{
92 int n = argc > 1 ? stoi(argv[1]) : 1000;
93
94 unsigned int t = std::time(0);
95
96 if (argc > 2)
97 t = stoi(argv[2]);
98
100 gsl_rng_set(rng.get(), t);
101
102 cout << argv[0] << " " << n << " " << t << endl;
103
104 BinTree<int> tree;
105
106 int ins_count = 0;
107
108 // Validate input to prevent infinite loops
109 if (n > 1000)
110 {
111 cerr << "Error: n must be <= 1000 to avoid infinite loops with current RNG range." << endl;
112 return 1;
113 }
114
115 cout << "Inserting " << n << " random values in tree ...\n";
116
117 for (int i = 0; i < n; i++)
118 {
119 int value;
120 BinTree<int>::Node *node;
121 do
122 {
123 value = gsl_rng_uniform_int(rng.get(), 1000);
124 node = tree.search(value);
125 }
126 while (node not_eq nullptr);
127
128 node = new BinTree<int>::Node (value);
129 tree.insert(node);
130 ins_count++;
131
132 cout << value << " ";
133 }
134 cout << endl << endl;
135
138
139 // Check if forest conversion succeeded before proceeding
140 if (ttree == nullptr)
141 {
142 cout << "Warning: Empty tree, no forest to process." << endl;
143 return 0;
144 }
145
147
148 cout << endl << "Secuencia paréntesis: ";
150 auto rc = ttree->get_right_sibling();
151 while (rc != nullptr)
152 {
154 rc = rc->get_right_sibling();
155 }
156 cout << endl << endl;
157
158 cout << "Secuencia en notación Deway: ";
160 rc = ttree->get_right_sibling();
161 size_t counter = 2;
162 while (rc != nullptr)
163 {
164 stringstream s;
165 s << counter++;
166 print_forest_deway(rc, s.str());
167 rc = rc->get_right_sibling();
168 }
169 cout << endl;
170
172
173 assert(tree.verifyBin());
174 cout << endl << ins_count << " insertions" << endl
175 << "prefijo: " << endl;
177 cout << endl << endl;
178
179 cout << "sufijo: " << endl;
181 cout << endl << endl;
182
183 cout << "infijo: " << endl;
185 cout << endl << endl;
186
187 cout << "Code = " << code(tree.getRoot()) << endl;
188
189 int ipl = internal_path_length(tree.getRoot());
190
191 cout << "IPL = " << ipl << endl
192 << "EPL = " << ipl + 2*n << endl;
193
194 BinTree<int>::Node * t1 = nullptr, * t2 = nullptr;
195
196 BinTree<int>::Node * aux;
197 aux = copyRec(tree.getRoot());
198
199 split_key(aux, 487, t1, t2);
200 cout << "t1: ";
201 preOrderRec(t1, printNode); cout << endl << endl;
202 cout << "t2: ";
203 preOrderRec(t2, printNode); cout << endl << endl;
204
205 int del_count = 0;
206
207 cout << "Removing " << n/4 << " keys" << endl;
208
209 std::vector<int> keys;
211 keys.push_back(node->get_key());
212 });
213 std::shuffle(keys.begin(), keys.end(), std::mt19937(t));
214
215 for (size_t i = 0; i < (size_t)(n/4) && i < keys.size(); i++)
216 {
217 int value = keys[i];
218 BinTree<int>::Node * node = tree.remove(value);
219 if (node != nullptr)
220 {
221 del_count++;
222 cout << node->get_key() << " ";
223 delete node;
224 }
225 }
226
227 cout << endl << del_count << " deletions" << endl
228 << "prefijo: ";
230 cout << endl;
231 cout << endl;
232
233 assert(tree.verifyBin());
234
235 destroyRec(tree.getRoot());
236 destroyRec(t1);
237 destroyRec(t2);
238
239 cout << argv[0] << " " << n << " " << t << endl;
240 }
241
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node *& getRoot() noexcept
Return the root of tree.
Node * insert(Node *p) noexcept
Insert a node in the tree.
Node * search(const Key &key) const noexcept
Search a key.
Node * remove(const Key &key) noexcept
Remove a key from the tree.
bool verifyBin() const
Forward declaration used by CRTP helpers before the full node definition.
void for_each_child(Operation &op) const
Visits each child of this and executes the operation on the child node.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
static mt19937 rng
Tree visualization and output generation.
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
int postOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in postorder a binary tree.
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
Node * copyRec(Node *root)
Copy recursively a tree.
size_t internal_path_length(Node *p) noexcept
Compute the internal path length.
void destroy_forest(Node *root)
Destroys (frees memory) the forest whose first tree is root.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
void split_key(Node *&root, const Key &key, Node *&l, Node *&r, const Compare &cmp=Compare()) noexcept
Split a binary search tree according to a key.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
STL namespace.
Binary search tree with nodes without virtual destructors,.
void operator()(gsl_rng *r) const
Definition testBinTree.C:53
string operator()(Tree_Node< int > *p)
Definition testBinTree.C:66
static long counter
Definition test-splice.C:40
std::unique_ptr< gsl_rng, GslRngDeleter > GslRngHandle
int keys[]
void print_forest_par(Tree_Node< int > *p)
Definition testBinTree.C:72
void print_forest_deway(Tree_Node< int > *p, const string &idx)
Definition testBinTree.C:79
static void printNode(BinTree< int >::Node *node, int, int)
Definition testBinTree.C:58
gsl_rng * r
Generic unbalanced binary search tree.
General tree (n-ary tree) node.