Files Classes Functions Hierarchy
00001 #ifndef TREEINDEXEDD2_H 00002 #define TREEINDEXEDD2_H 00003 00004 #include <iostream> 00005 #include <sstream> 00006 using namespace std; 00007 00008 #include <point.h> 00009 #include <print.h> 00010 #include <typedefs.h> 00011 00012 template <typename INDX> 00013 class treeindexedD2node; 00014 00015 template< typename INDX > 00016 class treeindexedD2noderep; 00017 00027 template <typename INDX> 00028 class treeindexedD2 00029 { 00030 public: 00031 00034 INDX nodecounter; 00037 INDX regioncounter; 00038 00040 treeindexedD2node<INDX> * root; 00042 treeindexedD2node<INDX> * current; 00043 00045 void construct(); 00048 treeindexedD2(); 00050 ~treeindexedD2(); 00051 00053 inline void reset(); 00055 inline boolc operator ! () const; 00057 inline treeindexedD2node<INDX> * operator()(); 00058 00060 void moveleft(); 00062 void moveright(); 00065 void move(INDX const bpath, INDX const bitlength); 00066 /* Given a path get the indexes of the nodes along the path. */ 00067 template< typename W > 00068 void pathindex( W & container, INDX const bpath, INDX const bitlength); 00069 00072 void addleft(); 00075 void addright(); 00076 00078 operator stringc () const; 00081 istream & serializeInverse(istream & istr); 00082 00085 void insert 00086 ( 00087 bool & found, 00088 treeindexedD2node<INDX> & curr, 00089 treeindexedD2noderep<INDX> const & ndrep 00090 ); 00092 void addroot(treeindexedD2noderep<INDX> const & ndrep); 00093 }; 00094 00095 template< typename INDX > 00096 istream & operator >> (istream & istr, treeindexedD2<INDX> & x) 00097 { return x.serializeInverse(istr); } 00098 00116 template <typename INDX> 00117 class treeindexedD2node 00118 { 00119 public: 00120 00122 treeindexedD2node * left; 00124 treeindexedD2node * right; 00126 INDX index; 00127 00129 boolc isleaf() const; 00131 boolc isleft(INDX index2 ) const; 00133 boolc isright(INDX index2) const; 00135 boolc isleftinternal(INDX index2) const; 00137 boolc isrightinternal(INDX index2) const; 00138 00140 treeindexedD2node(); 00142 treeindexedD2node(INDX const index_); 00144 treeindexedD2node 00145 ( 00146 treeindexedD2node * left_, 00147 treeindexedD2node * right_, 00148 INDX index_ 00149 ); 00151 ~treeindexedD2node(); 00152 00155 void unlink(); 00157 operator stringc () const; 00161 void printrecursive 00162 ( 00163 string & s, 00164 INDX const parentindex, 00165 boolc branch 00166 ) const; 00168 void swap(); 00170 void nodecount( INDX & n ); 00171 00172 }; 00173 00174 00175 /* 00176 \brief Uniquely identifies a node as a three indexes for the node, 00177 left and right, and its position in the tree. 00178 00179 The position is a parent index and a boolean of false 00180 for left parent branch, true for right parent branch. 00181 */ 00182 template< typename INDX > 00183 class treeindexedD2noderep 00184 { 00185 public: 00186 00188 point4<INDX> nd; 00190 bool branch; 00191 00193 operator stringc () const; 00194 00196 istream & serializeInverse(istream & istr); 00197 }; 00198 00199 template< typename INDX > 00200 istream & operator >> (istream & istr, treeindexedD2noderep<INDX> & x) 00201 { return x.serializeInverse(istr); } 00202 00203 template< typename INDX > 00204 ostream & operator << 00205 ( 00206 ostream & os, treeindexedD2noderep<INDX> const & x 00207 ) 00208 { return os << (stringc)x; } 00209 00210 00211 00212 //--------------------------------------------------------- 00213 // Implementation 00214 00215 00216 //<TODO> 00219 //1-2 00220 // -2-4-5 00221 // -3 00222 // -3-4 00223 // -1 00224 00225 //stringc printtree() const; 00226 00227 00228 00229 template <typename INDX> 00230 template< typename W > 00231 void treeindexedD2<INDX>::pathindex 00232 ( 00233 W & container, 00234 INDX const bpath, 00235 INDX const bitlength 00236 ) 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 } 00258 00259 template <typename INDX> 00260 treeindexedD2<INDX>::~treeindexedD2() 00261 { 00262 delete root; 00263 } 00264 00265 template <typename INDX> 00266 void treeindexedD2<INDX>::reset() 00267 { 00268 current=root; 00269 } 00270 00271 template <typename INDX> 00272 boolc treeindexedD2<INDX>::operator ! () const 00273 { 00274 return current!=0; 00275 } 00276 00277 template <typename INDX> 00278 treeindexedD2node<INDX> * treeindexedD2<INDX>::operator()() 00279 { 00280 assert(current!=0); 00281 return current; 00282 } 00283 00284 00285 template <typename INDX> 00286 treeindexedD2<INDX>::treeindexedD2() 00287 : nodecounter(0), regioncounter(0), root(0), current(0) 00288 { 00289 construct(); 00290 } 00291 00292 template <typename INDX> 00293 void treeindexedD2<INDX>::moveleft() 00294 { 00295 assert(root!=0); 00296 assert(current!=0); 00297 assert(current->left!=0); 00298 current = current->left; 00299 } 00300 00301 template <typename INDX> 00302 void treeindexedD2<INDX>::moveright() 00303 { 00304 assert(root!=0); 00305 assert(current!=0); 00306 assert(current->right!=0); 00307 current = current->right; 00308 } 00309 00310 template <typename INDX> 00311 void treeindexedD2<INDX>::move(INDX const bpath, INDX const bitlength) 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 } 00334 00335 00336 template <typename INDX> 00337 void treeindexedD2<INDX>::addroot 00338 ( 00339 treeindexedD2noderep<INDX> const & ndrep 00340 ) 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 } 00350 00351 template <typename INDX> 00352 void treeindexedD2<INDX>::insert 00353 ( 00354 bool & found, 00355 treeindexedD2node<INDX> & curr, 00356 treeindexedD2noderep<INDX> const & ndrep 00357 ) 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 } 00411 00412 00413 template <typename INDX> 00414 istream & treeindexedD2<INDX>::serializeInverse(istream & istr) 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 } 00448 00449 00450 template <typename INDX> 00451 void treeindexedD2<INDX>::addright() 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 } 00468 00469 template <typename INDX> 00470 void treeindexedD2<INDX>::addleft() 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 } 00487 00488 template <typename INDX> 00489 treeindexedD2<INDX>::operator stringc () const 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 } 00510 00511 00512 template <typename INDX> 00513 void treeindexedD2<INDX>::construct() 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 } 00526 00527 00528 /* 00529 template <typename INDX> 00530 treeindexedD2<INDX>::treeindexedD2 00531 ( 00532 INDX const nodecounter_, 00533 INDX const regioncounter_ 00534 ) 00535 : nodecounter(nodecounter_), regioncounter(regioncounter_) 00536 { 00537 root = new treeindexedD2node<INDX>(nodecounter++); 00538 assert(root!=0); 00539 root->left = new treeindexedD2node<INDX>(regioncounter++); 00540 root->right = new treeindexedD2node<INDX>(regioncounter++); 00541 current=root; 00542 } 00543 */ 00544 00545 00546 00547 00548 00549 template <typename INDX> 00550 void treeindexedD2node<INDX>::unlink() 00551 { 00552 treeindexedD2node<INDX> * p; 00553 p = left; 00554 if (left!=0) 00555 { 00556 left=0; 00557 p->unlink(); 00558 } 00559 p = right; 00560 if (right!=0) 00561 { 00562 right=0; 00563 p->unlink(); 00564 } 00565 } 00566 00567 template <typename INDX> 00568 treeindexedD2node<INDX>::operator stringc () const 00569 { 00570 stringstream ss; 00571 ss << index << " "; 00572 if (left) 00573 { 00574 ss << left->index; 00575 00576 if (right) 00577 { 00578 ss << " " << right->index; 00579 } 00580 else 00581 { 00582 ss << " 0"; 00583 } 00584 } 00585 else 00586 { 00587 ss << "0 "; 00588 if (right) 00589 { 00590 ss << " " << right->index; 00591 } 00592 else 00593 { 00594 ss << " 0"; 00595 } 00596 } 00597 00598 return ss.str(); 00599 } 00600 00601 template <typename INDX> 00602 void treeindexedD2node<INDX>::printrecursive 00603 ( 00604 string & s, 00605 INDX const parentindex, 00606 boolc branch 00607 ) const 00608 { 00609 s += (stringc)*this; 00610 00611 { 00612 stringstream ss; 00613 ss << parentindex; 00614 00615 s += " "; 00616 s += ss.str(); 00617 } 00618 00619 if (branch==false) 00620 s += " 0"; 00621 else 00622 s += " 1"; 00623 00624 if ((left!=0)||(right!=0)) 00625 s += '\n'; 00626 if (left!=0) 00627 { 00628 if (left->isleaf()==false) 00629 left->printrecursive(s,index,false); 00630 } 00631 if (right!=0) 00632 { 00633 if (right->isleaf()==false) 00634 right->printrecursive(s,index,true); 00635 } 00636 } 00637 00638 template <typename INDX> 00639 void treeindexedD2node<INDX>::nodecount( INDX & n ) 00640 { 00641 if (isleaf()) 00642 return; 00643 00644 ++n; 00645 if (left!=0) 00646 left->nodecount(n); 00647 if (right!=0) 00648 right->nodecount(n); 00649 } 00650 00651 template <typename INDX> 00652 treeindexedD2node<INDX>::treeindexedD2node() 00653 : left(0), right(0), index(0) 00654 { 00655 } 00656 00657 template <typename INDX> 00658 treeindexedD2node<INDX>::treeindexedD2node(INDX const index_) 00659 : left(0), right(0), index(index_) 00660 { 00661 } 00662 00663 template <typename INDX> 00664 treeindexedD2node<INDX>::treeindexedD2node 00665 ( 00666 treeindexedD2node * left_, 00667 treeindexedD2node * right_, 00668 INDX index_ 00669 ) 00670 : left(left_), right(right_), index(index_) 00671 { 00672 } 00673 00674 00675 template <typename INDX> 00676 treeindexedD2node<INDX>::~treeindexedD2node() 00677 { 00678 delete left; 00679 delete right; 00680 left=0; 00681 right=0; 00682 } 00683 00684 00685 template <typename INDX> 00686 void treeindexedD2node<INDX>::swap() 00687 { 00688 treeindexedD2node * tmp=left; 00689 right=left; 00690 left=tmp; 00691 } 00692 00693 00694 00695 00696 00697 template <typename INDX> 00698 boolc treeindexedD2node<INDX>::isleaf() const 00699 { 00700 return (left==0)&&(right==0); 00701 } 00702 00703 00704 template <typename INDX> 00705 boolc treeindexedD2node<INDX>::isleft(INDX index2 ) const 00706 { 00707 if (left==0) return false; 00708 if (left->index==index2) return true; 00709 return false; 00710 } 00711 00712 template <typename INDX> 00713 boolc treeindexedD2node<INDX>::isright(INDX index2) const 00714 { 00715 if (right==0) return false; 00716 if (right->index==index2) return true; 00717 return false; 00718 } 00719 00720 template <typename INDX> 00721 boolc treeindexedD2node<INDX>::isleftinternal(INDX index2) const 00722 { 00723 if (left==0) return false; 00724 if (left->index!=index2) return false; 00725 if (left->isleaf()) return false; 00726 return true; 00727 } 00728 00729 template <typename INDX> 00730 boolc treeindexedD2node<INDX>::isrightinternal(INDX index2) const 00731 { 00732 if (right==0) return false; 00733 if (right->index!=index2) return false; 00734 if (right->isleaf()) return false; 00735 return true; 00736 } 00737 00738 00739 00740 00741 template <typename INDX> 00742 treeindexedD2noderep<INDX>::operator stringc () const 00743 { 00744 string s((stringc)nd); 00745 if (branch==false) 00746 s += " 0"; 00747 else 00748 s += " 1"; 00749 00750 return s; 00751 } 00752 00753 template <typename INDX> 00754 istream & treeindexedD2noderep<INDX>::serializeInverse(istream & istr) 00755 { 00756 istr >> nd; 00757 istr >> branch; 00758 return istr; 00759 } 00760 00761 00762 00763 00764 #endif 00765 00766
1.5.8