ITK  5.1.0
Insight Toolkit
itkInOrderTreeIterator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkInOrderTreeIterator_h
19 #define itkInOrderTreeIterator_h
20 
21 #include "itkTreeIteratorBase.h"
22 
23 namespace itk
24 {
25 template <typename TTreeType>
26 class InOrderTreeIterator : public TreeIteratorBase<TTreeType>
27 {
28 public:
32  using TreeType = TTreeType;
33  using ValueType = typename TTreeType::ValueType;
35  using NodeType = typename Superclass::NodeType;
36 
39  InOrderTreeIterator(TreeType * tree, TreeNodeType * start = nullptr);
41 
43  NodeType
44  GetType() const override;
45 
48  Clone() override;
49 
50 protected:
52  const ValueType &
53  Next() override;
54 
56  bool
57  HasNext() const override;
58 
59 private:
61  const TreeNodeType *
62  FindNextNode() const;
63 };
64 
66 template <typename TTreeType>
68  : TreeIteratorBase<TTreeType>(start)
69 {}
70 
72 template <typename TTreeType>
74  : TreeIteratorBase<TTreeType>(tree, start)
75 {}
76 
78 template <typename TTreeType>
81 {
83 }
84 
86 template <typename TTreeType>
87 bool
89 {
90  if (const_cast<TreeNodeType *>(FindNextNode()) != nullptr)
91  {
92  return true;
93  }
94  return false;
95 }
97 
99 template <typename TTreeType>
102 {
103  this->m_Position = const_cast<TreeNodeType *>(FindNextNode());
104  if (this->m_Position == nullptr)
105  {
106  return this->m_Root->Get(); // value irrelevant, but we have to return something
107  }
108  return this->m_Position->Get();
109 }
111 
113 template <typename TTreeType>
116 {
117  if (this->m_Position == nullptr)
118  {
119  return nullptr;
120  }
121 
122  if (this->m_Position->HasChildren())
123  {
124  return this->m_Position->GetChild(0);
125  }
126 
127  if (!this->m_Position->HasParent())
128  {
129  return nullptr;
130  }
131 
132  TreeNodeType * child = this->m_Position;
133  TreeNodeType * parent = this->m_Position->GetParent();
134 
135  int childPosition = parent->ChildPosition(child);
136  int lastChildPosition = parent->CountChildren() - 1;
137 
138  while (childPosition < lastChildPosition)
139  {
140  TreeNodeType * help = parent->GetChild(childPosition + 1);
141  if (help != nullptr)
142  {
143  return help;
144  }
145  childPosition++;
146  }
147 
148  while (parent->HasParent())
149  {
150  child = parent;
151  parent = parent->GetParent();
152 
153  // Subtree
154  if (parent->ChildPosition(this->m_Root) >= 0)
155  {
156  return nullptr;
157  }
158  childPosition = parent->ChildPosition(child);
159  lastChildPosition = parent->CountChildren() - 1;
160 
161  while (childPosition < lastChildPosition)
162  {
163  TreeNodeType * help = parent->GetChild(childPosition + 1);
164  if (help != nullptr)
165  {
166  return help;
167  }
168  }
169  }
170  return nullptr;
171 }
172 
174 template <typename TTreeType>
177 {
178  auto * clone = new InOrderTreeIterator(const_cast<TTreeType *>(this->m_Tree));
179 
180  *clone = *this;
181  return clone;
182 }
183 } // end namespace itk
184 
185 #endif
itk::InOrderTreeIterator::FindNextNode
const TreeNodeType * FindNextNode() const
Definition: itkInOrderTreeIterator.h:115
itkTreeIteratorBase.h
itk::InOrderTreeIterator::HasNext
bool HasNext() const override
Definition: itkInOrderTreeIterator.h:88
itk::InOrderTreeIterator::TreeType
TTreeType TreeType
Definition: itkInOrderTreeIterator.h:32
itk::TreeIteratorBaseNodeEnum::INORDER
itk::InOrderTreeIterator::GetType
NodeType GetType() const override
Definition: itkInOrderTreeIterator.h:80
itk::TreeIteratorBase
This class provides the base implementation for tree iterators.
Definition: itkTreeIteratorBase.h:58
itk::InOrderTreeIterator::TreeNodeType
typename Superclass::TreeNodeType TreeNodeType
Definition: itkInOrderTreeIterator.h:34
itk::TreeIteratorBase::TreeNodeType
typename TTreeType::TreeNodeType TreeNodeType
Definition: itkTreeIteratorBase.h:64
itk::InOrderTreeIterator
Definition: itkInOrderTreeIterator.h:26
itk::InOrderTreeIterator::ValueType
typename TTreeType::ValueType ValueType
Definition: itkInOrderTreeIterator.h:33
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::InOrderTreeIterator::Clone
TreeIteratorBase< TTreeType > * Clone() override
Definition: itkInOrderTreeIterator.h:176
itk::InOrderTreeIterator::NodeType
typename Superclass::NodeType NodeType
Definition: itkInOrderTreeIterator.h:35
itk::TreeIteratorBase::NodeType
TreeIteratorBaseNodeEnum NodeType
Definition: itkTreeIteratorBase.h:68
itk::InOrderTreeIterator::Next
const ValueType & Next() override
Definition: itkInOrderTreeIterator.h:101
itk::InOrderTreeIterator::InOrderTreeIterator
InOrderTreeIterator(TreeType &start)
Definition: itkInOrderTreeIterator.h:67