KWStyle - itkTreeContainer.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkTreeContainer.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:48 $
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 DEF #ifndef _itkTreeContainer_txx
18 DEF #define _itkTreeContainer_txx
19
20 #include "itkTreeContainer.h"
21
22 namespace itk
23 {
24
25 /** Constructor */
26 template <class TValueType>
27 TreeContainer<TValueType>::TreeContainer()
28 {
29   m_Root = NULL;
30   this->SetSubtree( false );
31   m_DefaultChildrenCount = 2;
32 }
33
34 /** Constructor with default children count */
35 template <class TValueType>
36 TreeContainer<TValueType>::TreeContainer(int dcc)
37 {  
38   m_Root = NULL;
39   this->SetSubtree( false );
40   m_DefaultChildrenCount = dcc;
41 }
42
43
44 /** Constructor by adding a tree */
45 template <class TValueType>
46 TreeContainer<TValueType>::TreeContainer(TreeContainer<TValueType>& tree)
47 {
48   m_Root = NULL;
49   this->SetSubtree( false );
50   m_DefaultChildrenCount = 3;
51 }
52
53
54 /** Destructor */
55 template <class TValueType>
56 TreeContainer<TValueType>::~TreeContainer() 
57 {
58 }
59
60 /** Set the root of the tree */
61 template <class TValueType>
62 bool 
63 TreeContainer<TValueType>::SetRoot( TValueType element)  
64 {
65   if (m_Root)
66     {
67     return false;
68     }
69   m_Root = TreeNodeType::New();
70   m_Root->Set(element);
71   m_Root->SetParent(NULL);
72   return true;
73 }
74
75 /** Set the root of the tree */
76 template <class TValueType>
77 bool 
78 TreeContainer<TValueType>::SetRoot( TreeNode<TValueType>* node)
79 {
80   if (m_Root)
81     {
82     return false;
83     }
84   m_Root = node;
85   return true;
86 }
87
88 /** Count the number of nodes in the tree */
89 template <class TValueType>
90 int 
91 TreeContainer<TValueType>::Count() const
92 {
93   if(!m_Root)
94     {
95     return 0;
96     }
97   int size = 0;
98   PreOrderTreeIterator<Self> it(this,m_Root);
99   it.GoToBegin();
100   while(!it.IsAtEnd())
101     {
102     size++;
103     ++it;
104     }
105   return size;
106 IND return 0;
107 }
108
109 /** Swap the iterators */
110 template <class TValueType>
111 bool 
112 TreeContainer<TValueType>::Swap( IteratorType& v, IteratorType& w ) 
113 {
114   TreeNode<TValueType>* nv = v.GetNode();
115   TreeNode<TValueType>* nw = w.GetNode();
116
117   if ( nv == NULL || nw == NULL )
118     {
119     return false;
120     }
121   TreeNode<TValueType> *pv = nv->GetParent();
122   TreeNode<TValueType> *pw = nw->GetParent();
123
124   if ( pv == NULL && pw == NULL )
125     {
126     return false;
127     }
128   else if ( pv == NULL )
129     {
130     pw->ReplaceChild(nw, nv);
131     m_Root = nw;
132     }
133   else if( pw == NULL ) 
134     {
135     pv->ReplaceChild(nv, nw);
136     m_Root = nv;
137     }
138   else 
139     {
140     pv->ReplaceChild(nv, nw);
141     pw->ReplaceChild(nw, nv);
142     }
143
144   nv->SetParent( pw );
145   nw->SetParent( pv );
146
147   return true;
148 }
149
150 /** Return true if the tree contains this element */
151 template <class TValueType>
152 bool 
153 TreeContainer<TValueType>::Contains( const TValueType element ) 
154
155   PreOrderTreeIterator<Self> it(this,m_Root);
156   it.GoToBegin();
157   while(!it.IsAtEnd())
158     {
159     if(it.Get()== element)
160       {
161       return true;
162       }
163     ++it;
164     }
165   return false;
166 }
167
168 /** Equal operator */
169 template <class TValueType>
170 bool 
171 TreeContainer<TValueType>::operator==( TreeContainer<TValueType>& tree ) 
172 {
173   PreOrderTreeIterator<Self> it(this,m_Root);
174   it.GoToBegin();
175   PreOrderTreeIterator<Self> it2(&tree,tree.GetRoot());
176   it2.GoToBegin();
177    
178   while((!it.IsAtEnd()) && (!it2.IsAtEnd()))
179     {
180     if(it.Get() != it2.Get())
181       {
182       return false;
183       }
184     ++it;
185     ++it2;
186     }
187
188   return true;
189 }
190
191 /** Return true if the given element is a leaf of the tree */
192 template <class TValueType>
193 bool 
194 TreeContainer<TValueType>::IsLeaf( TValueType element ) 
195 {
196   PreOrderTreeIterator<Self> it(this,m_Root);
197   it.GoToBegin();
198   while(!it.IsAtEnd())
199     {
200     if(it.Get() == element)
201       {
202       if(it.IsLeaf())
203         {
204         return true;
205         }
206       else
207         {
208         return false;
209         }
210       }
211     }
212   return false;
213 }
214
215 /** Return true of the node containing the element is the root */
216 template <class TValueType>
217 bool 
218 TreeContainer<TValueType>::IsRoot( TValueType element ) 
219 {
220   PreOrderTreeIterator<Self> it(this,m_Root);
221   it.GoToBegin();
222   while(!it.IsAtEnd())
223     {
224     if(it.Get() == element)
225       {
226       if(!it.HasParent())
227         {
228         return true;
229         }
230       else
231         {
232         return false;
233         }
234       }
235     ++it;
236     }
237   return false;
238 }
239
240 /** Clear the tree */
241 template <class TValueType>
242 bool TreeContainer<TValueType>::Clear() 
243 {
244   PreOrderTreeIterator<Self> it(this,m_Root);
245   it.GoToBegin();
246   while(!it.IsAtEnd())
247     {
248     if(it.Remove())
249       {
250       ++it;
251       }
252     else
253       {
254       return false;
255       }
256     }
257   m_Root = NULL;
258   return true;
259 }
260
261 /** Get node given a value */
262 template <class TValueType>
263 const TreeNode<TValueType>* 
264 TreeContainer<TValueType>::GetNode(TValueType val) const
265 {
266   PreOrderTreeIterator<Self> it(this,m_Root);
267   it.GoToBegin();
268   while(!it.IsAtEnd())
269     {
270     if(it.Get() == val)
271       {
272       return it.GetNode();
273       }
274     ++it;
275     }
276   return NULL;
277 }
278
279 /** Set the root of the tree from the iterator position */
280 template <class TValueType>
281 bool 
282 TreeContainer<TValueType>::SetRoot( IteratorType& pos ) 
283 {
284   if( this->GetSubTree() )
285     {
286     return false;
287     }
288   TreeNode<TValueType>* node = pos.GetNode();
289   if ( node == NULL )
290     {
291     return false;
292     }
293     
294   TreeNode<TValueType>* parent = node->GetParent();
295   TreeNode<TValueType>* help = NULL;
296
297   if ( parent == NULL )
298     {
299     return false;
300     }
301   
302   m_Root = node;
303   node->AddChild( parent );
304   parent->Remove( node );
305   node->SetParent( NULL );
306   help = parent->GetParent();
307   parent->SetParent( node );
308   node = parent;
309
310   while ( help != NULL ) 
311     {
312     parent = help;
313     help = help->GetParent();
314     node->AddChild( parent );
315     parent->Remove( node );
316     parent->SetParent( node );
317     node = parent;
318     }
319   return true;
320 }
321
322 /** Add a child to a given parent */
323 template <class TValueType>
324 bool 
325 TreeContainer<TValueType>::Add(const TValueType child, const TValueType parent)
326 {
327   if(!m_Root)
328     {
329 LEN     std::cout << "TreeContainer<TValueType>::Add() : The tree is empty" << std::endl;
330     return false;
331     }
332   // Find the first node in the tree that has the parent value
333   PreOrderTreeIterator<Self> it(this,m_Root);
334   it.GoToBegin();
335   while(!it.IsAtEnd())
336     {
337     if(it.Get() == parent)
338       {
339       it.Add(child);
340       return true;
341       }
342     ++it;
343     }
344   return false;
345 }
346
347 /** Print self */
348 template <class TValueType>
349 void
350 TreeContainer<TValueType>::
351 PrintSelf(std::ostream &os, Indent indent) const
352 {
353   Superclass::PrintSelf(os,indent);
354   os << indent << "Number of objects = " << this->Count() << std::endl;
355
356   if(this->Count() > 0)
357     {
358     os << indent << "Tree:" << std::endl;
359     // Now prints the tree
360     PreOrderTreeIterator<Self> it(this,m_Root);
361     it.GoToBegin();
362     while(!it.IsAtEnd())
363       {
364       if(it.GetParent())
365         {
366         std::cout << it.GetParent()->Get() << " <- ";
367         }
368       std::cout << it.Get() << std::endl;
369       ++it;
370       }
371     }
372 }
373
374 // namespace itk
375
376 #endif
377

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