ITK  5.1.0
Insight Toolkit
itkPostOrderTreeIterator.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 itkPostOrderTreeIterator_h
19 #define itkPostOrderTreeIterator_h
20 
21 #include "itkTreeIteratorBase.h"
22 
23 namespace itk
24 {
25 template <typename TTreeType>
26 class PostOrderTreeIterator : public TreeIteratorBase<TTreeType>
27 {
28 public:
32  using TreeType = TTreeType;
33  using ValueType = typename TTreeType::ValueType;
35  using NodeType = typename Superclass::NodeType;
36 
39 
41  NodeType
42  GetType() const override;
43 
46  Clone() override;
47 
48 protected:
50  const ValueType &
51  Next() override;
52 
54  bool
55  HasNext() const override;
56 
57 protected:
58  const TreeNodeType *
59  FindNextNode() const;
60 
61  const TreeNodeType *
62  FindMostRightLeaf(TreeNodeType * node) const;
63 
64  const TreeNodeType *
65  FindSister(TreeNodeType * node) const;
66 };
67 
69 template <typename TTreeType>
71  : TreeIteratorBase<TTreeType>(tree, nullptr)
72 {
73  if (tree->GetRoot() == nullptr)
74  {
75  this->m_Begin = nullptr;
76  }
77  else
78  {
79  const auto * root = dynamic_cast<const TreeNodeType *>(tree->GetRoot());
80  if (root == nullptr)
81  {
82  itkGenericExceptionMacro(<< "Can't downcast root node to TreeNodeType *");
83  }
84  this->m_Position = const_cast<TreeNodeType *>(root);
85  this->m_Position = const_cast<TreeNodeType *>(FindMostRightLeaf(this->m_Position));
86  this->m_Begin = this->m_Position;
87  }
88 }
90 
92 template <typename TTreeType>
95 {
97 }
98 
100 template <typename TTreeType>
101 bool
103 {
104  if (const_cast<TreeNodeType *>(FindNextNode()) != nullptr)
105  {
106  return true;
107  }
108  return false;
109 }
111 
113 template <typename TTreeType>
116 {
117  this->m_Position = const_cast<TreeNodeType *>(FindNextNode());
118  return this->m_Position->Get();
119 }
121 
123 template <typename TTreeType>
126 {
127  if (this->m_Position == nullptr || this->m_Position == this->m_Root)
128  {
129  return nullptr;
130  }
131  auto * sister = const_cast<TreeNodeType *>(FindSister(this->m_Position));
133 
134  if (sister != nullptr)
135  {
136  return FindMostRightLeaf(sister);
137  }
138  if (this->m_Position->GetParent() == nullptr)
139  {
140  return nullptr;
141  }
142  auto * rval = dynamic_cast<TreeNodeType *>(this->m_Position->GetParent());
143  if (rval == nullptr)
144  {
145  itkGenericExceptionMacro(<< "Can't downcast to TreeNodeType *");
146  }
147  return rval;
148 }
149 
151 template <typename TTreeType>
154 {
155  if (!node->HasParent())
156  {
157  return nullptr;
158  }
159 
160  auto * parent = dynamic_cast<TreeNodeType *>(node->GetParent());
161  if (parent == nullptr)
162  {
163  itkGenericExceptionMacro(<< "Can't downcast to TreeNodeType *");
164  }
165 
166  int childPosition = parent->ChildPosition(node);
167  int lastChildPosition = parent->CountChildren() - 1;
168 
169  while (childPosition < lastChildPosition)
170  {
171  if (parent->GetChild(childPosition + 1) == nullptr)
172  {
173  childPosition++;
174  }
175  else
176  {
177  auto * sister = dynamic_cast<TreeNodeType *>(parent->GetChild(childPosition + 1));
178  if (sister == nullptr)
179  {
180  itkGenericExceptionMacro(<< "Can't downcast to TreeNodeType *");
181  }
182  return sister;
183  }
184  }
185  return nullptr;
186 }
187 
189 template <typename TTreeType>
192 {
193  while (node->HasChildren())
194  {
195  TreeNodeType * helpNode;
196  int childCount = node->CountChildren();
197  int i = 0;
199 
200  do
201  {
202  if (node->GetChild(i) == nullptr)
203  {
204  helpNode = nullptr;
205  }
206  else
207  {
208  helpNode = dynamic_cast<TreeNodeType *>(node->GetChild(i));
209  if (helpNode == nullptr)
210  {
211  itkGenericExceptionMacro(<< "Can't downcast to TreeNodeType *");
212  }
213  }
214  i++;
215  } while (helpNode == nullptr && i < childCount);
216 
217  if (helpNode == nullptr)
218  {
219  return node;
220  }
221  node = helpNode;
222  }
223  return node;
224 }
225 
227 template <typename TTreeType>
230 {
231  auto * clone = new PostOrderTreeIterator<TTreeType>(const_cast<TTreeType *>(this->m_Tree));
232  *clone = *this;
233  return clone;
234 }
235 } // end namespace itk
237 
238 #endif
itk::PostOrderTreeIterator::FindNextNode
const TreeNodeType * FindNextNode() const
Definition: itkPostOrderTreeIterator.h:125
itkTreeIteratorBase.h
itk::PostOrderTreeIterator
Definition: itkPostOrderTreeIterator.h:26
itk::TreeIteratorBase::m_Begin
TreeNodeType * m_Begin
Definition: itkTreeIteratorBase.h:264
itk::PostOrderTreeIterator::ValueType
typename TTreeType::ValueType ValueType
Definition: itkPostOrderTreeIterator.h:33
itk::TreeIteratorBaseNodeEnum::POSTORDER
itk::PostOrderTreeIterator::NodeType
typename Superclass::NodeType NodeType
Definition: itkPostOrderTreeIterator.h:35
itk::PostOrderTreeIterator::GetType
NodeType GetType() const override
Definition: itkPostOrderTreeIterator.h:94
itk::PostOrderTreeIterator::TreeNodeType
typename Superclass::TreeNodeType TreeNodeType
Definition: itkPostOrderTreeIterator.h:34
itk::PostOrderTreeIterator::Next
const ValueType & Next() override
Definition: itkPostOrderTreeIterator.h:115
itk::TreeIteratorBase
This class provides the base implementation for tree iterators.
Definition: itkTreeIteratorBase.h:58
itk::PostOrderTreeIterator::FindSister
const TreeNodeType * FindSister(TreeNodeType *node) const
Definition: itkPostOrderTreeIterator.h:153
itk::PostOrderTreeIterator::Clone
TreeIteratorBase< TTreeType > * Clone() override
Definition: itkPostOrderTreeIterator.h:229
itk::TreeIteratorBase::TreeNodeType
typename TTreeType::TreeNodeType TreeNodeType
Definition: itkTreeIteratorBase.h:64
itk::PostOrderTreeIterator::HasNext
bool HasNext() const override
Definition: itkPostOrderTreeIterator.h:102
itk::PostOrderTreeIterator::TreeType
TTreeType TreeType
Definition: itkPostOrderTreeIterator.h:32
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::TreeIteratorBase::m_Position
TreeNodeType * m_Position
Definition: itkTreeIteratorBase.h:263
itk::PostOrderTreeIterator::PostOrderTreeIterator
PostOrderTreeIterator(TreeType *tree)
Definition: itkPostOrderTreeIterator.h:70
itk::PostOrderTreeIterator::FindMostRightLeaf
const TreeNodeType * FindMostRightLeaf(TreeNodeType *node) const
Definition: itkPostOrderTreeIterator.h:191
itk::TreeIteratorBase::NodeType
TreeIteratorBaseNodeEnum NodeType
Definition: itkTreeIteratorBase.h:68