Helios++
Helios software for LiDAR simulations
BinaryTreeFastBreadthIterator.h
1 #pragma once
2 
3 #include <ITreeIterator.h>
4 #include <IBinaryTreeNode.h>
5 
6 #include <deque>
7 #include <iterator>
8 
42 template <typename NodeType>
43 class BinaryTreeFastBreadthIterator : public ITreeIterator<NodeType *>{
44 private:
45  // *** SERIALIZATION *** //
46  // *********************** //
47  friend class boost::serialization::access;
54  template <class Archive>
55  void serialize(Archive &ar, unsigned int const version){
56  boost::serialization::base_object<
59  >();
60  ar &boost::serialization::base_object<
62  >(*this);
63  ar &pendingNodes;
64  }
65 
66 protected:
67  // *** ATTRIBUTES *** //
68  // ******************** //
72  deque<IBinaryTreeNode*> pendingNodes;
73 
74 public:
75  // *** CONSTRUCTION / DESTRUCTION *** //
76  // ************************************ //
89  {start(node);}
90  virtual ~BinaryTreeFastBreadthIterator() {}
91 
92  // *** TREE ITERATOR INTERFACE *** //
93  // ********************************* //
100  inline void start(NodeType *node) override{
101  pendingNodes.clear();
102  pendingNodes.push_back(node);
103  }
110  inline bool hasNext() const override {return !pendingNodes.empty();}
116  inline NodeType * next() override{
117  IBinaryTreeNode * node = pendingNodes.front();
118  pendingNodes.pop_front();
119  if(node->getLeftChild() != nullptr)
120  pendingNodes.push_back(node->getLeftChild());
121  if(node->getRightChild() != nullptr)
122  pendingNodes.push_back(node->getRightChild());
123  return (NodeType *) node;
124  }
125 };
Fast breadth first iterator for binary tree like nodes.
Definition: BinaryTreeFastBreadthIterator.h:43
deque< IBinaryTreeNode * > pendingNodes
Double ended queue used as a queue to handles nodes visiting.
Definition: BinaryTreeFastBreadthIterator.h:72
BinaryTreeFastBreadthIterator(NodeType *node)
Construct a binary tree fast breadth iterator calling the start method with given node.
Definition: BinaryTreeFastBreadthIterator.h:87
void start(NodeType *node) override
Start the iterator so the first visited node will be given one. It is, when calling next,...
Definition: BinaryTreeFastBreadthIterator.h:100
void serialize(Archive &ar, unsigned int const version)
Serialize a BinaryTreeFastBreadthIterator to a stream of bytes.
Definition: BinaryTreeFastBreadthIterator.h:55
NodeType * next() override
Obtain the next node according to breadth iteration criterion.
Definition: BinaryTreeFastBreadthIterator.h:116
BinaryTreeFastBreadthIterator()=default
Default constructor for binary tree fast breadth iterator.
bool hasNext() const override
Check if the iterator has more nodes to visit (true) or not (false)
Definition: BinaryTreeFastBreadthIterator.h:110
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