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 <tpl_tree_node.H>
48# include <tpl_binNodeUtils.H>
49# include <ah-errors.H>
50
51namespace Aleph
52{
53 template <class Node>
54 struct Dft_Write
55 {
56 std::string operator ()(Node *p)
57 {
58 return to_str(p->get_key());
59 }
60 };
61
62 /*
63 Write debe exportar const std::string Write::operator () (Node *)
64
65 Que se encarga de convertir el valor de clave contenido en el nodo a
66 un std::string imprimible
67 */
68
69 template <typename Node, class Write>
70 static
72 int deway[],
73 const size_t & current_level,
74 const size_t & size,
75 std::ostream & output)
76 {
77 ah_overflow_error_if(current_level >= size)
78 << "Allocated size for deway array has been exceeded";
79
80 // imprimir número de Deway del padre
81 output << "Node ";
82 for (size_t i = 0; i < current_level; ++i)
83 {
84 output << deway[i];
85
86 if (i < current_level - 1)
87 output << ".";
88 }
89 output << " \"" << Write()(node) << "\" " << std::endl;
90
91 Node *child = static_cast<Node *>(node->get_left_child());
92 for (int i = 0; child != nullptr;
93 i++, child = static_cast<Node *>(child->get_right_sibling()))
94 {
95 deway[current_level + 1] = i;
97 (child, deway, current_level + 1, size, output);
98 }
99 }
100
101
102 constexpr size_t Max_Tree_Node_Depth = 1024;
103
127 template <typename Node, class Write = Dft_Write<Node>>
129 std::ostream & out,
130 const int & tree_number = 0)
131 {
132 out << "Root \"" << Write()(root) << "\" " << std::endl;
133
134 const auto deway = new int [Max_Tree_Node_Depth];
135
136 constexpr int level = 0; // Este es el nivel de partida
137
138 deway[level] = tree_number; // nivel 0 en el número del árbol
139
140 Node *child = static_cast<Node *>(root->get_left_child());
141 for (int i = 0; child != nullptr;
142 i++, child = static_cast<Node *>(child->get_right_sibling()))
143 {
144 deway[1] = i;
145 __generate_tree<Node, Write>(child, deway, level + 1,
147 }
148
149 delete [] deway;
150 }
151
173 template <typename Node, class Write = Dft_Write<Node>>
174 void generate_forest(Node *root, std::ostream & out)
175 {
176 Node *tree = root;
177
178 for (int i = 0; tree != nullptr; i++, tree = tree->get_right_sibling())
180 }
181
182
206 template <typename Node, class Write>
207 void generate_btree(Node *root, std::ostream & out)
208 {
209 out << "start-prefix ";
211 out << std::endl
212 << "start_infix ";
214 out << std::endl;
215 }
216} // end namespace Aleph
217
218# 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:117
__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.
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
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)
DynList< T > maps(const C &c, Op op)
Classic map operation.
int tree_number
Definition ntreepic.C:1372
std::string operator()(Node *p)
Utility functions for binary tree operations.
General tree (n-ary tree) node.
ofstream output
Definition writeHeap.C:213