Aleph-w  1.5a.2
Biblioteca general de algoritmos y estructuras de datos
 Todo Clases Archivos Funciones Variables 'typedefs' Enumeraciones Amigas Grupos Páginas
graph_to_tree.H
1 # ifndef GRAPH_TO_TREE_H
2 # define GRAPH_TO_TREE_H
3 
4 # include <tpl_tree_node.H>
5 # include <tpl_graph.H>
6 # include <tpl_graph_utils.H>
7 
8 namespace Aleph {
9 
10  template <class GT, typename Key, class Convert> static
11 Tree_Node<Key> * graph_to_tree_node(GT & g, typename GT::Node * groot);
12  template <class GT, typename Key, class Convert> static void
13 __graph_to_tree_node(GT & g, typename GT::Node * groot,
14  Tree_Node<Key> * troot);
15 
16  template <class GT, typename Key, typename Convert, class SA> static inline
17 void __graph_to_tree_node(GT & g, typename GT::Node * groot,
18  Tree_Node<Key> * troot);
19 
89  template <class GT, typename Key,
90  class Convert, class SA = Dft_Show_Arc<GT> > inline
91 Tree_Node<Key> * graph_to_tree_node(GT & g, typename GT::Node * groot)
92 {
93  if (not is_graph_acyclique<GT>(g))
94  throw std::domain_error("Graph is not a tree (not acyclique)");
95 
96  Tree_Node<Key> * troot = new Tree_Node<Key>; // apartar memoria raíz
97 
98  Convert () (groot, troot); //convertir de groot y copiar a troot
99 
100  __graph_to_tree_node <GT, Key, Convert, SA> (g, groot, troot);
101 
102  return troot;
103 }
104 
167  template <class GT, typename Key,
168  class Convert,
169  class SA = Dft_Show_Arc<GT> >
171 {
172  SA & sa;
173  Convert * convert;
174 
175  void graph_to_tree(typename GT::Node * groot,
176  Tree_Node<Key> * troot)
177  {
178  typedef typename GT::Node Node;
179  typedef typename GT::Arc Arc;
180 
181  // recorrer arcos de groot y construir recursivamente
182  for (Node_Arc_Iterator<GT, SA> it(groot, sa); it.has_curr(); it.next())
183  {
184  Arc * arc = it.get_current_arc();
185  if (IS_ARC_VISITED(arc, Convert_Tree))
186  continue;
187 
188  ARC_BITS(arc).set_bit(Convert_Tree, true); // arc visitado
189  Node * gtgt = it.get_tgt_node();
190  Tree_Node<Key> * ttgt = new Tree_Node<Key>;
191 
192  (*convert) (gtgt, ttgt); // asignarle la clave
193 
194  troot->insert_rightmost_child(ttgt); // insertarlo como hijo
195 
196  graph_to_tree(gtgt, ttgt);
197  }
198  }
199 
200  Tree_Node<Key> * graph_to_tree(GT & g,
201  typename GT::Node * groot,
202  Convert & conv)
203  {
204  if (not is_graph_acyclique<GT>(g))
205  throw std::domain_error("Graph is not a tree (not acyclique)");
206 
207  Tree_Node<Key> * troot = new Tree_Node<Key>; // apartar memoria raíz
208 
209  convert = &conv;
210 
211  (*convert) (groot, troot); //convertir de groot y copiar a troot
212 
213  graph_to_tree(groot, troot);
214 
215  return troot;
216  }
217 
218 public:
219 
220  Graph_To_Tree_Node(SA && __sa = SA()) : sa(__sa) { /* empty */ }
221 
222  Graph_To_Tree_Node(SA & __sa) : sa(__sa) { /* empty */ }
223 
235  Tree_Node<Key> *
236  operator () (GT & g, typename GT::Node * groot, Convert && conv = Convert())
237  {
238  return graph_to_tree(g, groot, conv);
239  }
240 
241  Tree_Node<Key> *
242  operator () (GT & g, typename GT::Node * groot, Convert & conv)
243  {
244  return graph_to_tree(g, groot, conv);
245  }
246 };
247 
248 
249  template <class GT, typename Key, typename Convert, class SA> static inline
250 void __graph_to_tree_node(GT & g, typename GT::Node * groot,
251  Tree_Node<Key> * troot)
252 {
253  typedef typename GT::Node Node;
254  typedef typename GT::Arc Arc;
255 
256  // recorrer arcos de groot y construir recursivamente
257  for (Node_Arc_Iterator<GT, SA> it(groot); it.has_current(); it.next())
258  {
259  Arc * arc = it.get_current_arc();
260  if (IS_ARC_VISITED(arc, Convert_Tree))
261  continue;
262 
263  ARC_BITS(arc).set_bit(Convert_Tree, true); // arc visitado
264  Node * gtgt = it.get_tgt_node();
265  Tree_Node<Key> * ttgt = new Tree_Node<Key>;
266 
267  Convert () (gtgt, ttgt); // asignarle la clave
268 
269  troot->insert_rightmost_child(ttgt); // insertarlo como hijo
270 
271  __graph_to_tree_node <GT, Key, Convert, SA> (g, gtgt, ttgt);
272  }
273 }
274 
275 } // end namespace Aleph
276 
277 # endif // GRAPH_TO_TREE_H
Tree_Node< Key > * graph_to_tree_node(GT &g, typename GT::Node *groot)
Definition: graph_to_tree.H:91
Tree_Node< Key > * operator()(GT &g, typename GT::Node *groot, Convert &&conv=Convert())
Definition: graph_to_tree.H:236
void insert_rightmost_child(Tree_Node *p)
Definition: tpl_tree_node.H:327
Definition: tpl_tree_node.H:35
Definition: graph_to_tree.H:170
#define IS_ARC_VISITED(p, bit)
Definition: aleph-graph.H:275
Definition: tpl_graph.H:634
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
#define ARC_BITS(p)
Definition: aleph-graph.H:266
Definition: tpl_graph.H:694

Leandro Rabindranath León