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
32
99# include <iostream>
100# include <tclap/CmdLine.h>
101# include <tpl_binNodeUtils.H>
102# include <tpl_tree_node.H>
103# include <generate_tree.H>
104# include <ah-errors.H>
105
106# include <cstdlib>
107# include <cassert>
108using namespace std;
109# include <cstdlib>
110# include <cassert>
111using namespace Aleph;
112
121void deway(Tree_Node<int> * p, int prefix[], const int & len,
122 const size_t & dim)
123{
124 int i = 1;
125
126 if (len == 0)
127 cout << "Root ";
128 else
129 cout << "Node " << prefix[i++];
130
131 while (i <= len)
132 cout << "." << prefix[i++];
133
134 cout << " \"" << p->get_key() << "\"" << endl;
135
136 if (static_cast<size_t>(len) >= dim)
137 ah_overflow_error_if(true) << "Array dimension is smaller than Deway chain";
138
139 Tree_Node<int> * child = p->get_left_child();
140
141 for (int j = 0; child != nullptr; ++j, child = child->get_right_sibling())
142 {
143 prefix[len + 1] = j;
144 deway(child, prefix, len + 1, dim);
145 }
146}
147
154void deway(Tree_Node<int> * p, const int & h)
155{
156 const size_t dim = 10 * h;
157
158 int * prefix = new int [dim];
159
160 for (int i = 0; p != nullptr; ++i, p = p->get_right_sibling())
161 {
162 prefix[0] = i;
163 deway(p, prefix, 0, dim);
164 }
165
166 delete [] prefix;
167}
168
169template <class Node>
170static void printNode(Node * node, int, int)
171{
172 cout << " " << node->get_key();
173}
174
178int random_int(int l, int r)
179{
180 assert(l <= r);
181 const int n = r - l;
182 const int rd = 1 + static_cast<int>(1.0 * n * rand() / (RAND_MAX + 1.0));
183 return l + rd - 1;
184}
185
190{
191 if (l > r)
192 return nullptr;
193
194 auto * root = new BinNode<int>(random_int(l, r));
195
196 LLINK(root) = random_tree(l, KEY(root) - 1);
197 RLINK(root) = random_tree(KEY(root) + 1, r);
198
199 return root;
200}
201
202int main(int argc, char * argv[])
203{
204 try
205 {
206 TCLAP::CmdLine cmd("Deway numbering example for trees", ' ', "1.0");
207
208 TCLAP::ValueArg<int> nArg("n", "nodes",
209 "Number of nodes in the tree",
210 false, 10, "int");
211 cmd.add(nArg);
212
213 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
214 "Random seed (0 = use time)",
215 false, 0, "unsigned int");
216 cmd.add(seedArg);
217
218 cmd.parse(argc, argv);
219
220 int n = nArg.getValue();
221 unsigned int t = seedArg.getValue();
222
223 if (t == 0)
224 t = time(nullptr);
225
226 srand(t);
227
228 cout << "Deway Numbering Example" << endl;
229 cout << "=======================" << endl;
230 cout << "Parameters: n=" << n << ", seed=" << t << endl << endl;
231
232 // Generate random binary tree
233 BinNode<int> * bp = random_tree(1, n);
234
235 cout << "Binary tree (preorder):";
237 cout << endl << endl;
238
239 cout << "Binary tree (inorder):";
241 cout << endl << endl;
242
243 // Convert to forest
245
246 // bin_to_forest creates a forest where sibling trees are linked,
247 // but the is_root flag is not set correctly for siblings.
248 // We fix this by setting is_root=true for all sibling trees.
249 for (auto* t = tree; t != nullptr; t = t->get_right_sibling())
250 t->set_is_root(true);
251
252 cout << "Forest (preorder):";
254 cout << endl << endl;
255
256 cout << "Forest (postorder):";
258 cout << endl << endl;
259
260 // Verify conversion is reversible
263 cout << "Conversion verification: PASSED" << endl << endl;
264
265 // Print Deway numbering
266 cout << "Deway Numbering:" << endl;
267 cout << "----------------" << endl;
268 deway(tree, computeHeightRec(bp));
269
270 // Cleanup
271 destroyRec(bp);
273 destroy_forest(tree);
274
275 cout << endl << "Done." << endl;
276 }
277 catch (TCLAP::ArgException &e)
278 {
279 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
280 return 1;
281 }
282
283 return 0;
284}
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:463
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
long double h
Definition btreepic.C:154
Node for binary search tree.
Generic m-ary trees.
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:121
static void printNode(Node *node, int, int)
Definition deway.C:170
BinNode< int > * random_tree(int l, int r)
Recursively build a random binary search tree.
Definition deway.C:189
int random_int(int l, int r)
Generate a random integer in range [l, r].
Definition deway.C:178
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:4052
__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.
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
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.
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:43
gsl_rng * r
Utility functions for binary tree operations.
General tree (n-ary tree) node.
DynList< int > l