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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <cstdlib>
28# include <ctime>
29# include <iostream>
30# include <random>
31# include <memory>
32# include <tpl_binNodeUtils.H>
33# include <tpl_tree_node.H>
34
35
36# include <cassert>
37using namespace std;
38using namespace Aleph;
39
41
42int random(int l, int r)
43{
44 assert(l <= r);
46 return dist(engine);
47}
48
49
51{
52 if (l > r)
53 return nullptr;
54
55 auto * root = new BinNode<int> (random(l, r));
56
57 LLINK(root) = random_tree(l, KEY(root) - 1);
58 RLINK(root) = random_tree(KEY(root) + 1, r);
59
60 return root;
61}
62
63
64 template <class Node>
65static void printNode(Node * node, int, int)
66{
67 cout << " " << node->get_key();
68}
69
70
71int main(int argc, char *argv[])
72{
73 int n = 2;
74 if (argc > 1)
75 {
76 try { n = stoi(argv[1]); }
77 catch (...) { n = 2; }
78 }
79
80 if (n <= 0)
81 {
82 cerr << "Error: n must be a positive integer." << endl;
83 return 1;
84 }
85
86 unsigned int t = std::time(0);
87
88 if (argc > 2)
89 {
90 try { t = static_cast<unsigned int>(stoul(argv[2])); }
91 catch (...) { t = std::time(0); }
92 }
93
94 engine.seed(t);
95
96 cout << argv[0] << " " << n << " " << t << endl;
97
98 auto bp_deleter = [](BinNode<int> * p) { destroyRec(p); };
100 BinNode<int> * bp = bp_ptr.get();
101
102 cout << "Prefijo:";
104 cout << endl << endl;
105
106 cout << "Infijo:";
108 cout << endl << endl;
109
110 auto forest_deleter = [](Tree_Node<int> * p) { destroy_forest(p); };
113 Tree_Node<int> * tree = tree_ptr.get();
114
116
119 BinNode<int> * prb = prb_ptr.get();
120
122}
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
Node for binary search tree.
Generic m-ary trees.
__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.
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
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.
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