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
106using namespace std;
107using namespace Aleph;
108
117void deway(Tree_Node<int> * p, int prefix[], const int & len,
118 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",
205 "Number of nodes in the tree",
206 false, 10, "int");
207 cmd.add(nArg);
208
209 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
210 "Random seed (0 = use time)",
211 false, 0, "unsigned int");
212 cmd.add(seedArg);
213
214 cmd.parse(argc, argv);
215
216 int n = nArg.getValue();
217 unsigned int t = seedArg.getValue();
218
219 if (t == 0)
220 t = time(nullptr);
221
222 srand(t);
223
224 cout << "Deway Numbering Example" << endl;
225 cout << "=======================" << endl;
226 cout << "Parameters: n=" << n << ", seed=" << t << endl << endl;
227
228 // Generate random binary tree
229 BinNode<int> * bp = random_tree(1, n);
230
231 cout << "Binary tree (preorder):";
233 cout << endl << endl;
234
235 cout << "Binary tree (inorder):";
237 cout << endl << endl;
238
239 // Convert to forest
241
242 // bin_to_forest creates a forest where sibling trees are linked,
243 // but the is_root flag is not set correctly for siblings.
244 // We fix this by setting is_root=true for all sibling trees.
245 for (auto* t = tree; t != nullptr; t = t->get_right_sibling())
246 t->set_is_root(true);
247
248 cout << "Forest (preorder):";
250 cout << endl << endl;
251
252 cout << "Forest (postorder):";
254 cout << endl << endl;
255
256 // Verify conversion is reversible
259 cout << "Conversion verification: PASSED" << endl << endl;
260
261 // Print Deway numbering
262 cout << "Deway Numbering:" << endl;
263 cout << "----------------" << endl;
264 deway(tree, computeHeightRec(bp));
265
266 // Cleanup
267 destroyRec(bp);
269 destroy_forest(tree);
270
271 cout << endl << "Done." << endl;
272 }
273 catch (TCLAP::ArgException &e)
274 {
275 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
276 return 1;
277 }
278
279 return 0;
280}
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
int main()
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:117
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: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.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
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.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Utility functions for binary tree operations.
General tree (n-ary tree) node.
DynList< int > l