Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
generate_tree.H
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
43# ifndef GENERATE_TREE_H
44# define GENERATE_TREE_H
45
46# include <fstream>
47# include <memory>
48# include <string>
49# include <tpl_dfs_stack.H>
50# include <tpl_tree_node.H>
51# include <tpl_binNodeUtils.H>
52# include <ah-errors.H>
53
54namespace Aleph
55{
56 inline std::string escape_dot_label(const std::string & text)
57 {
58 std::string escaped;
59 escaped.reserve(text.size());
60
61 for (const char ch : text)
62 switch (ch)
63 {
64 case '\\':
65 escaped += "\\\\";
66 break;
67 case '"':
68 escaped += "\\\"";
69 break;
70 case '\n':
71 escaped += "\\n";
72 break;
73 case '\r':
74 escaped += "\\r";
75 break;
76 case '\t':
77 escaped += "\\t";
78 break;
79 default:
80 escaped += ch;
81 }
82
83 return escaped;
84 }
85
86 template <class Node>
87 struct Dft_Write
88 {
89 std::string operator ()(Node *p)
90 {
91 return to_str(p->get_key());
92 }
93 };
94
95 /*
96 Write must give std::string Write::operator () (Node *), which turns
97 the key of the node into a printable std::string.
98
99 The nodes go in preorder, as a recursion would take them; deway holds
100 the positions of the nodes of the path. A path can be as long as the
101 tree is deep, too long for recursion: the parent of a node is found
102 through its left siblings and its first child.
103 */
104
105 template <typename Node, class Write>
106 static
108 int deway[],
109 const size_t & current_level,
110 const size_t & size,
111 std::ostream & output)
112 {
113 Node *p = node;
114 size_t level = current_level;
115 while (true)
116 {
117 // the Dewey number of the parent
118 output << "Node ";
119 for (size_t i = 0; i < level; ++i)
120 {
121 output << deway[i];
122
123 if (i < level - 1)
124 output << ".";
125 }
126 output << " \"" << Write()(p) << "\" " << std::endl;
127
128 if (auto *child = static_cast<Node *>(p->get_left_child()))
129 {
130 ah_overflow_error_if(level + 1 >= size)
131 << "Allocated size for deway array has been exceeded";
132
133 deway[level + 1] = 0;
134 p = child;
135 ++level;
136 continue;
137 }
138
139 // the right sibling of p, or of its lowest ancestor below node
140 // that has one
141 while (p != node and p->is_rightmost())
142 {
143 p = static_cast<Node *>(p->get_parent());
144 --level;
145 }
146
147 if (p == node)
148 return;
149
150 p = static_cast<Node *>(p->get_right_sibling());
151 ++deway[level];
152 }
153 }
154
155
158 constexpr size_t Max_Tree_Node_Depth = 1024;
159
189 template <typename Node, class Write = Dft_Write<Node>>
191 std::ostream & out,
192 const int & tree_number = 0)
193 {
194 out << "Root \"" << Write()(root) << "\" " << std::endl;
195
196 // the Dewey numbers, as long as the tree is deep
197 const size_t size = compute_height(root) + 1;
198 const auto deway = std::make_unique<int[]>(size);
199
200 constexpr int level = 0; // the level where it starts
201
202 deway[level] = tree_number; // level 0 is the number of the tree
203
204 Node *child = static_cast<Node *>(root->get_left_child());
205 for (int i = 0; child != nullptr;
206 i++, child = static_cast<Node *>(child->get_right_sibling()))
207 {
208 deway[1] = i;
209 __generate_tree<Node, Write>(child, deway.get(), level + 1,
210 size, out);
211 }
212 }
213
235 template <typename Node, class Write = Dft_Write<Node>>
236 void generate_forest(Node *root, std::ostream & out)
237 {
238 Node *tree = root;
239
240 for (int i = 0; tree != nullptr; i++, tree = tree->get_right_sibling())
241 generate_tree<Node, Write>(tree, out, i);
242 }
243
244
268 template <typename Node, class Write>
269 void generate_btree(Node *root, std::ostream & out)
270 {
271 out << "start-prefix ";
273 out << std::endl
274 << "start_infix ";
276 out << std::endl;
277 }
278
300 template <typename Node, class Write = Dft_Write<Node>>
301 void generate_tree_graphviz(Node *root, std::ostream & out)
302 {
303 out << "digraph Tree {" << std::endl;
304 out << " node [shape=circle, fontname=\"Helvetica\"];" << std::endl;
305 out << " edge [fontname=\"Helvetica\"];" << std::endl;
306
307 if (root == nullptr)
308 {
309 out << "}" << std::endl;
310 return;
311 }
312
313 // In preorder, as a recursion would go. The ancestors of the node wait
314 // in a stack, to give the arcs to the right siblings of their children:
315 // a path can be as long as the tree is deep, too long for recursion.
317 Node * p = root;
318 while (true)
319 {
320 out << " \"" << p << "\" [label=\""
321 << escape_dot_label(Write()(p)) << "\"];" << std::endl;
322
323 if (auto * child = static_cast<Node *>(p->get_left_child()))
324 {
325 out << " \"" << p << "\" -> \"" << child << "\";" << std::endl;
326 path.push(p);
327 p = child;
328 continue;
329 }
330
331 // the right sibling of p, or of its lowest ancestor that has one
332 while (not path.is_empty() and p->is_rightmost())
333 {
334 p = path.top();
335 path.pop();
336 }
337
338 if (path.is_empty())
339 break;
340
341 auto * sibling = static_cast<Node *>(p->get_right_sibling());
342 out << " \"" << path.top() << "\" -> \"" << sibling << "\";" << std::endl;
343 p = sibling;
344 }
345
346 out << "}" << std::endl;
347 }
348} // end namespace Aleph
349
350# endif // GENERATE_TREE_H
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
size_t size_t int32_t * out
Definition ca-c-api.h:120
Stack of the frames of a depth-first search that does not recurse.
T & push(Args &&...args)
Builds a frame on top of the stack.
bool is_empty() const noexcept
Whether the stack has no frames.
T & top() noexcept
The top frame; the stack must not be empty.
void pop() noexcept
Destroys the top frame; the stack must not be empty.
Generic inorder traversal of a binary tree.
Generic preorder traversal of a binary tree.
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
__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
void generate_btree(Node *root, std::ostream &out)
Generate a binary tree specification for the btreepic drawing tool.
void generate_forest(Node *root, std::ostream &out)
Generate a forest specification for the ntreepic drawing tool.
void generate_tree(Node *root, std::ostream &out, const int &tree_number=0)
Generate a tree specification for the ntreepic drawing tool.
size_t compute_height(Node *root)
Computes the height of the tree root.
void generate_tree_graphviz(Node *root, std::ostream &out)
Generate a Graphviz DOT specification for a tree.
Main namespace for Aleph-w library functions.
constexpr size_t Max_Tree_Node_Depth
The depth of the trees that generate_tree() wrote before 7.3.2; it writes trees of any depth now.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::string escape_dot_label(const std::string &text)
and
Check uniqueness with explicit hash + equality functors.
std::string to_str(const double d)
Convert double to a std::string with maximum round-trip precision.
static void __generate_tree(Node *node, int deway[], const size_t &current_level, const size_t &size, std::ostream &output)
int tree_number
Definition ntreepic.C:1377
std::string operator()(Node *p)
Utility functions for binary tree operations.
Stack of the frames of a depth-first search that does not recurse.
General tree (n-ary tree) node.
ofstream output
Definition writeHeap.C:215