Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkInOrderTreeIterator.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkInOrderTreeIterator.h,v $
00005   Language:  C++
00006   Date:      $Date: 2004/12/11 20:29:18 $
00007   Version:   $Revision: 1.2 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkInOrderTreeIterator_h
00018 #define __itkInOrderTreeIterator_h
00019 
00020 #include <itkTreeIteratorBase.h>
00021 
00022 namespace itk{
00023 
00024 template <class TTreeType>
00025 class InOrderTreeIterator : public TreeIteratorBase<TTreeType> 
00026 {
00027 public:
00028 
00030   typedef TreeIteratorBase<TTreeType>  Superclass;
00031   typedef TTreeType TreeType;
00032   typedef typename TTreeType::ValueType ValueType;
00033   typedef typename Superclass::TreeNodeType TreeNodeType;
00034 
00036   InOrderTreeIterator( TreeType& start );
00037   InOrderTreeIterator( TreeType* tree, TreeNodeType* start=NULL);
00038 
00040   int GetType() const;
00041   
00043   TreeIteratorBase<TTreeType>* Clone();
00044 
00045 protected:
00046 
00048   const ValueType& Next();
00049 
00051   bool HasNext() const;
00052 
00053 private:
00054 
00056   const TreeNodeType* FindNextNode() const;
00057 
00058 };
00059 
00060 
00062 template <class TTreeType>
00063 InOrderTreeIterator<TTreeType>::InOrderTreeIterator( TTreeType& start )
00064     :TreeIteratorBase<TTreeType>(start) 
00065 {
00066 }
00067 
00068 
00070 template <class TTreeType>
00071 InOrderTreeIterator<TTreeType>::InOrderTreeIterator( TTreeType* tree, TreeNodeType* start)
00072     :TreeIteratorBase<TTreeType>(tree,start)
00073 {
00074 }
00075 
00077 template <class TTreeType>
00078 int 
00079 InOrderTreeIterator<TTreeType>::GetType() const 
00080 {
00081   return TreeIteratorBase<TTreeType>::INORDER;
00082 }
00083 
00084 
00086 template <class TTreeType>
00087 bool InOrderTreeIterator<TTreeType>::HasNext() const
00088 {
00089   if ( const_cast<TreeNodeType*>(FindNextNode()) != NULL )
00090     {
00091     return true;
00092     }
00093   return false;
00094 }
00095 
00096 
00098 template <class TTreeType>
00099 const typename InOrderTreeIterator<TTreeType>::ValueType&
00100 InOrderTreeIterator<TTreeType>::Next() 
00101 {
00102   this->m_Position =  const_cast<TreeNodeType* >(FindNextNode());
00103   return this->m_Position->Get();
00104 }
00105 
00106 
00108 template <class TTreeType>
00109 const typename InOrderTreeIterator<TTreeType>::TreeNodeType* 
00110 InOrderTreeIterator<TTreeType>::FindNextNode() const 
00111 {
00112   if ( this->m_Position == NULL )
00113     {
00114     return NULL;
00115     }
00116   
00117   if ( this->m_Position->HasChildren() )
00118     {
00119     return this->m_Position->GetChild(0);
00120     }
00121     
00122   if ( !this->m_Position->HasParent() )
00123     {
00124     return NULL;
00125     }
00126   
00127   TreeNodeType* child = this->m_Position;
00128   TreeNodeType* parent = this->m_Position->GetParent();
00129 
00130   int ChildPosition = parent->ChildPosition( child );
00131   int lastChildPosition = parent->CountChildren() - 1;
00132 
00133   while ( ChildPosition < lastChildPosition ) 
00134     {
00135     TreeNodeType* help = parent->GetChild( ChildPosition + 1 );
00136     if ( help != NULL )
00137       {
00138       return help;
00139       }
00140     ChildPosition++;
00141     }
00142 
00143   while ( parent->HasParent() )
00144     {
00145     child = parent;
00146     parent = parent->GetParent();
00147 
00148     // Subtree
00149     if( parent->ChildPosition( this->m_Root ) >= 0 )
00150       {
00151       return NULL;
00152       }
00153     ChildPosition = parent->ChildPosition(child);
00154     lastChildPosition = parent->CountChildren() - 1;
00155 
00156     while ( ChildPosition < lastChildPosition ) 
00157       {
00158       TreeNodeType* help = parent->GetChild( ChildPosition + 1 );
00159       if ( help != NULL )
00160         {
00161         return help;
00162         }
00163       }
00164     }
00165   return NULL;
00166 }
00167 
00169 template <class TTreeType>
00170 TreeIteratorBase<TTreeType>* InOrderTreeIterator<TTreeType>::Clone() 
00171 {
00172   InOrderTreeIterator* clone = new InOrderTreeIterator( const_cast<TTreeType*>(this->m_Tree) );
00173   *clone = *this;
00174   return clone;
00175 }
00176 
00177 } // end namespace itk
00178 
00179 #endif

Generated at Wed May 24 23:27:58 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000