Java iterators for C++:
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Tree_PLR.h
Go to the documentation of this file.
1 // Tree_PLR.h (c) 2009 walter_wabe@yahoo.com
2 
3 #ifdef English_dox
4 /// \file Tree_PLR.h
5 /// \brief Process-Left-Right iterator.
6 /// \author Walter Wabe Acuña <walter_wabe@yahoo.com>
7 /// \author Adolfo Di Mare <adolfo@di-mare.com>
8 /// \date 2009
9 ///
10 #endif
11 #ifdef Spanish_dox
12 /// \file Tree_PLR.h
13 /// \brief Iterador Proceso-Izquierda-Derecha.
14 /// \author Walter Wabe Acuña <walter_wabe@yahoo.com>
15 /// \author Adolfo Di Mare <adolfo@di-mare.com>
16 /// \date 2009
17 ///
18 #endif
19 
20 #ifndef Tree_PLR_h
21 #define Tree_PLR_h
22 
23 #ifdef English_dox
24  /// Doxygen English documentation.
25  #define English_dox "Doxygen English documentation"
26  /// \def English_dox ///< Marks English documentation blocks.
27  /// \def Tree_PLR_h ///< Avoids multiple inclusion.
28 #endif
29 #ifdef Spanish_dox
30  /// Documentaci¢n en espa¤ol.
31  #define Spanish_dox "Documentaci¢n Doxygen en espa¤ol"
32  /// \def Spanish_dox ///< Macro usado para que Doxygen genere documentación.
33  /// \def Tree_PLR_h ///< Evita la inclusión múltiple.
34 #endif
35 
36 #include <list>
37 #include "Tree_L.h"
38 #include <iostream>
39 
40 #ifdef English_dox
41 /// Process-Left-Right iterator.
42 #endif
43 #ifdef Spanish_dox
44 /// Iterador Proceso-Izquierda-Derecha.
45 #endif
46 /**
47  \dontinclude test_iterJava.cpp
48  \skipline test::Tree_PLR()
49  \until }}
50  \see test_iterJava::test_Tree_PLR()
51 
52  \see make_a_o(TL::Tree<char> & T)
53  \dontinclude test_iterJava.cpp
54  \skipline T = a
55  \until +--k
56 */
57 
58 template <typename E>
59 class Tree_PLR {
60  std::list< TL::Tree<E> > m_Q; ///< std::queue<>.
61 public:
62  /// \c init().
63  Tree_PLR( const TL::Tree<E>& T = TL::Tree<E>() )
64  : m_Q() { set(T); }
65  void set( const TL::Tree<E>& T ); ///< \c Iterator::set().
66 
67  bool hasNext() const; ///< \c Iterator::hasNext().
68  const TL::Tree<E> next(); ///< \c Iterator::next().
69 };
70 
71 template <typename E>
72 void Tree_PLR<E>::set( const TL::Tree<E>& T ) {
73  m_Q.clear(); // erase whatever was left
74  if ( T.Empty() ) {
75  return;
76  }
77  m_Q.push_back( T.Root() ); // Push the first element of the Tree
78  TL::Tree<E> Child = m_Q.begin()->Leftmost(); // Creates a Tree
79  while ( ! Child.Empty() ) { // While the processed child is not empty..
80  m_Q.push_back( Child ); // Push the child into the list
81  if ( ! Child.Leftmost().Empty() ) { // If the processed child have a child
82  Child = Child.Leftmost(); // Its child becomes the new child
83  }
84  else { // If the precessed child doesn`t have a child
85  if ( ! Child.Right_Sibling().Empty() ) { // If the processed child have a sibling to the right
86  Child = Child.Right_Sibling(); // That sibling becomes the new child
87  }
88  else { // No sibling to the right
89  TL::Tree<E> Temp = Child; // Use a temporary Tree
90  while ( Temp.Father().Right_Sibling().Empty()
91  &&
92  Temp.Father() != T.Root()
93  ) {
94  Temp = Temp.Father(); // The temporal Tree becomes it`s own father
95  }
96  if ( Temp.Father()==T.Root() ) { // If the temporal Tree equals T.Root()
97  return; // Its the end of the process
98  }
99  else {
100  Child = Temp.Father().Right_Sibling(); // This right Sibling becomes the Child
101  }
102  }
103  }
104  }
105 }
106 
107 /* Indicates if the iterator have a next element */
108 template <typename E>
109 bool Tree_PLR<E>::hasNext() const {
110  return ( ! m_Q.empty() );
111 }
112 
113 /* Moves the iterator to it`s next element */
114 template <typename E>
116  TL::Tree<E> first = m_Q.front();
117  m_Q.pop_front();
118  return first;
119 }
120 
121 #endif // Tree_PLR_h
122 // EOF: Tree_PLR.h
Tree Father() const
Acceso al padre.
Definition: Tree_L.h:355
bool hasNext() const
Iterator::hasNext().
Definition: Tree_PLR.h:109
std::list< TL::Tree< E > > m_Q
std::queue&lt;&gt;.
Definition: Tree_PLR.h:60
Tree Right_Sibling() const
Obtiene el hermano no vacío siguiente, que está hacia la derecha.
Definition: Tree_L.h:521
Tree Leftmost() const
Obtiene el hijo más izquierdo del árbol.
Definition: Tree_L.h:698
Declaraciones y definiciones para la clase Tree.
bool Empty() const
Retorna &quot;true&quot; si el sub-árbol está vacío.
Definition: Tree_L.h:91
Los métodos para trabajar con árboles regresan &quot;referencias&quot; que son sub-árboles. ...
Definition: Tree_L.h:25
void set(const TL::Tree< E > &T)
Iterator::set().
Definition: Tree_PLR.h:72
Process-Left-Right iterator.
Definition: Tree_PLR.h:59
const TL::Tree< E > next()
Iterator::next().
Definition: Tree_PLR.h:115
Tree Root() const
Raíz del sub-árbol.
Definition: Tree_L.h:154