Aleph-w 3.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 ah_overflow_error_if(true) << "Array dimension is smaller than Deway chain";
134
135 Tree_Node<int> *child = p->get_left_child();
136
137 for (int j = 0; child != nullptr; ++j, child = child->get_right_sibling())
138 {
139 prefix[len + 1] = j;
140 deway(child, prefix, len + 1, dim);
141 }
142}
143
150void deway(Tree_Node<int> *p, const int &h)
151{
152 const size_t dim = 10 * h;
153
154 int *prefix = new int[dim];
155
156 for (int i = 0; p != nullptr; ++i, p = p->get_right_sibling())
157 {
158 prefix[0] = i;
159 deway(p, prefix, 0, dim);
160 }
161
162 delete[] prefix;
163}
164
165template <class Node>
166static void printNode(Node *node, int, int)
167{
168 cout << " " << node->get_key();
169}
170
174int random_int(int l, int r)
175{
176 assert(l <= r);
177 const int n = r - l;
178 const int rd = 1 + static_cast<int>(1.0 * n * rand() / (RAND_MAX + 1.0));
179 return l + rd - 1;
180}
181
186{
187 if (l > r)
188 return nullptr;
189
190 auto *root = new BinNode<int>(random_int(l, r));
191
192 LLINK(root) = random_tree(l, KEY(root) - 1);
193 RLINK(root) = random_tree(KEY(root) + 1, r);
194
195 return root;
196}
197
198int main(int argc, char *argv[])
199{
200 try
201 {
202 TCLAP::CmdLine cmd("Deway numbering example for trees", ' ', "1.0");
203
204 TCLAP::ValueArg<int> nArg("n", "nodes", "Number of nodes in the tree", false, 10, "int");
205 cmd.add(nArg);
206
207 TCLAP::ValueArg<unsigned int> seedArg("s", "seed", "Random seed (0 = use time)", false, 0,
208 "unsigned int");
209 cmd.add(seedArg);
210
211 cmd.parse(argc, argv);
212
213 int n = nArg.getValue();
214 unsigned int t = seedArg.getValue();
215
216 if (t == 0)
217 t = time(nullptr);
218
219 srand(t);
220
221 cout << "Deway Numbering Example" << endl;
222 cout << "=======================" << endl;
223 cout << "Parameters: n=" << n << ", seed=" << t << endl << endl;
224
225 // Generate random binary tree
226 BinNode<int> *bp = random_tree(1, n);
227
228 cout << "Binary tree (preorder):";
230 cout << endl << endl;
231
232 cout << "Binary tree (inorder):";
234 cout << endl << endl;
235
236 // Convert to forest
238
239 cout << "Forest (preorder):";
241 cout << endl << endl;
242
243 cout << "Forest (postorder):";
245 cout << endl << endl;
246
247 // Verify conversion is reversible
250 cout << "Conversion verification: PASSED" << endl << endl;
251
252 // Print Deway numbering
253 cout << "Deway Numbering:" << endl;
254 cout << "----------------" << endl;
255 deway(tree, computeHeightRec(bp));
256
257 // Cleanup
258 destroyRec(bp);
260 destroy_forest(tree);
261
262 cout << endl << "Done." << endl;
263 }
264 catch (TCLAP::ArgException &e)
265 {
266 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
267 return 1;
268 }
269
270 return 0;
271}
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:468
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:166
BinNode< int > * random_tree(int l, int r)
Recursively build a random binary search tree.
Definition deway.C:185
int random_int(int l, int r)
Generate a random integer in range [l, r].
Definition deway.C:174
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:4063
__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.
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 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.
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)
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)
CmdLine cmd
Definition testHash.C:48
gsl_rng * r
Utility functions for binary tree operations.
General tree (n-ary tree) node.
DynList< int > l