proj home

Files   Classes   Functions   Hierarchy  

treeindexedD2< INDX > Class Template Reference

Binary tree with state. More...

#include <treeindexedD2.h>

Inheritance diagram for treeindexedD2< INDX >:
Collaboration diagram for treeindexedD2< INDX >:

List of all members.

Public Member Functions

void construct ()
 Build the initial tree with root and two leafs.
 treeindexedD2 ()
 Set the counters with a value 1.
 ~treeindexedD2 ()
 Memory cleanup.
void reset ()
 Set current to top of tree.
boolc operator! () const
 Is iteration possible?
treeindexedD2node< INDX > * operator() ()
 Access the current node.
void moveleft ()
 Move the current node left.
void moveright ()
 Move the current node right.
void move (INDX const bpath, INDX const bitlength)
 Move along a path.
template<typename W >
void pathindex (W &container, INDX const bpath, INDX const bitlength)
 Print the tree as a tree in text.
void addleft ()
 Assuming the current node is an internal node ( has two leafs), add a new node left.
void addright ()
 At the current node add a new node right.
 operator stringc () const
 Serialize this object by writing it out as a string.
istream & serializeInverse (istream &istr)
 Inverse of string serialization.
void insert (bool &found, treeindexedD2node< INDX > &curr, treeindexedD2noderep< INDX > const &ndrep)
 Set found to false before use as this is recursive.
void addroot (treeindexedD2noderep< INDX > const &ndrep)
 The first node inserted into the tree.

Public Attributes

INDX nodecounter
 As nodes are created this index increases and assigns indexes to the new nodes.
INDX regioncounter
 As regions are created this index increases and assignes indexes to the new regions.
treeindexedD2node< INDX > * root
 The top node.
treeindexedD2node< INDX > * current
 Current node.


Detailed Description

template<typename INDX>
class treeindexedD2< INDX >

Binary tree with state.

treeindexedD2 and treeindexedD2node are coupled, with functionality in whatever was convenient.

The tree has nodes and leafs. By design a node is locally balanced having a left and right node or leaf.

Definition at line 28 of file treeindexedD2.h.


Constructor & Destructor Documentation

template<typename INDX >
treeindexedD2< INDX >::treeindexedD2 (  )  [inline]

Set the counters with a value 1.

Build the root node and two leafs.

Definition at line 286 of file treeindexedD2.h.

References treeindexedD2< INDX >::construct().

00287   : nodecounter(0), regioncounter(0), root(0), current(0) 
00288 { 
00289   construct(); 
00290 }

template<typename INDX >
treeindexedD2< INDX >::~treeindexedD2 (  )  [inline]

Memory cleanup.

Definition at line 260 of file treeindexedD2.h.

References treeindexedD2< INDX >::root.

00261 { 
00262   delete root; 
00263 }


Member Function Documentation

template<typename INDX >
void treeindexedD2< INDX >::addleft (  )  [inline]

Assuming the current node is an internal node ( has two leafs), add a new node left.

Definition at line 470 of file treeindexedD2.h.

References treeindexedD2< INDX >::current, treeindexedD2< INDX >::nodecounter, and treeindexedD2< INDX >::regioncounter.

Referenced by treeindexedD2test::test01(), treeindexedD2test::test02(), treeindexedD2test::test03(), and treeindexedD2test::unittest02().

00471 {
00472   assert(current!=0); 
00473   // Balanced node.
00474   assert(current->left!=0); 
00475   assert(current->right!=0); 
00476 
00477   // left is a leaf.
00478   assert(current->isleaf()==false);
00479   assert(current->left->isleaf()==true);
00480 
00481   treeindexedD2node<INDX> * q = 
00482     new treeindexedD2node<INDX>(regioncounter++);
00483   treeindexedD2node<INDX> * p2 = 
00484     new treeindexedD2node<INDX>(current->left,q,nodecounter++);
00485   current->left = p2;
00486 }

template<typename INDX >
void treeindexedD2< INDX >::addright (  )  [inline]

At the current node add a new node right.

It is assumed that the current node has two leafs.

Definition at line 451 of file treeindexedD2.h.

References treeindexedD2< INDX >::current, treeindexedD2< INDX >::nodecounter, and treeindexedD2< INDX >::regioncounter.

Referenced by treeindexedD2test::test01(), treeindexedD2test::test02(), treeindexedD2test::test03(), and treeindexedD2test::unittest02().

00452 {
00453   assert(current!=0); 
00454   // Balanced node.
00455   assert(current->left!=0); 
00456   assert(current->right!=0); 
00457 
00458   // right is a leaf.
00459   assert(current->isleaf()==false);
00460   assert(current->right->isleaf()==true);
00461 
00462   treeindexedD2node<INDX> * q = 
00463     new treeindexedD2node<INDX>(regioncounter++);
00464   treeindexedD2node<INDX> * p2 = 
00465     new treeindexedD2node<INDX>(current->right,q,nodecounter++);
00466   current->right = p2;
00467 }

template<typename INDX>
void treeindexedD2< INDX >::addroot ( treeindexedD2noderep< INDX > const &  ndrep  )  [inline]

The first node inserted into the tree.

Definition at line 338 of file treeindexedD2.h.

References treeindexedD2noderep< INDX >::nd.

Referenced by treeindexedD2< INDX >::serializeInverse().

00341 {
00342   assert(root==0);
00343 
00344   root        = new treeindexedD2node<INDX>(ndrep.nd[0]);
00345   root->left  = new treeindexedD2node<INDX>(ndrep.nd[1]);
00346   root->right = new treeindexedD2node<INDX>(ndrep.nd[2]);
00347 
00348   current=root;
00349 }

template<typename INDX >
void treeindexedD2< INDX >::construct (  )  [inline]

Build the initial tree with root and two leafs.

Definition at line 513 of file treeindexedD2.h.

References treeindexedD2< INDX >::current, treeindexedD2< INDX >::nodecounter, treeindexedD2< INDX >::regioncounter, and treeindexedD2< INDX >::root.

Referenced by treeindexedD2< INDX >::treeindexedD2().

00514 {
00515   nodecounter=1;
00516   regioncounter=1;
00517 
00518   delete root;
00519 
00520   root        = new treeindexedD2node<INDX>(nodecounter++);
00521   assert(root!=0);
00522   root->left  = new treeindexedD2node<INDX>(regioncounter++);
00523   root->right = new treeindexedD2node<INDX>(regioncounter++);
00524   current=root;
00525 }

template<typename INDX>
void treeindexedD2< INDX >::insert ( bool found,
treeindexedD2node< INDX > &  curr,
treeindexedD2noderep< INDX > const &  ndrep 
) [inline]

Set found to false before use as this is recursive.

The root node must be inserted before use.

Definition at line 353 of file treeindexedD2.h.

References treeindexedD2noderep< INDX >::branch, treeindexedD2node< INDX >::index, treeindexedD2node< INDX >::isleaf(), treeindexedD2node< INDX >::left, treeindexedD2noderep< INDX >::nd, and treeindexedD2node< INDX >::right.

Referenced by treeindexedD2< INDX >::serializeInverse().

00358 {
00359   // Only insert one node.
00360   if (found)
00361     return;
00362 
00363   // Is the current index the parent of the target node?
00364   if (curr.index != ndrep.nd[3])
00365   {
00366     if (curr.left!=0)
00367       insert(found,*curr.left,ndrep);
00368     if (curr.right!=0)
00369       insert(found,*curr.right,ndrep);
00370 
00371     return;
00372   }
00373 
00374   treeindexedD2node<INDX> * curr2;
00375   
00376   // Meet insertion condtions.  
00377   curr2 = curr.left;
00378   if ( (ndrep.branch==false) && (curr2!=0) )
00379   {
00380     if (curr2->isleaf())
00381     {
00382       if (curr2->index==ndrep.nd[0])
00383       {
00384         found = true;
00385 
00386         curr2->left  = new treeindexedD2node<INDX>(ndrep.nd[1]);
00387         curr2->right = new treeindexedD2node<INDX>(ndrep.nd[2]);
00388 
00389         return;
00390       }
00391     }
00392   }
00393 
00394   curr2 = curr.right;
00395   if ( (ndrep.branch==true) && (curr2!=0) )
00396   {
00397     if (curr2->isleaf())
00398     {
00399       if (curr2->index==ndrep.nd[0])
00400       {
00401         found = true;
00402 
00403         curr2->left  = new treeindexedD2node<INDX>(ndrep.nd[1]);
00404         curr2->right = new treeindexedD2node<INDX>(ndrep.nd[2]);
00405 
00406         return;
00407       }
00408     }
00409   }
00410 }

template<typename INDX>
void treeindexedD2< INDX >::move ( INDX const   bpath,
INDX const   bitlength 
) [inline]

Move along a path.

For example move(0*1+1*2+0*4,3) is move left, right, left.

Definition at line 311 of file treeindexedD2.h.

References treeindexedD2< INDX >::moveleft(), treeindexedD2< INDX >::moveright(), and treeindexedD2< INDX >::reset().

Referenced by treeindexedD2test::unittest04().

00312 {
00313   reset();
00314 
00315 //cout << SHOW(current->index) << endl;
00316 
00317   INDX u(bpath);
00318 //cout << SHOW(bpath) << endl;
00319 //cout << SHOW(bitlength) << endl;
00320 
00321   bool branch;
00322   for (INDX i=0; i<bitlength; ++i)
00323   {
00324     branch=(u%2);
00325 //cout << SHOW(u) << "  " << SHOW(branch) << endl;
00326     u = (u >> 1);
00327  
00328     if (branch==false)
00329       moveleft();
00330     else
00331       moveright();
00332   }
00333 }

template<typename INDX >
void treeindexedD2< INDX >::moveleft (  )  [inline]

Move the current node left.

Definition at line 293 of file treeindexedD2.h.

References treeindexedD2< INDX >::current, and treeindexedD2< INDX >::root.

Referenced by bsptreeD2find< PT, PD, INDX >::find(), treeindexedD2< INDX >::move(), and treeindexedD2test::test02().

00294 { 
00295   assert(root!=0);
00296   assert(current!=0); 
00297   assert(current->left!=0); 
00298   current = current->left;
00299 }

template<typename INDX >
void treeindexedD2< INDX >::moveright (  )  [inline]

Move the current node right.

Definition at line 302 of file treeindexedD2.h.

References treeindexedD2< INDX >::current, and treeindexedD2< INDX >::root.

Referenced by bsptreeD2find< PT, PD, INDX >::find(), treeindexedD2< INDX >::move(), treeindexedD2test::test03(), and treeindexedD2test::unittest02().

00303 { 
00304   assert(root!=0);
00305   assert(current!=0); 
00306   assert(current->right!=0); 
00307   current = current->right;
00308 }

template<typename INDX >
treeindexedD2< INDX >::operator stringc (  )  const [inline]

Serialize this object by writing it out as a string.

Definition at line 489 of file treeindexedD2.h.

References treeindexedD2< INDX >::root.

00490 {
00491   string s;
00492 
00493   if (root==0)
00494   {
00495     s += "0";
00496     return s;
00497   }
00498 
00499   INDX count(0);
00500   root->nodecount(count);
00501   stringstream ss;
00502   ss << count;
00503   s += ss.str();
00504   s += "\n";
00505 
00506   root->printrecursive(s,0,false);
00507   
00508   return s;
00509 }

template<typename INDX >
boolc treeindexedD2< INDX >::operator! (  )  const [inline]

Is iteration possible?

Definition at line 272 of file treeindexedD2.h.

References treeindexedD2< INDX >::current.

00273 { 
00274   return current!=0; 
00275 }

template<typename INDX >
treeindexedD2node< INDX > * treeindexedD2< INDX >::operator() (  )  [inline]

Access the current node.

Definition at line 278 of file treeindexedD2.h.

References treeindexedD2< INDX >::current.

00279 { 
00280   assert(current!=0); 
00281   return current; 
00282 }

template<typename INDX>
template<typename W >
void treeindexedD2< INDX >::pathindex ( W &  container,
INDX const   bpath,
INDX const   bitlength 
) [inline]

Print the tree as a tree in text.

This lets the tree be easily visualized for small trees.

Definition at line 232 of file treeindexedD2.h.

References reset().

00237 {
00238   container.clear();
00239 
00240   reset();
00241   INDX u(bpath);
00242 
00243   bool branch;
00244   for (INDX i=0; i<bitlength; ++i)
00245   {
00246     branch=(u%2);
00247 //cout << SHOW(u) << "  " << SHOW(branch) << endl;
00248     u = (u >> 1);
00249 
00250     container.push_back(current->index);
00251  
00252     if (branch==false)
00253       moveleft();
00254     else
00255       moveright();
00256   }
00257 }

template<typename INDX >
void treeindexedD2< INDX >::reset (  )  [inline]

template<typename INDX >
istream & treeindexedD2< INDX >::serializeInverse ( istream &  istr  )  [inline]

Inverse of string serialization.

O(n^2) complexity because the tree is unordered.

Definition at line 414 of file treeindexedD2.h.

References treeindexedD2< INDX >::addroot(), treeindexedD2< INDX >::insert(), and treeindexedD2< INDX >::root.

Referenced by operator>>().

00415 {
00416   uint n;
00417   istr >> n;
00418 
00419   if (n==0)
00420     return istr;
00421 
00422   treeindexedD2noderep<INDX> vi[n];
00423 
00424   for (uint i=0; i<n; ++i)
00425     istr >> vi[i];
00426 
00427 //cout << SHOW(n) << endl;
00428 //cout << "Verify vi" << endl;
00429 //cout << print(vi,vi+n,"\n") << endl << endl;
00430 
00431   addroot(vi[0]);
00432 //cout << (stringc)*this << endl << endl;
00433 
00434   bool found;
00435 
00436   for (uint i=1; i<n; ++i)
00437   {
00438     found=false;
00439     
00440     insert(found,*root,vi[i]);
00441     assert(found==true); 
00442 
00443 //cout << (stringc)*this << endl << endl;
00444   }
00445 
00446   return istr;
00447 }


Member Data Documentation

template<typename INDX>
treeindexedD2node<INDX>* treeindexedD2< INDX >::current

template<typename INDX>
INDX treeindexedD2< INDX >::nodecounter

As nodes are created this index increases and assigns indexes to the new nodes.

Definition at line 34 of file treeindexedD2.h.

Referenced by treeindexedD2< INDX >::addleft(), treeindexedD2< INDX >::addright(), and treeindexedD2< INDX >::construct().

template<typename INDX>
INDX treeindexedD2< INDX >::regioncounter

As regions are created this index increases and assignes indexes to the new regions.

Definition at line 37 of file treeindexedD2.h.

Referenced by treeindexedD2< INDX >::addleft(), treeindexedD2< INDX >::addright(), and treeindexedD2< INDX >::construct().

template<typename INDX>
treeindexedD2node<INDX>* treeindexedD2< INDX >::root


The documentation for this class was generated from the following file:

Generated on Fri Mar 4 00:50:20 2011 for Chelton Evans Source by  doxygen 1.5.8