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
32using namespace std;
33namespace bpp
34{
35class TreeIterator // abstract class from which each iterator type inherits
36{
37protected:
38 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.
39 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.
40 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
41 map<int, bool> nodeToSonVisited_; // a map that matches to each node id if a son of his was visited or not
42 map<int, size_t> nodeToLastVisitedSonIndex_; // a map that matches to each node id the index of its last visited son visited son
43
44public:
45 /* constructors and destructors */
46 explicit TreeIterator(const TreeTemplate<Node>& tree) :
47 tree_(tree),
48 currNode_(tree.getRootNode()), // The pointer to the initial node is initialized as 0 since it's actual assignment depends on which traversal is chosen
52 { init(); }
53
54 explicit TreeIterator(const TreeIterator& tree_iterator) :
55 tree_(tree_iterator.tree_),
56 currNode_(tree_.getRootNode()),
60 {}
61
62 TreeIterator& operator=(const TreeIterator& tree_iterator);
63
64 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/)
65
66 void init();
67 // function to initialize nodes properties
68 /* iterating functions */
69 const Node* begin();
70 virtual const Node* next() = 0; // Set as virtual because the function should be implemented separately in each iterator type
72 const Node* end(){ return NULL; }
73};
74
75
77{
78public:
79 /* constructors and destructors */
81 TreeIterator(tree)
82 {
83 currNode_ = tree_.getNodes()[0]; // Get the leftmost leaf of the tree
84 }
85
86 ~PostOrderTreeIterator() {} // Inherited from TreeIterator
87
88 const Node* getLeftMostPredecessor(const Node* startNode);
89 const Node* next();
90};
91
92
94{
95public:
96 /* constructors and destructors */
98 TreeIterator(tree)
99 {
100 currNode_ = tree_.getRootNode();
101 }
102
103 ~PreOrderTreeIterator() {} // Inherited from TreeIterator
104
105 const Node* next();
106};
107
108
110{
111public:
112 /* constructors and destructors */
114 TreeIterator(tree)
115 {
116 currNode_ = tree_.getNodes()[0]; // Get the leftmost leaf of the tree
117 }
118
119 ~InOrderTreeIterator() {} // Inherited from TreeIterator
120
121 const Node* doStep(const Node* node);
122 const Node* next();
123};
124} // end of namespace bpp.
125#endif // BPP_PHYL_LEGACY_TREEITERATOR_H
const Node * doStep(const Node *node)
InOrderTreeIterator(const TreeTemplate< Node > &tree)
Definition: TreeIterator.h:113
The phylogenetic node class.
Definition: Node.h:59
const Node * getLeftMostPredecessor(const Node *startNode)
PostOrderTreeIterator(const TreeTemplate< Node > &tree)
Definition: TreeIterator.h:80
PreOrderTreeIterator(const TreeTemplate< Node > &tree)
Definition: TreeIterator.h:97
TreeIterator & operator++()
const Node * end()
Definition: TreeIterator.h:72
const TreeTemplate< Node > & tree_
Definition: TreeIterator.h:38
const Node * begin()
virtual ~TreeIterator()
Definition: TreeIterator.h:64
TreeIterator & operator=(const TreeIterator &tree_iterator)
map< int, bool > nodeToVisited_
Definition: TreeIterator.h:40
map< int, size_t > nodeToLastVisitedSonIndex_
Definition: TreeIterator.h:42
virtual const Node * next()=0
TreeIterator(const TreeIterator &tree_iterator)
Definition: TreeIterator.h:54
const Node * currNode_
Definition: TreeIterator.h:39
TreeIterator(const TreeTemplate< Node > &tree)
Definition: TreeIterator.h:46
map< int, bool > nodeToSonVisited_
Definition: TreeIterator.h:41
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Defines the basic types of data flow nodes.