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 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 <iostream>
31# include <vector>
32# include <algorithm>
33# include <random>
34# include <memory>
35
36# include <aleph.H>
37# include <tpl_binTree.H>
38# include <tpl_tree_node.H>
39# include <generate_tree.H>
40
41# include <gsl/gsl_rng.h>
42
43using namespace std;
44using namespace Aleph;
45
46struct GslRngDeleter
47{
48 void operator()(gsl_rng * r) const { gsl_rng_free(r); }
49};
50
51using GslRngHandle = std::unique_ptr<gsl_rng, GslRngDeleter>;
52
53static void printNode(BinTree<int>::Node* node, int, int)
54{
55 cout << node->get_key() << " ";
56}
57
58
59struct Write
60{
62 {
63 return to_string(p->get_key());
64 }
65};
66
68{
69 cout << "(" << p->get_key();
70 p->for_each_child([](auto c) { print_forest_par(c); });
71 cout << ")";
72}
73
74void print_forest_deway(Tree_Node<int> * p, const string & idx)
75{
76 cout << "(" << idx << ":" << p->get_key() << ")";
77 size_t counter = 1;
78 p->for_each_child([&idx, &counter](auto c) {
79 stringstream s;
80 s << idx << '.' << counter++;
81 print_forest_deway(c, s.str());
82 });
83}
84
85int main(int argc, char *argv[])
86{
87 int n = argc > 1 ? stoi(argv[1]) : 1000;
88
89 unsigned int t = std::time(0);
90
91 if (argc > 2)
92 t = stoi(argv[2]);
93
95 gsl_rng_set(rng.get(), t);
96
97 cout << argv[0] << " " << n << " " << t << endl;
98
99 BinTree<int> tree;
100
101 int ins_count = 0;
102
103 // Validate input to prevent infinite loops
104 if (n > 1000)
105 {
106 cerr << "Error: n must be <= 1000 to avoid infinite loops with current RNG range." << endl;
107 return 1;
108 }
109
110 cout << "Inserting " << n << " random values in tree ...\n";
111
112 for (int i = 0; i < n; i++)
113 {
114 int value;
115 BinTree<int>::Node *node;
116 do
117 {
118 value = gsl_rng_uniform_int(rng.get(), 1000);
119 node = tree.search(value);
120 }
121 while (node not_eq nullptr);
122
123 node = new BinTree<int>::Node (value);
124 tree.insert(node);
125 ins_count++;
126
127 cout << value << " ";
128 }
129 cout << endl << endl;
130
133
134 // Check if forest conversion succeeded before proceeding
135 if (ttree == nullptr)
136 {
137 cout << "Warning: Empty tree, no forest to process." << endl;
138 return 0;
139 }
140
142
143 cout << endl << "Secuencia paréntesis: ";
145 auto rc = ttree->get_right_sibling();
146 while (rc != nullptr)
147 {
149 rc = rc->get_right_sibling();
150 }
151 cout << endl << endl;
152
153 cout << "Secuencia en notación Deway: ";
155 rc = ttree->get_right_sibling();
156 size_t counter = 2;
157 while (rc != nullptr)
158 {
159 stringstream s;
160 s << counter++;
161 print_forest_deway(rc, s.str());
162 rc = rc->get_right_sibling();
163 }
164 cout << endl;
165
167
168 assert(tree.verifyBin());
169 cout << endl << ins_count << " insertions" << endl
170 << "prefijo: " << endl;
172 cout << endl << endl;
173
174 cout << "sufijo: " << endl;
176 cout << endl << endl;
177
178 cout << "infijo: " << endl;
180 cout << endl << endl;
181
182 cout << "Code = " << code(tree.getRoot()) << endl;
183
184 int ipl = internal_path_length(tree.getRoot());
185
186 cout << "IPL = " << ipl << endl
187 << "EPL = " << ipl + 2*n << endl;
188
189 BinTree<int>::Node * t1 = nullptr, * t2 = nullptr;
190
191 BinTree<int>::Node * aux;
192 aux = copyRec(tree.getRoot());
193
194 split_key(aux, 487, t1, t2);
195 cout << "t1: ";
196 preOrderRec(t1, printNode); cout << endl << endl;
197 cout << "t2: ";
198 preOrderRec(t2, printNode); cout << endl << endl;
199
200 int del_count = 0;
201
202 cout << "Removing " << n/4 << " keys" << endl;
203
204 std::vector<int> keys;
206 keys.push_back(node->get_key());
207 });
208 std::shuffle(keys.begin(), keys.end(), std::mt19937(t));
209
210 for (size_t i = 0; i < (size_t)(n/4) && i < keys.size(); i++)
211 {
212 int value = keys[i];
213 BinTree<int>::Node * node = tree.remove(value);
214 if (node != nullptr)
215 {
216 del_count++;
217 cout << node->get_key() << " ";
218 delete node;
219 }
220 }
221
222 cout << endl << del_count << " deletions" << endl
223 << "prefijo: ";
225 cout << endl;
226 cout << endl;
227
228 assert(tree.verifyBin());
229
230 destroyRec(tree.getRoot());
231 destroyRec(t1);
232 destroyRec(t2);
233
234 cout << argv[0] << " " << n << " " << t << endl;
235 }
236
Core header for the Aleph-w library.
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
Generic m-ary trees.
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.
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
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.
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:48
string operator()(Tree_Node< int > *p)
Definition testBinTree.C:61
static long counter
Definition test-splice.C:35
std::unique_ptr< gsl_rng, GslRngDeleter > GslRngHandle
int keys[]
void print_forest_par(Tree_Node< int > *p)
Definition testBinTree.C:67
void print_forest_deway(Tree_Node< int > *p, const string &idx)
Definition testBinTree.C:74
static void printNode(BinTree< int >::Node *node, int, int)
Definition testBinTree.C:53
gsl_rng * r
Generic unbalanced binary search tree.
General tree (n-ary tree) node.