Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_to_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
96# ifndef GRAPH_TO_TREE_H
97# define GRAPH_TO_TREE_H
98
99# include <tpl_tree_node.H>
100# include <tpl_graph.H>
101# include <tpl_graph_utils.H>
102 # include <ah-errors.H>
103
104namespace Aleph {
105
106 template <class GT, typename Key, class Convert> static
108 template <class GT, typename Key, class Convert> static void
111
112 template <class GT, typename Key, typename Convert, class SA> static inline
113void __graph_to_tree_node(GT & g, typename GT::Node * groot,
115
158 template <class GT, typename Key,
159 class Convert, class SA = Dft_Show_Arc<GT>> inline
161{
163 << "Graph is not a tree (not acyclique)";
164
165 Tree_Node<Key> * troot = new Tree_Node<Key>; // apartar memoria raíz
166
167 Convert () (groot, troot); //convertir de groot y copiar a troot
168
170
171 return troot;
172}
173
208 template <class GT, typename Key,
209 class Convert,
210 class SA = Dft_Show_Arc<GT> >
212{
214 Convert * convert = nullptr;
215
217 {
218 typedef typename GT::Node Node;
219 typedef typename GT::Arc Arc;
220
221 // recorrer arcos de groot y construir recursivamente
222 for (Node_Arc_Iterator<GT, SA> it(groot, sa); it.has_curr(); it.next_ne())
223 {
224 Arc * arc = it.get_current_arc_ne();
225 if (IS_ARC_VISITED(arc, Convert_Tree))
226 continue;
227
228 ARC_BITS(arc).set_bit(Convert_Tree, true); // arc visitado
229 Node * gtgt = it.get_tgt_node();
231
232 (*convert) (gtgt, ttgt); // asignarle la clave
233
234 troot->insert_rightmost_child(ttgt); // insertarlo como hijo
235
237 }
238 }
239
241 typename GT::Node * groot,
242 Convert & conv)
243 {
245 << "Graph is not a tree (not acyclique)";
246
247 Tree_Node<Key> * troot = new Tree_Node<Key>; // apartar memoria raíz
248
249 convert = &conv;
250
251 (*convert) (groot, troot); //convertir de groot y copiar a troot
252
254
255 return troot;
256 }
257
258public:
259
260 Graph_To_Tree_Node(SA __sa = SA()) : sa(__sa) { /* empty */ }
261
274 {
275 return graph_to_tree(g, groot, conv);
276 }
277
280 {
281 return graph_to_tree(g, groot, conv);
282 }
283};
284
285
286 template <class GT, typename Key, typename Convert, class SA> static inline
289{
290 typedef typename GT::Node Node;
291 typedef typename GT::Arc Arc;
292
293 // recorrer arcos de groot y construir recursivamente
294 for (Node_Arc_Iterator<GT, SA> it(groot); it.has_curr(); it.next_ne())
295 {
296 Arc * arc = it.get_current_arc_ne();
297 if (IS_ARC_VISITED(arc, Convert_Tree))
298 continue;
299
300 ARC_BITS(arc).set_bit(Convert_Tree, true); // arc visitado
301 Node * gtgt = it.get_tgt_node();
303
304 Convert () (gtgt, ttgt); // asignarle la clave
305
306 troot->insert_rightmost_child(ttgt); // insertarlo como hijo
307
309 }
310}
311
312} // end namespace Aleph
313
314# endif // GRAPH_TO_TREE_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Functor class to convert a tree graph to Tree_Node structure.
Tree_Node< Key > * operator()(GT &g, typename GT::Node *groot, Convert &&conv=Convert())
Convert a tree graph to Tree_Node structure.
void graph_to_tree(typename GT::Node *groot, Tree_Node< Key > *troot)
Graph_To_Tree_Node(SA __sa=SA())
Tree_Node< Key > * graph_to_tree(GT &g, typename GT::Node *groot, Convert &conv)
_Graph_Node Node
The graph type.
Definition tpl_graph.H:432
Generic m-ary trees.
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
#define ARC_BITS(p)
Return the control bits of arc p.
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
@ Convert_Tree
Definition aleph-graph.H:81
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
static Tree_Node< Key > * graph_to_tree_node(GT &g, typename GT::Node *groot)
static void __graph_to_tree_node(GT &g, typename GT::Node *groot, Tree_Node< Key > *troot)
DynList< T > maps(const C &c, Op op)
Classic map operation.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1000
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1119
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
General tree (n-ary tree) node.