Overture  Version 25
NTreeNode.h
Go to the documentation of this file.
1 #ifndef __KKC_NTREENODE__
2 #define __KKC_NTREENODE__
3 
4 
5 // *wdh* turn off assertions.
6 // define ASSERTIONS_ON
7 #undef ASSERTIONS_ON
8 
9 //kkc 040415 #include <iostream.h>
10 #include "OvertureDefine.h"
11 #include OV_STD_INCLUDE(iostream)
12 
13 #include "Overture.h"
14 #include "NTreeNodeExceptions.h"
15 
16 // NTreeNode is a class used to build fixed degree tree container classes,it is not intended for use all by itself
17 
18 template<int degree, class Data>
19 class NTreeNode
20 {
21 public:
22 
23  typedef Data DataType;
24  inline NTreeNode();
25  inline NTreeNode(Data &data_);
26  inline NTreeNode(Data &data_, NTreeNode *trunk_);
27  inline NTreeNode(NTreeNode *trunk_);
28  inline NTreeNode(NTreeNode<degree, Data> &node_) : data(node_.data)
29  {
30  for (int i=0; i<degree; i++) leaves[i] = node_.leaves[i];
31  trunk = node_.trunk;
32  }
33 
34  inline ~NTreeNode();
35 
36  inline int add(Data &data_);
37  inline int add(int d, Data &data_);
38  inline int add(int d); // add an empty node
39  inline int del(int nDel);
40  inline int change(NTreeNode *nPtr);
41  inline int change(int d, NTreeNode *nPtr);
42  inline bool querry(int d); // returns false for NULL and true for alive
43  inline bool querry(); // returns false if there is no trunk, true otherwise
44  inline int nullify();
45 
46  inline NTreeNode & getTrunk();
47  inline const NTreeNode & getTrunk() const;
48  inline NTreeNode & getLeaf(int d);
49  inline const NTreeNode & getLeaf(int d) const;
50  inline Data & getData() { return data; }
51  inline const Data & getData() const { return data; }
52 
53 private:
54  int isValid;
55  Data data;
56 
57  NTreeNode *trunk;
58  NTreeNode *leaves[degree];
59 };
60 
61 //
62 // inlined methods for NTreeNode
63 //
64 //\begin{>NTreeNode.tex}{\subsection{Default Constructor}}
65 template<int degree, class Data>
66 inline
69 //======================================================
70 // /Purpose : build and initialize a tree node
71 //\end
72 //======================================================
73 {
74  //
75  // set the internal check isValid to 1
76  //
77  isValid = 1;
78  //
79  // nullify all the leaves
80  //
81  nullify();
82 }
83 
84 //\begin{>>NTreeNode.tex}{\subsection{Constructor}}
85 template<int degree, class Data>
86 inline
88 NTreeNode(Data &data_)
89 //======================================================
90 // /Purpose : build and initialize a tree node given the data for the node to contain
91 // /data (input) : reference to the data to be stored in the node
92 //\end
93 //======================================================
94 {
95  //
96  // set the internal check isValid to 1
97  //
98  isValid = 1;
99  //
100  // nullify all the leaves
101  //
102  nullify();
103  //
104  // assign the internal data reference
105  //
106  data=data_;
107 
108 }
109 
110 //\begin{>>NTreeNode.tex}{\subsection{Constructor}}
111 template<int degree, class Data>
112 inline
114 NTreeNode(Data &data_, NTreeNode *trunk_)
115 //======================================================
116 // /Purpose : build and initialize a tree node
117 // /data\_ (input) : reference to the data to be stored in the node
118 // /trunk\_ (input) : pointer to the trunk
119 //\end
120 //======================================================
121 {
122  //
123  // set the internal check isValid to 1
124  //
125  isValid = 1;
126  //
127  // nullify all the leaves
128  //
129  nullify();
130  //
131  // assign data members
132  //
133  data=data_;
134  trunk=trunk_;
135 }
136 
137 //\begin{>>NTreeNode.tex}{\subsection{Constructor}}
138 template<int degree, class Data>
139 inline
142 //======================================================
143 // /Purpose : build and initialize a tree node
144 // /data\_ (input) : reference to the data to be stored in the node
145 // /trunk\_ (input) : pointer to the trunk
146 //\end
147 //======================================================
148 {
149  //
150  // set the internal check isValid to 1
151  //
152  isValid = 1;
153  //
154  // nullify all the leaves
155  //
156  nullify();
157  trunk=trunk_;
158 }
159 
160 //\begin{>>NTreeNode.tex}{\subsection{Destructor}}
161 template<int degree, class Data>
162 inline
165 //======================================================
166 // /Purpose : destroy a node and all its leaves
167 //\end
168 //======================================================
169 {
170  //
171  // confirm that this node is not corrupt (ie, deallocated elsewhere)
172  //
173 #ifdef ASSERTIONS_ON
174 #if 1
175  AssertException (isValid == 1, InvalidNode());
176 #else
177  AssertException<InvalidNode> (isValid == 1);
178 #endif
179 #endif
180  //
181  // delete the leaves
182  //
183  for (int d=0; d<degree; d++)
184  if (leaves[d]!=NULL) del(d);
185 
186  //
187  // make sure the node is invalid before destruction is complete
188  //
189  isValid = -1;
190 
191 }
192 
193 //\begin{>>NTreeNode.tex}{\subsection{add}}
194 template<int degree, class Data>
195 inline
196 int
198 add(Data &data_)
199 //======================================================
200 // /Purpose : add a data leaf to the next available leaf position
201 // /data (input) : data item to be stored
202 // /Returns : 0 on success
203 // /Throws :
204 // \begin{itemize}
205 // \item TreeDegreeViolation : if the tree bookkeeping is corrupt
206 // \item NodeFullError : if all the leaves in this node are ful
207 // \end{itemize}
208 //\end
209 //======================================================
210 {
211  int d=0;
212  while(d<degree && leaves[d]!=NULL)
213  d++;
214 
215 #ifdef ASSERTIONS_ON
216 #if 1
217  AssertException (d<degree && d>=0, TreeDegreeViolation());
218  AssertException (leaves[d]==NULL, NodeFullError());
219 #else
220  AssertException<TreeDegreeViolation> (d<degree && d>=0);
221  AssertException<NodeFullError> (leaves[d]==NULL);
222 #endif
223 #endif
224 
225  //assert(d<degree && leaves[d]==NULL);
226  leaves[d] = new NTreeNode( data_, this);
227  return 0;
228 }
229 
230 //\begin{>>NTreeNode.tex}{\subsection{add}}
231 template<int degree, class Data>
232 inline
233 int
235 add(int d, Data &data_)
236 //======================================================
237 // /Purpose : add a data leaf to a specified leaf
238 // /d (input) : leaf to store data
239 // /data_ (input) : data item to be stored
240 // /Returns : 0 on success
241 // /Throws :
242 // \begin{itemize}
243 // \item TreeDegreeViolation : if d is not a valid leaf number (if d is greater than the degree of the tree or less than zero
244 // \item NodeFullError : if d is already used up
245 // \end{itemize}
246 //\end
247 //======================================================
248 {
249  // assert(d<degree && leaves[d]==NULL);
250 #ifdef ASSERTIONS_ON
251 #if 1
252  AssertException (d<degree && d>=0, TreeDegreeViolation());
253  AssertException (leaves[d]==NULL, NodeFullError());
254 #else
255  AssertException<TreeDegreeViolation> (d<degree && d>=0);
256  AssertException<NodeFullError> (leaves[d]==NULL);
257 #endif
258 #endif
259  leaves[d] = new NTreeNode( data_, this);
260  return 0;
261 }
262 
263 //\begin{>>NTreeNode.tex}{\subsection{add}}
264 template<int degree, class Data>
265 inline
266 int
268 add(int d)
269 //======================================================
270 // /Purpose : add a data leaf to a specified leaf
271 // /d (input) : leaf to store data
272 // /data (input) : data item to be stored
273 // /Returns : 0 on success
274 // /Throws :
275 // \begin{itemize}
276 // \item TreeDegreeViolation : if d is not a valid leaf number (if d is greater than the degree of the tree or less than zero
277 // \item NodeFullError : if d is already used up
278 // \end{itemize}
279 //\end
280 //======================================================
281 {
282  // assert(d<degree && leaves[d]==NULL);
283 #ifdef ASSERTIONS_ON
284 #if 1
285  AssertException (d<degree && d>=0, TreeDegreeViolation());
286  AssertException (leaves[d]==NULL, NodeFullError());
287 #else
288  AssertException<TreeDegreeViolation> (d<degree && d>=0);
289  AssertException<NodeFullError> (leaves[d]==NULL);
290 #endif
291 #endif
292  leaves[d] = new NTreeNode(this);
293  return 0;
294 }
295 
296 //\begin{>>NTreeNode.tex}{\subsection{del}}
297 template<int degree, class Data>
298 inline
299 int
301 del(int nDel)
302 //======================================================
303 // /Purpose : delete a specified leaf
304 // /nDel (input) : leaf to delete
305 // /Returns : 0 on success
306 // /Throws :
307 // \begin{itemize}
308 // \item TreeDegreeViolation : if d is not a valid leaf number (if d is greater than the degree of the tree or less than zero
309 // \end{itemize}
310 //\end
311 //======================================================
312 {
313  //cout << "inside del"<<endl;
314 #ifdef ASSERTIONS_ON
315 #if 1
316  AssertException (nDel>=0 && nDel<degree, TreeDegreeViolation());
317 #else
318  AssertException<TreeDegreeViolation> (nDel>=0 && nDel<degree);
319 #endif
320 #endif
321  //assert(nDel>=0 && nDel<degree);
322  if (leaves[nDel]!=NULL) {
323  delete leaves[nDel];
324  leaves[nDel] = NULL;
325  }
326  // trunk = NULL;
327  return 0;
328 }
329 
330 //\begin{>>NTreeNode.tex}{\subsection{change}}
331 template<int degree, class Data>
332 inline
333 int
336 //======================================================
337 // /Purpose : change a node's trunk
338 // /nPtr (input) : pointer to the new trunk
339 // /Returns : 0 on success
340 // /Throws : nothing
341 //\end
342 //======================================================
343 {
344  trunk = nPtr;
345  return 0;
346 }
347 
348 //\begin{>>NTreeNode.tex}{\subsection{change}}
349 template<int degree, class Data>
350 inline
351 int
354 //======================================================
355 // /Purpose : changes a particular leaf
356 // /d (input) : leaf to change
357 // /nPtr (input) : pointer to the new leaf
358 // /Returns : 0 on success
359 // /Throws :
360 // \begin{itemize}
361 // \item TreeDegreeViolation : if d is not a valid leaf number
362 // \end{itemize}
363 //\end
364 //======================================================
365 {
366 #ifdef ASSERTIONS_ON
367 #if 1
368  AssertException (d<degree && d>=0, TreeDegreeViolation());
369 #else
370  AssertException<TreeDegreeViolation> (d<degree && d>=0);
371 #endif
372 #endif
373  //assert(d<degree && d>=0);
374  leaves[d] = nPtr;
375  return 0;
376 }
377 
378 //\begin{>>NTreeNode.tex}{\subsection{change}}
379 template<int degree, class Data>
380 inline
381 bool
383 querry(int d)
384 //======================================================
385 // /Purpose : see if a particular leaf has data
386 // /d (input) : leaf to querry
387 // /Returns : false if leaf d is NULL, true otherwise
388 // /Throws :
389 // \begin{itemize}
390 // \item TreeDegreeViolation : if d is not a valid leaf number
391 // \end{itemize}
392 //\end
393 //======================================================
394 {
395 #ifdef ASSERTIONS_ON
396 #if 1
397  AssertException (d<degree && d>=0, TreeDegreeViolation());
398 #else
399  AssertException<TreeDegreeViolation> (d<degree && d>=0);
400 #endif
401 #endif
402  return leaves[d] != NULL;
403 // if (leaves[d] == NULL)
404 // return false;
405 // else
406 // return true;
407 }
408 
409 //\begin{>>NTreeNode.tex}{\subsection{change}}
410 template<int degree, class Data>
411 inline
412 bool
415 //======================================================
416 // /Purpose : see if the trunk has data
417 // /d (input) : leaf to querry
418 // /Returns : false if the trunk pointer is NULL, true otherwise
419 // /Throws :
420 // \begin{itemize}
421 // \item TreeDegreeViolation : if d is not a valid leaf number
422 // \end{itemize}
423 //\end
424 //======================================================
425 {
426  return trunk!=NULL;
427 // if (trunk == NULL)
428 // return false;
429 // else
430 // return true;
431 }
432 
433 template<int degree, class Data>
434 inline
435 int
438 {
439  trunk = NULL;
440  for (int d=0; d<degree; d++)
441  leaves[d] = NULL;
442  return 0;
443 }
444 
445 //\begin{>>NTreeNode.tex}{\subsection{getTrunk}}
446 template<int degree, class Data>
447 inline
451 //======================================================
452 // /Purpose : return a reference to the trunk node
453 // /Returns : a reference the the trunk
454 // /Throws : nothing
455 //\end
456 //======================================================
457 {
458  return *trunk;
459 }
460 
461 //\begin{>>NTreeNode.tex}{\subsection{const getTrunk}}
462 template<int degree, class Data>
463 inline
464 const NTreeNode<degree,Data> &
466 getTrunk() const
467 //======================================================
468 // /Purpose : return a const reference to the trunk node
469 // /Returns : a const reference the the trunk
470 // /Throws : nothing
471 //\end
472 //======================================================
473 {
474  return *trunk;
475 }
476 
477 //\begin{>>NTreeNode.tex}{\subsection{const getLeaf}}
478 template<int degree, class Data>
479 inline
482 //======================================================
483 // /Purpose : return a reference to a specific leaf node
484 // /d (input) : leaf to return
485 // /Returns : a reference to the requested leaf
486 // /Throws :
487 // \begin{itemize}
488 // \item TreeDegreeViolation : if d is not a valid leaf number
489 // \end{itemize}
490 //\end
491 //======================================================
492 {
493 #ifdef ASSERTIONS_ON
494 #if 1
495  AssertException(d<degree && d>=0,TreeDegreeViolation());
496 #else
497  AssertException<TreeDegreeViolation> (d<degree && d>=0);
498 #endif
499 #endif
500 
501  return *leaves[d];
502 }
503 
504 //\begin{>>NTreeNode.tex}{\subsection{const getLeaf}}
505 template<int degree, class Data>
506 inline
507 const NTreeNode<degree,Data> &
509 getLeaf(int d) const
510 //======================================================
511 // /Purpose : return a const reference to a specific leaf node
512 // /d (input) : leaf to return
513 // /Returns : a reference to the requested leaf
514 // /Throws :
515 // \begin{itemize}
516 // \item TreeDegreeViolation : if d is not a valid leaf number
517 // \end{itemize}
518 //\end
519 //======================================================
520 {
521 #ifdef ASSERTIONS_ON
522 #if 1
523  AssertException(d<degree && d>=0, TreeDegreeViolation());
524 #else
525  AssertException<TreeDegreeViolation> (d<degree && d>=0);
526 #endif
527 #endif
528  return *leaves[d];
529 }
530 
531 #if 0
532 template<int degree, class Data>
533 inline
534 //const Data &
535 Data &
537 getData()
538 {
539  return data;
540 }
541 
542 template<int degree, class Data>
543 inline
544 //const Data &
545 const Data &
547 getData() const
548 {
549  return data;
550 }
551 #endif
552 
553 #undef ASSERTIONS_ON
554 #endif