Helios++
Helios software for LiDAR simulations
BinaryTreeBreadthIterator.h
1 #pragma once
2 
3 #include <IBinaryTreeNode.h>
4 #include <IterableTreeNode.h>
5 #include <ITreeIterator.h>
6 
7 #include <deque>
8 #include <iterator>
9 
10 
11 using std::deque;
12 
27 template <typename NodeType>
29  public ITreeIterator<IterableTreeNode<IBinaryTreeNode>>
30 {
31 private:
32  // *** SERIALIZATION *** //
33  // *********************** //
34  friend class boost::serialization::access;
41  template <class Archive>
42  void serialize(Archive &ar, unsigned int const version){
43  boost::serialization::base_object<
46  >();
47  ar &boost::serialization::base_object<
49  >(*this);
50  ar &pendingNodes;
51  }
52 
53 protected:
54  // *** ATTRIBUTES *** //
55  // ******************** //
59  deque<IterableTreeNode<IBinaryTreeNode>> pendingNodes;
60 
61 public:
62  // *** CONSTRUCTION / DESTRUCTION *** //
63  // ************************************ //
75  BinaryTreeBreadthIterator(NodeType *node, int const depth=0) :
77  {start(node, depth);}
86  {start(node);}
87  virtual ~BinaryTreeBreadthIterator() {}
88 
89  // *** TREE ITERATOR INTERFACE *** //
90  // ********************************* //
95  inline void start(NodeType *node, int const depth=0){
97  }
104  inline void start(IterableTreeNode<IBinaryTreeNode> node) override{
105  pendingNodes.clear();
106  pendingNodes.push_back(node);
107  }
114  inline bool hasNext() const override {return !pendingNodes.empty();}
122  pendingNodes.pop_front();
123  if(node.getNode()->getLeftChild() != nullptr)
124  pendingNodes.emplace_back(
125  node.getNode()->getLeftChild(),
126  node.getDepth()+1
127  );
128  if(node.getNode()->getRightChild() != nullptr)
129  pendingNodes.emplace_back(
130  node.getNode()->getRightChild(),
131  node.getDepth()+1
132  );
133  return node;
134  }
135 };
Like fast breadth first iterator but wrapping tree nodes inside a IterableTreeNode instance.
Definition: BinaryTreeBreadthIterator.h:30
void start(IterableTreeNode< IBinaryTreeNode > node) override
Start the iterator so the first visited node will be given one. It is, when calling next,...
Definition: BinaryTreeBreadthIterator.h:104
void start(NodeType *node, int const depth=0)
Definition: BinaryTreeBreadthIterator.h:95
IterableTreeNode< IBinaryTreeNode > next() override
Obtain the next node according to breadth iteration criterion.
Definition: BinaryTreeBreadthIterator.h:120
bool hasNext() const override
Check if the iterator has more nodes to visit (true) or not (false)
Definition: BinaryTreeBreadthIterator.h:114
deque< IterableTreeNode< IBinaryTreeNode > > pendingNodes
Double ended queue used as a queue to handle nodes visiting.
Definition: BinaryTreeBreadthIterator.h:59
void serialize(Archive &ar, unsigned int const version)
Serialize a BinaryTreeBreadthIterator to a stream of bytes.
Definition: BinaryTreeBreadthIterator.h:42
BinaryTreeBreadthIterator(NodeType *node, int const depth=0)
Construct a binary tree breadth iterator calling the start method with given node.
Definition: BinaryTreeBreadthIterator.h:75
BinaryTreeBreadthIterator(IterableTreeNode< IBinaryTreeNode > node)
Construct a binary tree breadth iterator calling the start method with given iterable tree node.
Definition: BinaryTreeBreadthIterator.h:84
BinaryTreeBreadthIterator()=default
Default constructor for binary tree breadth iterator.
Interface defining core mechanisms that must be provided by any tree iterator.
Definition: ITreeIterator.h:13
Class representing an iterable tree node. It is a wrapper for a given tree node type which handles so...
Definition: IterableTreeNode.h:12
NodeType * getNode() const
Obtain the tree node being wrapped.
Definition: IterableTreeNode.h:63
int getDepth() const
Obtain the depth of wrapped tree node.
Definition: IterableTreeNode.h:84