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