Helios++
Helios software for LiDAR simulations
BinaryTreeFastDepthIterator.h
1 #pragma once
2 
3 #include <ITreeIterator.h>
4 #include <IBinaryTreeNode.h>
5 
6 #include <deque>
7 #include <iterator>
8 
9 
10 using std::deque;
11 
46 template <typename NodeType>
47 class BinaryTreeFastDepthIterator : public ITreeIterator<NodeType *>{
48 private:
49  // *** SERIALIZATION *** //
50  // *********************** //
51  friend class boost::serialization::access;
58  template<class Archive>
59  void serialize(Archive &ar, const unsigned int version){
60  boost::serialization::base_object<
63  >();
64  ar &boost::serialization::base_object<
66  >(*this);
67  ar &pendingNodes;
68  }
69 
70 protected:
71  // *** ATTRIBUTES *** //
72  // ******************** //
76  deque<IBinaryTreeNode*> pendingNodes;
77 
78 public:
79  // *** CONSTRUCTION / DESTRUCTION *** //
80  // ************************************ //
92  {start(node);}
93  virtual ~BinaryTreeFastDepthIterator() {}
94 
95  // *** TREE ITERATOR INTERFACE *** //
96  // ********************************* //
103  inline void start(NodeType *node) override{
104  pendingNodes.clear();
105  pendingNodes.push_back(node);
106  }
113  inline bool hasNext() const override {return !pendingNodes.empty();}
119  inline NodeType * next() override {
120  IBinaryTreeNode * node = pendingNodes.back();
121  pendingNodes.pop_back();
122  if(node->getRightChild() != nullptr)
123  pendingNodes.push_back(node->getRightChild());
124  if(node->getLeftChild() != nullptr)
125  pendingNodes.push_back(node->getLeftChild());
126  return (NodeType *) node;
127  }
128 };
Fast depth first iterator for binary tree like nodes.
Definition: BinaryTreeFastDepthIterator.h:47
void start(NodeType *node) override
Start the iterator so the first visited node will be given one. It is, when calling next,...
Definition: BinaryTreeFastDepthIterator.h:103
BinaryTreeFastDepthIterator()=default
Default constructor for binary tree fast depth iterator.
void serialize(Archive &ar, const unsigned int version)
Serialize a BinaryTreeFastDepthIterator to a stream of bytes.
Definition: BinaryTreeFastDepthIterator.h:59
deque< IBinaryTreeNode * > pendingNodes
Double ended queue used as a stack to handle nodes visiting.
Definition: BinaryTreeFastDepthIterator.h:76
BinaryTreeFastDepthIterator(NodeType *node)
Construct a binary tree fast depth iterator calling the start method with given node.
Definition: BinaryTreeFastDepthIterator.h:91
NodeType * next() override
Obtain the next node according to depth iteration criterion.
Definition: BinaryTreeFastDepthIterator.h:119
bool hasNext() const override
Check if the iterator has more nodes to visit (true) or not (false)
Definition: BinaryTreeFastDepthIterator.h:113
Binary tree node interface that must be implemented by any class providing binary tree node based fun...
Definition: IBinaryTreeNode.h:10
virtual IBinaryTreeNode * getLeftChild() const =0
Obtain the left child of current node.
virtual IBinaryTreeNode * getRightChild() const =0
Obtain the right child of current node.
Interface defining core mechanisms that must be provided by any tree iterator.
Definition: ITreeIterator.h:13