Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeJoin.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
211# include <iostream>
212# include <fstream>
213# include <tclap/CmdLine.h>
214# include <aleph.H>
215# include <tpl_binNodeUtils.H>
216# include <tpl_binNodeXt.H>
217
218using namespace Aleph;
219using namespace std;
220
222
223// Output streams for the three trees
224ofstream output;
225ofstream output1;
226ofstream output2;
227
228void print_key(Node * p, int, int)
229{
230 output << p->get_key() << " ";
231}
232
233void print_key1(Node * p, int, int)
234{
235 output1 << p->get_key() << " ";
236}
237
238void print_key2(Node * p, int, int)
239{
240 output2 << p->get_key() << " ";
241}
242
243int main(int argc, char* argv[])
244{
245 try
246 {
247 TCLAP::CmdLine cmd(
248 "Demonstrate BST join operation.\n"
249 "Creates two BSTs and joins them into one, generating visualization files.",
250 ' ', "1.0");
251
252 TCLAP::ValueArg<int> nArg("n", "count",
253 "Number of elements per tree (total = 2n)",
254 false, 10, "int");
255 cmd.add(nArg);
256
257 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
258 "Random seed (0 = use time)",
259 false, 0, "unsigned int");
260 cmd.add(seedArg);
261
262 cmd.parse(argc, argv);
263
264 int n = nArg.getValue();
265 unsigned int t = seedArg.getValue();
266
267 if (t == 0)
268 t = time(nullptr);
269
270 srand(t);
271
272 cout << "=== BST Join Operation Demo ===" << endl;
273 cout << "Elements per tree: " << n << ", Seed: " << t << endl << endl;
274
275 // Open output files
276 output.open("join-aux.Tree", ios::out);
277 output1.open("join-1-aux.Tree", ios::out);
278 output2.open("join-2-aux.Tree", ios::out);
279
280 if (!output.is_open() || !output1.is_open() || !output2.is_open())
281 {
282 cerr << "Error: cannot open output files" << endl;
283 return 1;
284 }
285
286 // Build first tree with n elements
287 int value;
288 Node* root1 = Node::NullPtr;
289
290 cout << "Building first tree with " << n << " elements..." << endl;
291 for (int i = 0; i < n; i++)
292 {
293 while (true)
294 {
295 value = rand() % (n * 100) + 1;
296 if (searchInBinTree(root1, value) == nullptr)
297 break;
298 }
299 Node* p = new Node(value);
300 insert_root(root1, p);
301 }
302
303 assert(check_bst(root1));
304 int n1 = preOrderRec(root1, &print_key1);
305 output1 << endl;
306
307 cout << " Nodes: " << n1
308 << ", Height: " << computeHeightRec(root1) << endl;
309
310 // Build second tree with n elements (non-overlapping keys)
311 Node* root2 = Node::NullPtr;
312
313 cout << "Building second tree with " << n << " elements..." << endl;
314 for (int i = 0; i < n; i++)
315 {
316 while (true)
317 {
318 value = rand() % (n * 100) + 1;
319 if (searchInBinTree(root1, value) == nullptr &&
320 searchInBinTree(root2, value) == nullptr)
321 break;
322 }
323 Node* p = new Node(value);
324 insert_in_bst(root2, p);
325 }
326
327 assert(check_bst(root2));
328 int n2 = preOrderRec(root2, &print_key2);
329 output2 << endl;
330
331 cout << " Nodes: " << n2
332 << ", Height: " << computeHeightRec(root2) << endl;
333
334 // Join the two trees
335 cout << endl << "Joining trees..." << endl;
336 Node* dup = nullptr;
337 Node* root = join(root1, root2, dup);
338
339 if (dup != nullptr)
340 {
341 cout << "Warning: duplicates found (unexpected)" << endl;
342 }
343
346 output << endl;
347
348 cout << "Resulting tree:" << endl;
349 cout << " Nodes: " << n_total
350 << ", Height: " << computeHeightRec(root) << endl;
351
352 // Clean up
353 output.close();
354 output1.close();
355 output2.close();
357
358 cout << endl << "Generated files:" << endl;
359 cout << " - join-1-aux.Tree (first tree)" << endl;
360 cout << " - join-2-aux.Tree (second tree)" << endl;
361 cout << " - join-aux.Tree (joined result)" << endl;
362 }
363 catch (TCLAP::ArgException& e)
364 {
365 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
366 return 1;
367 }
368
369 return 0;
370}
int main()
Core header for the Aleph-w library.
WeightedDigraph::Node Node
Node for binary search tree.
Key & get_key() noexcept
static BinNode *const NullPtr
__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.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
Node * insert_in_bst(Node *&r, Node *p, const Compare &cmp=Compare()) noexcept
Insert a node p in a binary search tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Search a key in a binary search tree.
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
size_t computeHeightRec(Node *root) noexcept
Compute recursively the height of root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Utility functions for binary tree operations.
Extended binary node with subtree count.
BinNode< int > Node
Definition writeJoin.C:221
void print_key(Node *p, int, int)
Definition writeJoin.C:228
ofstream output2
Definition writeJoin.C:226
ofstream output
Definition writeJoin.C:224
ofstream output1
Definition writeJoin.C:225
void print_key1(Node *p, int, int)
Definition writeJoin.C:233
void print_key2(Node *p, int, int)
Definition writeJoin.C:238