Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
deway.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
98#include <iostream>
99#include <tclap/CmdLine.h>
100#include <tpl_binNodeUtils.H>
101#include <tpl_tree_node.H>
102#include <generate_tree.H>
103#include <ah-errors.H>
104
105#include <cstdlib>
106#include <cassert>
107using namespace std;
108using namespace Aleph;
109
118void deway(Tree_Node<int> *p, int prefix[], const int &len, const size_t &dim)
119{
120 int i = 1;
121
122 if (len == 0)
123 cout << "Root ";
124 else
125 cout << "Node " << prefix[i++];
126
127 while (i <= len)
128 cout << "." << prefix[i++];
129
130 cout << " \"" << p->get_key() << "\"" << endl;
131
132 if (static_cast<size_t>(len) >= dim)
133 {
134 ah_overflow_error_if(true) << "Array dimension is smaller than Deway chain";
135 }
136
137 Tree_Node<int> *child = p->get_left_child();
138
139 for (int j = 0; child != nullptr; ++j, child = child->get_right_sibling())
140 {
141 prefix[len + 1] = j;
142 deway(child, prefix, len + 1, dim);
143 }
144}
145
152void deway(Tree_Node<int> *p, const int &h)
153{
154 const size_t dim = 10 * h;
155
156 int *prefix = new int[dim];
157
158 for (int i = 0; p != nullptr; ++i, p = p->get_right_sibling())
159 {
160 prefix[0] = i;
161 deway(p, prefix, 0, dim);
162 }
163
164 delete[] prefix;
165}
166
167template <class Node>
168static void printNode(Node *node, int, int)
169{
170 cout << " " << node->get_key();
171}
172
176int random_int(int l, int r)
177{
178 assert(l <= r);
179 const int n = r - l;
180 const int rd = 1 + static_cast<int>(1.0 * n * rand() / (RAND_MAX + 1.0));
181 return l + rd - 1;
182}
183
188{
189 if (l > r)
190 return nullptr;
191
192 auto *root = new BinNode<int>(random_int(l, r));
193
194 LLINK(root) = random_tree(l, KEY(root) - 1);
195 RLINK(root) = random_tree(KEY(root) + 1, r);
196
197 return root;
198}
199
200int main(int argc, char *argv[])
201{
202 try
203 {
204 TCLAP::CmdLine cmd("Deway numbering example for trees", ' ', "1.0");
205
206 TCLAP::ValueArg<int> nArg("n", "nodes", "Number of nodes in the tree", false, 10, "int");
207 cmd.add(nArg);
208
209 TCLAP::ValueArg<unsigned int> seedArg("s", "seed", "Random seed (0 = use time)", false, 0,
210 "unsigned int");
211 cmd.add(seedArg);
212
213 cmd.parse(argc, argv);
214
215 int n = nArg.getValue();
216 unsigned int t = seedArg.getValue();
217
218 if (t == 0)
219 t = time(nullptr);
220
221 srand(t);
222
223 cout << "Deway Numbering Example" << endl;
224 cout << "=======================" << endl;
225 cout << "Parameters: n=" << n << ", seed=" << t << endl << endl;
226
227 // Generate random binary tree
228 BinNode<int> *bp = random_tree(1, n);
229
230 cout << "Binary tree (preorder):";
232 cout << endl << endl;
233
234 cout << "Binary tree (inorder):";
236 cout << endl << endl;
237
238 // Convert to forest
239 Tree_Node<int> *tree = bin_to_forest<Tree_Node<int>, BinNode<int>>(bp);
240
241 cout << "Forest (preorder):";
243 cout << endl << endl;
244
245 cout << "Forest (postorder):";
247 cout << endl << endl;
248
249 // Verify conversion is reversible
250 BinNode<int> *prb = forest_to_bin<Tree_Node<int>, BinNode<int>>(tree);
251 assert(areEquivalents(prb, bp));
252 cout << "Conversion verification: PASSED" << endl << endl;
253
254 // Print Deway numbering
255 cout << "Deway Numbering:" << endl;
256 cout << "----------------" << endl;
257 deway(tree, computeHeightRec(bp));
258
259 // Cleanup
260 destroyRec(bp);
261 destroyRec(prb);
262 destroy_forest(tree);
263
264 cout << endl << "Done." << endl;
265 }
266 catch (TCLAP::ArgException &e)
267 {
268 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
269 return 1;
270 }
271
272 return 0;
273}
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
WeightedDigraph::Node Node
int main()
@ KEY
Definition btreepic.C:169
long double h
Definition btreepic.C:154
Node for binary search tree.
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
Tree_Node * get_right_sibling() const noexcept
Returns the right sibling of this.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
void deway(Tree_Node< int > *p, int prefix[], const int &len, const size_t &dim)
Recursively compute and print Deway numbering for a tree node.
Definition deway.C:118
static void printNode(Node *node, int, int)
Definition deway.C:168
BinNode< int > * random_tree(int l, int r)
Recursively build a random binary search tree.
Definition deway.C:187
int random_int(int l, int r)
Generate a random integer in range [l, r].
Definition deway.C:176
Tree visualization and output generation.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4130
__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:4138
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
void forest_postorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Postorder traversal of a 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 all the memory occupied by the tree root
void forest_preorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Preorder traversal of a forest.
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Main namespace for Aleph-w library functions.
bool areEquivalents(Node *t1, Node *t2, Equal &op) noexcept
Return true if trees are equivalents.
static void prefix(Node *root, DynList< Node * > &acc)
STL namespace.
#define RLINK(i, n)
#define LLINK(i, n)
CmdLine cmd
Definition testHash.C:48
Gsl_Compatible_Rng r
Utility functions for binary tree operations.
General tree (n-ary tree) node.
DynList< int > l