KWStyle - itkInOrderTreeIterator.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkInOrderTreeIterator.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:40 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 #ifndef __itkInOrderTreeIterator_h
18 #define __itkInOrderTreeIterator_h
19
20 #include <itkTreeIteratorBase.h>
21
22 NMS namespace itk{
23
24 template <class TTreeType>
25 class InOrderTreeIterator : public TreeIteratorBase<TTreeType> 
26 {
27 public:
28
29   /** Typedefs */
30   typedef TreeIteratorBase<TTreeType>  Superclass;
31 TDA   typedef TTreeType TreeType;
32 TDA   typedef typename TTreeType::ValueType ValueType;
33 TDA   typedef typename Superclass::TreeNodeType TreeNodeType;
34
35   /** Constructors */
36   InOrderTreeIterator( TreeType& start );
37   InOrderTreeIterator( TreeType* tree, TreeNodeType* start=NULL);
38
39   /** Get the type of iterator */
40   int GetType() const;
41   
42   /** Clone function */
43   TreeIteratorBase<TTreeType>* Clone();
44
45 protected:
46
47   /** Return the next node */
48   const ValueType& Next();
49
50   /** Return true if the next node exists */
51   bool HasNext() const;
52
53 private:
54
55   /** Find the next node */
56   const TreeNodeType* FindNextNode() const;
57
58 };
59
60
61 /** Constructor */
62 template <class TTreeType>
63 InOrderTreeIterator<TTreeType>::InOrderTreeIterator( TTreeType& start )
64 IND ****:TreeIteratorBase<TTreeType>(start) 
65 {
66 }
67
68
69 /** Constructor */
70 template <class TTreeType>
71 LEN InOrderTreeIterator<TTreeType>::InOrderTreeIterator( TTreeType* tree, TreeNodeType* start)
72 IND ****:TreeIteratorBase<TTreeType>(tree,start)
73 {
74 }
75
76 /** Get the type of the iterator */
77 template <class TTreeType>
78 int 
79 InOrderTreeIterator<TTreeType>::GetType() const 
80 {
81   return TreeIteratorBase<TTreeType>::INORDER;
82 }
83
84
85 /** Return true if the next node exists */
86 template <class TTreeType>
87 bool InOrderTreeIterator<TTreeType>::HasNext() const
88 {
89   if ( const_cast<TreeNodeType*>(FindNextNode()) != NULL )
90     {
91     return true;
92     }
93   return false;
94 }
95
96
97 /** Return the next node */
98 template <class TTreeType>
99 const typename InOrderTreeIterator<TTreeType>::ValueType&
100 InOrderTreeIterator<TTreeType>::Next() 
101 {
102   this->m_Position =  const_cast<TreeNodeType* >(FindNextNode());
103   return this->m_Position->Get();
104 }
105
106
107 /** Find the next node */
108 template <class TTreeType>
109 const typename InOrderTreeIterator<TTreeType>::TreeNodeType* 
110 InOrderTreeIterator<TTreeType>::FindNextNode() const 
111 {
112   if ( this->m_Position == NULL )
113     {
114     return NULL;
115     }
116   
117   if ( this->m_Position->HasChildren() )
118     {
119     return this->m_Position->GetChild(0);
120     }
121     
122   if ( !this->m_Position->HasParent() )
123     {
124     return NULL;
125     }
126   
127   TreeNodeType* child = this->m_Position;
128   TreeNodeType* parent = this->m_Position->GetParent();
129
130   int ChildPosition = parent->ChildPosition( child );
131   int lastChildPosition = parent->CountChildren() - 1;
132
133   while ( ChildPosition < lastChildPosition ) 
134     {
135     TreeNodeType* help = parent->GetChild( ChildPosition + 1 );
136     if ( help != NULL )
137       {
138       return help;
139       }
140     ChildPosition++;
141     }
142
143   while ( parent->HasParent() )
144     {
145     child = parent;
146     parent = parent->GetParent();
147
148     // Subtree
149     if( parent->ChildPosition( this->m_Root ) >= 0 )
150       {
151       return NULL;
152       }
153     ChildPosition = parent->ChildPosition(child);
154     lastChildPosition = parent->CountChildren() - 1;
155
156     while ( ChildPosition < lastChildPosition ) 
157       {
158       TreeNodeType* help = parent->GetChild( ChildPosition + 1 );
159       if ( help != NULL )
160         {
161         return help;
162         }
163       }
164     }
165   return NULL;
166 }
167
168 /** Clone function */
169 template <class TTreeType>
170 TreeIteratorBase<TTreeType>* InOrderTreeIterator<TTreeType>::Clone() 
171 {
172 LEN   InOrderTreeIterator* clone = new InOrderTreeIterator( const_cast<TTreeType*>(this->m_Tree) );
173   *clone = *this;
174   return clone;
175 }
176
177 // end namespace itk
178
179 #endif
180

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:17PM
© Kitware Inc.