Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
write_tree.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
224# include <iostream>
225# include <fstream>
226# include <cstdlib>
227# include <ctime>
228# include <vector>
229# include <set>
230# include <tclap/CmdLine.h>
231# include <aleph.H>
232# include <tpl_avl.H>
233# include <tpl_rb_tree.H>
234# include <tpl_splay_tree.H>
235# include <tpl_treap.H>
236# include <tpl_rand_tree.H>
237# include <tpl_binTree.H>
238# include <tpl_binNodeUtils.H>
239
240using namespace std;
241using namespace Aleph;
242
243// =============================================================================
244// Helper to generate unique random values
245// =============================================================================
246
247vector<int> generate_random_values(size_t n, unsigned int seed)
248{
249 srand(seed);
250 vector<int> values;
251 values.reserve(n);
252
253 set<int> used; // Standard STL set for uniqueness
254
255 while (values.size() < n)
256 {
257 int val = 1 + rand() % (n * 10);
258 if (used.find(val) == used.end())
259 {
260 used.insert(val);
261 values.push_back(val);
262 }
263 }
264
265 return values;
266}
267
268// =============================================================================
269// Output stream pointer for print functions
270// =============================================================================
271
272static ofstream* current_output = nullptr;
273
274// =============================================================================
275// AVL Tree Writer
276// =============================================================================
277
278static void avl_print_key(Avl_Tree<int>::Node* p, int, int)
279{
280 *current_output << p->get_key() << " ";
281}
282
283void write_avl(const vector<int>& values, const string& prefix)
284{
286
287 ofstream output(prefix + "avl.Tree", ios::out);
288 if (!output.is_open())
289 {
290 cerr << "Error: cannot open " << prefix << "avl.Tree" << endl;
291 return;
292 }
293
294 Avl_Tree<int> tree;
295
296 for (int val : values)
297 {
298 if (tree.search(val) == nullptr)
299 {
300 Node* node = new Node(val);
301 (void) tree.insert(node); // Ignore nodiscard
302 }
303 }
304
306 int count = preOrderRec(tree.getRoot(), avl_print_key);
307 output << endl;
308
309 cout << " Height: " << computeHeightRec(tree.getRoot())
310 << ", Nodes: " << count << endl;
311 cout << " Written: " << prefix << "avl.Tree" << endl;
312
313 destroyRec(tree.getRoot());
314}
315
316// =============================================================================
317// Red-Black Tree Writer (with color info)
318// =============================================================================
319
320static void rb_print_key(Rb_Tree<int>::Node* p, int, int)
321{
322 *current_output << p->get_key() << " ";
323}
324
325static int rb_pos = 0;
326static void rb_write_colors(Rb_Tree<int>::Node* p, int, int)
327{
328 if (COLOR(p) == RED)
329 *current_output << rb_pos << " ";
330 rb_pos++;
331}
332
333void write_rb(const vector<int>& values, const string& prefix)
334{
335 using Node = Rb_Tree<int>::Node;
336
337 ofstream output(prefix + "rb.Tree", ios::out);
338 if (!output.is_open())
339 {
340 cerr << "Error: cannot open " << prefix << "rb.Tree" << endl;
341 return;
342 }
343
344 Rb_Tree<int> tree;
345
346 for (int val : values)
347 {
348 if (tree.search(val) == nullptr)
349 {
350 Node* node = new Node(val);
351 tree.insert(node);
352 }
353 }
354
356
357 // Write keys in preorder
358 int count = preOrderRec(tree.getRoot(), rb_print_key);
359
360 // Write red node positions for visualization
361 output << endl << "START-SHADOW ";
362 rb_pos = 0;
364 output << endl;
365
366 cout << " Height: " << computeHeightRec(tree.getRoot())
367 << ", Nodes: " << count << endl;
368 cout << " Written: " << prefix << "rb.Tree (with color info)" << endl;
369
370 destroyRec(tree.getRoot());
371}
372
373// =============================================================================
374// Splay Tree Writer
375// =============================================================================
376
377static void splay_print_key(Splay_Tree<int>::Node* p, int, int)
378{
379 *current_output << p->get_key() << " ";
380}
381
382void write_splay(const vector<int>& values, const string& prefix)
383{
385
386 ofstream output(prefix + "splay.Tree", ios::out);
387 if (!output.is_open())
388 {
389 cerr << "Error: cannot open " << prefix << "splay.Tree" << endl;
390 return;
391 }
392
393 Splay_Tree<int> tree;
394
395 for (int val : values)
396 {
397 if (tree.search(val) == nullptr)
398 {
399 Node* node = new Node(val);
400 tree.insert(node);
401 }
402 }
403
406 output << endl;
407
408 cout << " Height: " << computeHeightRec(tree.getRoot())
409 << ", Nodes: " << count << endl;
410 cout << " Written: " << prefix << "splay.Tree" << endl;
411
412 destroyRec(tree.getRoot());
413}
414
415// =============================================================================
416// Treap Writer
417// =============================================================================
418
419static void treap_print_key(Treap<int>::Node* p, int, int)
420{
421 *current_output << p->get_key() << " ";
422}
423
424void write_treap(const vector<int>& values, const string& prefix)
425{
426 using Node = Treap<int>::Node;
427
428 ofstream output(prefix + "treap.Tree", ios::out);
429 if (!output.is_open())
430 {
431 cerr << "Error: cannot open " << prefix << "treap.Tree" << endl;
432 return;
433 }
434
435 Treap<int> tree;
436
437 for (int val : values)
438 {
439 if (tree.search(val) == nullptr)
440 {
441 Node* node = new Node(val);
442 tree.insert(node);
443 }
444 }
445
448 output << endl;
449
450 cout << " Height: " << computeHeightRec(tree.getRoot())
451 << ", Nodes: " << count << endl;
452 cout << " Written: " << prefix << "treap.Tree" << endl;
453
454 destroyRec(tree.getRoot());
455}
456
457// =============================================================================
458// Rand Tree Writer
459// =============================================================================
460
461static void rand_print_key(Rand_Tree<int>::Node* p, int, int)
462{
463 *current_output << p->get_key() << " ";
464}
465
466void write_rand(const vector<int>& values, const string& prefix)
467{
469
470 ofstream output(prefix + "rand.Tree", ios::out);
471 if (!output.is_open())
472 {
473 cerr << "Error: cannot open " << prefix << "rand.Tree" << endl;
474 return;
475 }
476
477 Rand_Tree<int> tree;
478
479 for (int val : values)
480 {
481 if (tree.search(val) == nullptr)
482 {
483 Node* node = new Node(val);
484 tree.insert(node);
485 }
486 }
487
490 output << endl;
491
492 cout << " Height: " << computeHeightRec(tree.getRoot())
493 << ", Nodes: " << tree.size() << endl;
494 cout << " Written: " << prefix << "rand.Tree" << endl;
495
496 destroyRec(tree.getRoot());
497}
498
499// =============================================================================
500// Binary Tree Writer (unbalanced)
501// =============================================================================
502
503static void bin_print_key(BinTree<int>::Node* p, int, int)
504{
505 *current_output << p->get_key() << " ";
506}
507
508void write_bin(const vector<int>& values, const string& prefix)
509{
510 using Node = BinTree<int>::Node;
511
512 ofstream output(prefix + "bin.Tree", ios::out);
513 if (!output.is_open())
514 {
515 cerr << "Error: cannot open " << prefix << "bin.Tree" << endl;
516 return;
517 }
518
519 BinTree<int> tree;
520
521 for (int val : values)
522 {
523 if (tree.search(val) == nullptr)
524 {
525 Node* node = new Node(val);
526 tree.insert(node);
527 }
528 }
529
531 int count = preOrderRec(tree.getRoot(), bin_print_key);
532 output << endl;
533
534 cout << " Height: " << computeHeightRec(tree.getRoot())
535 << ", Nodes: " << count << endl;
536 cout << " Written: " << prefix << "bin.Tree" << endl;
537
538 destroyRec(tree.getRoot());
539}
540
541// =============================================================================
542// Main program
543// =============================================================================
544
545int main(int argc, char* argv[])
546{
547 try
548 {
549 TCLAP::CmdLine cmd(
550 "Generate tree structure files for visualization.\n"
551 "Creates .Tree files with preorder traversal for btreepic.",
552 ' ', "1.0");
553
554 TCLAP::ValueArg<size_t> nArg("n", "count",
555 "Number of elements",
556 false, 30, "size_t");
557 cmd.add(nArg);
558
559 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
560 "Random seed (0 = use time)",
561 false, 0, "unsigned int");
562 cmd.add(seedArg);
563
564 vector<string> allowedTypes = {"all", "avl", "rb", "splay", "treap", "rand", "bin"};
565 TCLAP::ValuesConstraint<string> typeConstraint(allowedTypes);
566 TCLAP::ValueArg<string> typeArg("t", "type",
567 "Tree type to generate",
568 false, "all", &typeConstraint);
569 cmd.add(typeArg);
570
571 TCLAP::ValueArg<string> prefixArg("o", "output",
572 "Output file prefix",
573 false, "", "prefix");
574 cmd.add(prefixArg);
575
576 cmd.parse(argc, argv);
577
578 size_t n = nArg.getValue();
579 unsigned int seed = seedArg.getValue();
580 string type = typeArg.getValue();
581 string prefix = prefixArg.getValue();
582
583 if (seed == 0)
584 seed = time(nullptr);
585
586 cout << "=== Tree Structure Generator ===" << endl;
587 cout << "Elements: " << n << ", Seed: " << seed << endl;
588 cout << "Type: " << type << endl << endl;
589
590 // Generate random values
591 vector<int> values = generate_random_values(n, seed);
592
593 cout << "Generated " << values.size() << " unique values" << endl << endl;
594
595 // Generate requested trees
596 if (type == "all" || type == "avl")
597 {
598 cout << "AVL Tree:" << endl;
599 write_avl(values, prefix);
600 }
601
602 if (type == "all" || type == "rb")
603 {
604 cout << "Red-Black Tree:" << endl;
605 write_rb(values, prefix);
606 }
607
608 if (type == "all" || type == "splay")
609 {
610 cout << "Splay Tree:" << endl;
611 write_splay(values, prefix);
612 }
613
614 if (type == "all" || type == "treap")
615 {
616 cout << "Treap:" << endl;
617 write_treap(values, prefix);
618 }
619
620 if (type == "all" || type == "rand")
621 {
622 cout << "Rand Tree:" << endl;
623 write_rand(values, prefix);
624 }
625
626 if (type == "all" || type == "bin")
627 {
628 cout << "Binary Tree (unbalanced):" << endl;
629 write_bin(values, prefix);
630 }
631
632 cout << endl << "Done." << endl;
633 }
634 catch (TCLAP::ArgException& e)
635 {
636 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
637 return 1;
638 }
639
640 return 0;
641}
int main()
Core header for the Aleph-w library.
WeightedDigraph::Node Node
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
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 * search(const Key &key) const noexcept
Search a node containing key; if found, then a pointer to the node containing it is returned; otherwi...
Definition tpl_avl.H:513
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Definition tpl_avl.H:506
Node * insert(Node *p) noexcept
Insert the node pointed by p in the tree.
Definition tpl_avl.H:529
Node * insert(Node *p) noexcept
Insert a node in the tree.
size_t size() const noexcept
Return the number of nodes that have the tree.
Node *& getRoot() noexcept
Return the tree's root.
Node * search(const Key &key) const noexcept
Search a key.
Node * insert(Node *p) noexcept
Insert a node into the tree.
Node * search(const Key &key) const noexcept
Search for a key in the tree.
Node *& getRoot() noexcept
Get reference to root pointer.
Node * search(const Key &key) const noexcept
Search a key in a treap.
Definition tpl_treap.H:220
Node *& getRoot() noexcept
Return the tree's root.
Definition tpl_treap.H:212
Node * insert(Node *root, Node *p) noexcept
Definition tpl_treap.H:227
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
NodeType< Key > Node
Node *& getRoot() noexcept
Get the top-down splay tree's root.
Node * insert(Node *p) noexcept
Inserts a node in a top-down splay tree.
Node * search(const Key &key) noexcept
Searches a key in a top-down splay tree.
iterator end() noexcept
Return an STL-compatible end iterator.
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
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
size_t computeHeightRec(Node *root) noexcept
Compute recursively the height of root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
static void prefix(Node *root, DynList< Node * > &acc)
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
#define COLOR(p)
Definition quadnode.H:58
#define RED
Red color constant (newly inserted nodes are red)
Definition rbNode.H:73
AVL binary search tree with nodes without virtual destructor.
Definition tpl_avl.H:704
Binary search tree with nodes without virtual destructors,.
Randomized binary search tree.
Red-black tree with nodes without virtual destructor.
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:611
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
Generic unbalanced binary search tree.
Randomized binary search tree.
Red-Black tree implementation (bottom-up balancing).
Top-down splay tree implementation (without rank support).
Treap: randomized BST combining tree and heap properties.
ofstream output
Definition writeHeap.C:213
void write_bin(const vector< int > &values, const string &prefix)
Definition write_tree.C:508
static int rb_pos
Definition write_tree.C:325
void write_rand(const vector< int > &values, const string &prefix)
Definition write_tree.C:466
void write_avl(const vector< int > &values, const string &prefix)
Definition write_tree.C:283
static void bin_print_key(BinTree< int >::Node *p, int, int)
Definition write_tree.C:503
vector< int > generate_random_values(size_t n, unsigned int seed)
Definition write_tree.C:247
static void avl_print_key(Avl_Tree< int >::Node *p, int, int)
Definition write_tree.C:278
void write_treap(const vector< int > &values, const string &prefix)
Definition write_tree.C:424
static void treap_print_key(Treap< int >::Node *p, int, int)
Definition write_tree.C:419
void write_splay(const vector< int > &values, const string &prefix)
Definition write_tree.C:382
static void rb_write_colors(Rb_Tree< int >::Node *p, int, int)
Definition write_tree.C:326
static void splay_print_key(Splay_Tree< int >::Node *p, int, int)
Definition write_tree.C:377
void write_rb(const vector< int > &values, const string &prefix)
Definition write_tree.C:333
static void rand_print_key(Rand_Tree< int >::Node *p, int, int)
Definition write_tree.C:461
static ofstream * current_output
Definition write_tree.C:272
static void rb_print_key(Rb_Tree< int >::Node *p, int, int)
Definition write_tree.C:320