bpp-phyl3  3.0.0
TreeIterator.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_LEGACY_TREEITERATOR_H
6 #define BPP_PHYL_LEGACY_TREEITERATOR_H
7 
8 
9 #include "Node.h"
10 #include "TreeTemplate.h"
11 
12 // From the STL:
13 #include <string>
14 #include <vector>
15 #include <map>
16 
33 using namespace std;
34 namespace bpp
35 {
36 class TreeIterator // abstract class from which each iterator type inherits
37 {
38 protected:
39  const TreeTemplate<Node>& tree_; // The tree to iterate over its nodes. Can't be const because user should be allowed to edit the nodes of the tree during the traversal.
40  const Node* currNode_; // A pointer to the current node visited by the iterator. Can't be const because user should be allowed to edit the nodes of the tree during the traversal.
41  map<int, bool> nodeToVisited_; // a map that matches to each node id of booleans that for each node id states if it is visited or not
42  map<int, bool> nodeToSonVisited_; // a map that matches to each node id if a son of his was visited or not
43  map<int, size_t> nodeToLastVisitedSonIndex_; // a map that matches to each node id the index of its last visited son visited son
44 
45 public:
46  /* constructors and destructors */
47  explicit TreeIterator(const TreeTemplate<Node>& tree) :
48  tree_(tree),
49  currNode_(tree.getRootNode()), // The pointer to the initial node is initialized as 0 since it's actual assignment depends on which traversal is chosen
50  nodeToVisited_(),
51  nodeToSonVisited_(),
52  nodeToLastVisitedSonIndex_()
53  { init(); }
54 
55  explicit TreeIterator(const TreeIterator& tree_iterator) :
56  tree_(tree_iterator.tree_),
57  currNode_(tree_.getRootNode()),
58  nodeToVisited_(),
59  nodeToSonVisited_(),
60  nodeToLastVisitedSonIndex_()
61  {}
62 
63  TreeIterator& operator=(const TreeIterator& tree_iterator);
64 
65  virtual ~TreeIterator() {} // must be virtual to assume that upon deletion, the destructor of any inheriting class is called as well (see https://www.geeksforgeeks.org/virtual-destructor/)
66 
67  void init();
68  // function to initialize nodes properties
69  /* iterating functions */
70  const Node* begin();
71  virtual const Node* next() = 0; // Set as virtual because the function should be implemented separately in each iterator type
72  TreeIterator& operator++();
73  const Node* end(){ return NULL; }
74 };
75 
76 
78 {
79 public:
80  /* constructors and destructors */
81  explicit PostOrderTreeIterator(const TreeTemplate<Node>& tree) :
82  TreeIterator(tree)
83  {
84  currNode_ = tree_.getNodes()[0]; // Get the leftmost leaf of the tree
85  }
86 
87  ~PostOrderTreeIterator() {} // Inherited from TreeIterator
88 
89  const Node* getLeftMostPredecessor(const Node* startNode);
90  const Node* next();
91 };
92 
93 
95 {
96 public:
97  /* constructors and destructors */
98  explicit PreOrderTreeIterator(const TreeTemplate<Node>& tree) :
99  TreeIterator(tree)
100  {
101  currNode_ = tree_.getRootNode();
102  }
103 
104  ~PreOrderTreeIterator() {} // Inherited from TreeIterator
105 
106  const Node* next();
107 };
108 
109 
111 {
112 public:
113  /* constructors and destructors */
114  explicit InOrderTreeIterator(const TreeTemplate<Node>& tree) :
115  TreeIterator(tree)
116  {
117  currNode_ = tree_.getNodes()[0]; // Get the leftmost leaf of the tree
118  }
119 
120  ~InOrderTreeIterator() {} // Inherited from TreeIterator
121 
122  const Node* doStep(const Node* node);
123  const Node* next();
124 };
125 } // end of namespace bpp.
126 #endif // BPP_PHYL_LEGACY_TREEITERATOR_H
InOrderTreeIterator(const TreeTemplate< Node > &tree)
Definition: TreeIterator.h:114
The phylogenetic node class.
Definition: Node.h:59
PostOrderTreeIterator(const TreeTemplate< Node > &tree)
Definition: TreeIterator.h:81
PreOrderTreeIterator(const TreeTemplate< Node > &tree)
Definition: TreeIterator.h:98
const Node * end()
Definition: TreeIterator.h:73
const TreeTemplate< Node > & tree_
Definition: TreeIterator.h:39
virtual ~TreeIterator()
Definition: TreeIterator.h:65
map< int, bool > nodeToVisited_
Definition: TreeIterator.h:41
virtual const Node * next()=0
map< int, size_t > nodeToLastVisitedSonIndex_
Definition: TreeIterator.h:43
TreeIterator(const TreeIterator &tree_iterator)
Definition: TreeIterator.h:55
const Node * currNode_
Definition: TreeIterator.h:40
TreeIterator & operator=(const TreeIterator &tree_iterator)
TreeIterator(const TreeTemplate< Node > &tree)
Definition: TreeIterator.h:47
map< int, bool > nodeToSonVisited_
Definition: TreeIterator.h:42
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Defines the basic types of data flow nodes.