Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_bin_to_tree.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32# include <cstdlib>
33# include <ctime>
34# include <iostream>
35# include <random>
36# include <memory>
37# include <tpl_binNodeUtils.H>
38# include <tpl_tree_node.H>
39
40
41# include <cassert>
42using namespace std;
43using namespace Aleph;
44
46
47int random(int l, int r)
48{
49 assert(l <= r);
51 return dist(engine);
52}
53
54
56{
57 if (l > r)
58 return nullptr;
59
60 auto * root = new BinNode<int> (random(l, r));
61
62 LLINK(root) = random_tree(l, KEY(root) - 1);
63 RLINK(root) = random_tree(KEY(root) + 1, r);
64
65 return root;
66}
67
68
69 template <class Node>
70static void printNode(Node * node, int, int)
71{
72 cout << " " << node->get_key();
73}
74
75
76int main(int argc, char *argv[])
77{
78 int n = 2;
79 if (argc > 1)
80 {
81 try { n = stoi(argv[1]); }
82 catch (...) { n = 2; }
83 }
84
85 if (n <= 0)
86 {
87 cerr << "Error: n must be a positive integer." << endl;
88 return 1;
89 }
90
91 unsigned int t = std::time(0);
92
93 if (argc > 2)
94 {
95 try { t = static_cast<unsigned int>(stoul(argv[2])); }
96 catch (...) { t = std::time(0); }
97 }
98
99 engine.seed(t);
100
101 cout << argv[0] << " " << n << " " << t << endl;
102
103 auto bp_deleter = [](BinNode<int> * p) { destroyRec(p); };
105 BinNode<int> * bp = bp_ptr.get();
106
107 cout << "Prefijo:";
109 cout << endl << endl;
110
111 cout << "Infijo:";
113 cout << endl << endl;
114
115 auto forest_deleter = [](Tree_Node<int> * p) { destroy_forest(p); };
118 Tree_Node<int> * tree = tree_ptr.get();
119
121
124 BinNode<int> * prb = prb_ptr.get();
125
127}
WeightedDigraph::Node Node
int main()
@ KEY
Definition btreepic.C:169
Node for binary search tree.
Forward declaration used by CRTP helpers before the full node definition.
__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:4071
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
TNode * bin_to_forest(BNode *broot)
Converts a binary tree to its equivalent forest.
void destroy_forest(Node *root)
Destroys (frees memory) the forest whose first tree is root.
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
void forest_preorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Preorder traversal of a forest.
BNode * forest_to_bin(TNode *root)
Converts a forest to its equivalent binary tree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool areEquivalents(Node *t1, Node *t2, Equal &op) noexcept
Return true if trees are equivalents.
STL namespace.
#define RLINK(i, n)
#define LLINK(i, n)
static void printNode(Node *node, int, int)
BinNode< int > * random_tree(int l, int r)
int random(int l, int r)
static mt19937 engine
gsl_rng * r
Utility functions for binary tree operations.
General tree (n-ary tree) node.
DynList< int > l