Aleph-w 3.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 <string>
48# include <tpl_tree_node.H>
49# include <tpl_binNodeUtils.H>
50# include <ah-errors.H>
51
52namespace Aleph
53{
54 inline std::string escape_dot_label(const std::string & text)
55 {
56 std::string escaped;
57 escaped.reserve(text.size());
58
59 for (const char ch : text)
60 switch (ch)
61 {
62 case '\\':
63 escaped += "\\\\";
64 break;
65 case '"':
66 escaped += "\\\"";
67 break;
68 case '\n':
69 escaped += "\\n";
70 break;
71 case '\r':
72 escaped += "\\r";
73 break;
74 case '\t':
75 escaped += "\\t";
76 break;
77 default:
78 escaped += ch;
79 }
80
81 return escaped;
82 }
83
84 template <class Node>
85 struct Dft_Write
86 {
87 std::string operator ()(Node *p)
88 {
89 return to_str(p->get_key());
90 }
91 };
92
93 /*
94 Write debe exportar const std::string Write::operator () (Node *)
95
96 Que se encarga de convertir el valor de clave contenido en el nodo a
97 un std::string imprimible
98 */
99
100 template <typename Node, class Write>
101 static
103 int deway[],
104 const size_t & current_level,
105 const size_t & size,
106 std::ostream & output)
107 {
108 ah_overflow_error_if(current_level >= size)
109 << "Allocated size for deway array has been exceeded";
110
111 // imprimir número de Deway del padre
112 output << "Node ";
113 for (size_t i = 0; i < current_level; ++i)
114 {
115 output << deway[i];
116
117 if (i < current_level - 1)
118 output << ".";
119 }
120 output << " \"" << Write()(node) << "\" " << std::endl;
121
122 Node *child = static_cast<Node *>(node->get_left_child());
123 for (int i = 0; child != nullptr;
124 i++, child = static_cast<Node *>(child->get_right_sibling()))
125 {
126 deway[current_level + 1] = i;
128 (child, deway, current_level + 1, size, output);
129 }
130 }
131
132
133 constexpr size_t Max_Tree_Node_Depth = 1024;
134
158 template <typename Node, class Write = Dft_Write<Node>>
160 std::ostream & out,
161 const int & tree_number = 0)
162 {
163 out << "Root \"" << Write()(root) << "\" " << std::endl;
164
165 const auto deway = new int [Max_Tree_Node_Depth];
166
167 constexpr int level = 0; // Este es el nivel de partida
168
169 deway[level] = tree_number; // nivel 0 en el número del árbol
170
171 Node *child = static_cast<Node *>(root->get_left_child());
172 for (int i = 0; child != nullptr;
173 i++, child = static_cast<Node *>(child->get_right_sibling()))
174 {
175 deway[1] = i;
176 __generate_tree<Node, Write>(child, deway, level + 1,
178 }
179
180 delete [] deway;
181 }
182
204 template <typename Node, class Write = Dft_Write<Node>>
205 void generate_forest(Node *root, std::ostream & out)
206 {
207 Node *tree = root;
208
209 for (int i = 0; tree != nullptr; i++, tree = tree->get_right_sibling())
211 }
212
213
237 template <typename Node, class Write>
238 void generate_btree(Node *root, std::ostream & out)
239 {
240 out << "start-prefix ";
242 out << std::endl
243 << "start_infix ";
245 out << std::endl;
246 }
247
264 template <typename Node, class Write = Dft_Write<Node>>
265 void generate_tree_graphviz(Node *root, std::ostream & out)
266 {
267 out << "digraph Tree {" << std::endl;
268 out << " node [shape=circle, fontname=\"Helvetica\"];" << std::endl;
269 out << " edge [fontname=\"Helvetica\"];" << std::endl;
270
271 if (root == nullptr)
272 {
273 out << "}" << std::endl;
274 return;
275 }
276
277 auto traverse = [&out](auto & self, Node * p) -> void {
278 if (p == nullptr)
279 return;
280
281 out << " \"" << p << "\" [label=\""
282 << escape_dot_label(Write()(p)) << "\"];" << std::endl;
283
284 for (typename Node::Children_Iterator it(p); it.has_curr(); it.next())
285 {
286 Node * child = it.get_curr();
287 out << " \"" << p << "\" -> \"" << child << "\";" << std::endl;
288 self(self, child);
289 }
290 };
291
293 out << "}" << std::endl;
294 }
295} // end namespace Aleph
296
297# 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:463
WeightedDigraph::Node Node
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
__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
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.
void generate_tree_graphviz(Node *root, std::ostream &out)
Generate a Graphviz DOT specification for a tree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
constexpr size_t Max_Tree_Node_Depth
size_t size(Node *root) noexcept
bool traverse(Node *root, Op op)
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.
std::string escape_dot_label(const std::string &text)
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:1373
std::string operator()(Node *p)
Utility functions for binary tree operations.
General tree (n-ary tree) node.
ofstream output
Definition writeHeap.C:215