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
169# include <cassert>
170using namespace Aleph;
171# include <cassert>
172using namespace std;
173
174static void printNode(BinTreeVtl<int>::Node* node, int, int)
175{
176 cout << node->get_key() << " ";
177}
178
183{
185 {
186 return to_string(p->get_key());
187 }
188};
189
190int main(int argc, char *argv[])
191{
192 try
193 {
194 TCLAP::CmdLine cmd("Generate forest from random binary tree", ' ', "1.0");
195
196 TCLAP::ValueArg<int> nArg("n", "nodes",
197 "Number of nodes in the tree",
198 false, 100, "int");
199 cmd.add(nArg);
200
201 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
202 "Random seed (0 = use time)",
203 false, 0, "unsigned int");
204 cmd.add(seedArg);
205
206 TCLAP::ValueArg<string> outputArg("o", "output",
207 "Output file name",
208 false, "arborescencia.Tree", "string");
209 cmd.add(outputArg);
210
211 cmd.parse(argc, argv);
212
213 int n = nArg.getValue();
214 unsigned int t = seedArg.getValue();
215 string outputFile = outputArg.getValue();
216
217 if (t == 0)
218 t = time(nullptr);
219
220 srand(t);
221
222 cout << "Forest Generation Example" << endl;
223 cout << "=========================" << endl;
224 cout << "Parameters: n=" << n << ", seed=" << t << endl;
225 cout << "Output file: " << outputFile << endl << endl;
226
227 BinTreeVtl<int> tree;
229 int value;
230
231 cout << "Inserting " << n << " random values into BST..." << endl;
232
233 int ins_count = 0;
234
235 for (int i = 0; i < n; i++)
236 {
237 do
238 {
239 value = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
240 node = tree.search(value);
241 }
242 while (node != nullptr);
243
244 node = new BinTreeVtl<int>::Node(value);
245 tree.insert(node);
246 ins_count++;
247 }
248
249 cout << ins_count << " insertions completed" << endl;
250
251 assert(tree.verifyBin());
252 cout << "BST verification: PASSED" << endl << endl;
253
254 cout << "Preorder traversal: ";
256 cout << endl << endl;
257
258 // Convert to forest
261
262 // Write to file
263 ofstream output(outputFile, ios::trunc);
265 output.close();
266
267 cout << "Forest written to " << outputFile << endl;
268
269 // Cleanup
271 destroyRec(tree.getRoot());
272
273 cout << endl << "Done." << endl;
274 }
275 catch (TCLAP::ArgException &e)
276 {
277 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
278 return 1;
279 }
280
281 return 0;
282}
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
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 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 with virtual destructors,.
Functor to convert tree node to string for output.
string operator()(Tree_Node< int > *p)
CmdLine cmd
Definition testHash.C:43
Generic unbalanced binary search tree.
General tree (n-ary tree) node.
ofstream output
Definition writeHeap.C:215