Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
generate_forest.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
160# include <cstdlib>
161# include <ctime>
162# include <iostream>
163# include <fstream>
164# include <tclap/CmdLine.h>
165# include <tpl_tree_node.H>
166# include <generate_tree.H>
167# include <tpl_binTree.H>
168
169using namespace Aleph;
170using namespace std;
171
172static void printNode(BinTreeVtl<int>::Node* node, int, int)
173{
174 cout << node->get_key() << " ";
175}
176
181{
183 {
184 return to_string(p->get_key());
185 }
186};
187
188int main(int argc, char *argv[])
189{
190 try
191 {
192 TCLAP::CmdLine cmd("Generate forest from random binary tree", ' ', "1.0");
193
194 TCLAP::ValueArg<int> nArg("n", "nodes",
195 "Number of nodes in the tree",
196 false, 100, "int");
197 cmd.add(nArg);
198
199 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
200 "Random seed (0 = use time)",
201 false, 0, "unsigned int");
202 cmd.add(seedArg);
203
204 TCLAP::ValueArg<string> outputArg("o", "output",
205 "Output file name",
206 false, "arborescencia.Tree", "string");
207 cmd.add(outputArg);
208
209 cmd.parse(argc, argv);
210
211 int n = nArg.getValue();
212 unsigned int t = seedArg.getValue();
213 string outputFile = outputArg.getValue();
214
215 if (t == 0)
216 t = time(nullptr);
217
218 srand(t);
219
220 cout << "Forest Generation Example" << endl;
221 cout << "=========================" << endl;
222 cout << "Parameters: n=" << n << ", seed=" << t << endl;
223 cout << "Output file: " << outputFile << endl << endl;
224
225 BinTreeVtl<int> tree;
227 int value;
228
229 cout << "Inserting " << n << " random values into BST..." << endl;
230
231 int ins_count = 0;
232
233 for (int i = 0; i < n; i++)
234 {
235 do
236 {
237 value = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
238 node = tree.search(value);
239 }
240 while (node != nullptr);
241
242 node = new BinTreeVtl<int>::Node(value);
243 tree.insert(node);
244 ins_count++;
245 }
246
247 cout << ins_count << " insertions completed" << endl;
248
249 assert(tree.verifyBin());
250 cout << "BST verification: PASSED" << endl << endl;
251
252 cout << "Preorder traversal: ";
254 cout << endl << endl;
255
256 // Convert to forest
259
260 // Write to file
261 ofstream output(outputFile, ios::trunc);
263 output.close();
264
265 cout << "Forest written to " << outputFile << endl;
266
267 // Cleanup
269 destroyRec(tree.getRoot());
270
271 cout << endl << "Done." << endl;
272 }
273 catch (TCLAP::ArgException &e)
274 {
275 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
276 return 1;
277 }
278
279 return 0;
280}
int main()
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.
bool verifyBin() const
Generic m-ary trees.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
static void printNode(BinTreeVtl< int >::Node *node, int, int)
Tree visualization and output generation.
__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
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Binary search tree with nodes with virtual destructors,.
Functor to convert tree node to string for output.
string operator()(Tree_Node< int > *p)
Generic unbalanced binary search tree.
General tree (n-ary tree) node.
ofstream output
Definition writeHeap.C:213