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

Generated at Sun May 19 2013 00:10:51 for Orfeo Toolbox with doxygen 1.8.3.1