18 #ifndef itkPostOrderTreeIterator_h
19 #define itkPostOrderTreeIterator_h
25 template<
typename TTreeType >
64 template<
typename TTreeType >
68 if ( tree->GetRoot() == nullptr )
74 const auto * root =
dynamic_cast<const TreeNodeType *
>(tree->GetRoot());
77 itkGenericExceptionMacro(<<
"Can't downcast root node to TreeNodeType *");
87 template<
typename TTreeType >
95 template<
typename TTreeType >
99 if ( const_cast< TreeNodeType * >( FindNextNode() ) !=
nullptr )
108 template<
typename TTreeType >
112 this->m_Position =
const_cast< TreeNodeType *
>( FindNextNode() );
113 return this->m_Position->Get();
118 template<
typename TTreeType >
122 if ( this->m_Position ==
nullptr || this->m_Position == this->m_Root )
126 auto * sister =
const_cast< TreeNodeType *
>( FindSister(this->m_Position) );
129 if ( sister !=
nullptr )
131 return FindMostRightLeaf(sister);
133 if(this->m_Position->GetParent() ==
nullptr)
137 auto * rval =
dynamic_cast<TreeNodeType *
>(this->m_Position->GetParent());
140 itkGenericExceptionMacro(<<
"Can't downcast to TreeNodeType *");
146 template<
typename TTreeType >
150 if ( !node->HasParent() )
155 auto * parent =
dynamic_cast<TreeNodeType *
>(node->GetParent());
156 if(parent ==
nullptr)
158 itkGenericExceptionMacro(<<
"Can't downcast to TreeNodeType *");
161 int childPosition = parent->ChildPosition(node);
162 int lastChildPosition = parent->CountChildren() - 1;
164 while ( childPosition < lastChildPosition )
166 if(parent->GetChild(childPosition + 1) ==
nullptr)
172 auto * sister =
dynamic_cast<TreeNodeType *
>(parent->GetChild(childPosition + 1));
173 if ( sister ==
nullptr)
175 itkGenericExceptionMacro(<<
"Can't downcast to TreeNodeType *");
184 template<
typename TTreeType >
188 while ( node->HasChildren() )
191 int childCount = node->CountChildren();
197 if(node->GetChild(i) ==
nullptr)
203 helpNode =
dynamic_cast<TreeNodeType *
>(node->GetChild(i));
204 if(helpNode ==
nullptr)
206 itkGenericExceptionMacro(<<
"Can't downcast to TreeNodeType *");
211 while ( helpNode ==
nullptr && i < childCount );
213 if ( helpNode ==
nullptr )
223 template<
typename TTreeType >
const TreeNodeType * FindSister(TreeNodeType *node) const
bool HasNext() const override
typename TTreeType::ValueType ValueType
const TreeNodeType * FindMostRightLeaf(TreeNodeType *node) const
PostOrderTreeIterator(TreeType *tree)
const TreeNodeType * FindNextNode() const
typename Superclass::NodeType NodeType
NodeType GetType() const override
This class provides the base implementation for tree iterators.
typename Superclass::TreeNodeType TreeNodeType
const ValueType & Next() override
TreeIteratorBase< TTreeType > * Clone() override
typename TTreeType::TreeNodeType TreeNodeType
TreeNodeType * m_Position