Tree.h

Go to the documentation of this file.
00001 
00011 #ifndef _H_TREE
00012 #define _H_TREE
00013 #include <vector>
00014 
00024 //#define _INDEX
00025 
00031 template< typename stored_type, typename tree_type >
00032 class Tree {
00033   protected:
00034 
00036         vector< stored_type > elements;
00037 
00039         vector< tree_type* > children;
00040 
00041 #ifdef _INDEX
00042 //TODO find a way to traverse the tree without using this int and the changed bool!
00043         int _id;
00044 #endif
00045 
00047         tree_type *parent;
00048 
00049 #ifdef _INDEX
00050 
00051         bool changed;
00052 
00059         tree_type *getNext( const int id_at );
00060         
00067         int index( int id_at );
00068 #endif
00069 
00070 public:
00071 
00075         virtual ~Tree() {}
00076 
00080         Tree();
00081 
00087         Tree( tree_type *par );
00088 
00089 #ifdef _INDEX
00090 
00093         void index();
00094 #endif
00095 
00099         tree_type *getRoot();
00100         
00107         int getDepth() const;
00108         
00119         int getHeight() const;
00120 
00126         bool isRoot() const { return ( parent == 0); }
00127 
00132         bool isEmpty() { return ( elements.size() == 0 && children.size() == 0 ); }
00133         
00138         bool isLeaf() const { return ( children.size() == 0 ); }
00139         
00146         tree_type getChild( int index );
00147 
00154         stored_type getElement( unsigned int index );
00155         
00156         //virtual:
00157         
00166         virtual tree_type *insert(stored_type newElement) = 0;
00167         
00174         template < class InputIterator >
00175         void insertElements( InputIterator begin,  InputIterator end ) { for ( ; begin!=end; ++begin ) insert( *begin ); }
00176 
00187         virtual tree_type *remove(stored_type element) = 0;
00188         
00198         virtual tree_type *search(stored_type toSearch) = 0;
00199 
00200 #ifdef _INDEX
00201 
00205         class Iterator : public std::iterator<std::forward_iterator_tag, stored_type> {
00206         protected:
00207         
00209                 Tree* _current;
00210                 
00212                 int id_at;
00213 
00214         public:
00215 
00221                 Iterator( Tree< stored_type, tree_type > *current );
00222         
00223         //operators:
00224         
00232                 Iterator& operator=( const Iterator& other ) {
00233                         _current = other._current;
00234                         id_at = other.id_at;
00235                         return (*this);
00236                 }
00237                 
00244                 bool operator==( const Iterator& other ) {
00245                         return ( _current == other._current && id_at = other.id_at);
00246                 }
00247 
00254                 bool operator!=( const Iterator& other ) {
00255                         return ( _current != other._current || id_at != other.id_at );
00256                 }
00257 
00262                 Iterator& operator++() {
00263                         _current = _current->getNext( id_at );
00264                         while ( ! _current->isEmpty() )
00265                                 _current = _current->getNext( id_at );
00266                         if ( _current != 0)
00267                                 id_at = _current->_id;
00268                         else
00269                                 id_at = 0;
00270                         return (*this);
00271                 }
00272 
00279                 stored_type operator*() {
00280                         return ( _current->getElement( id_at ) );
00281                 }
00282                 
00283                 //the below doesnt work..
00284                 //const stored_type* operator->() const {
00285                 //      return _current->getElement( id_at ).operator ->();
00287         };
00288         
00294         Iterator begin() { index(); return Iterator( this ); }
00295         
00302         Iterator end() { return Iterator( 0 ); }
00303 #endif
00304 
00305         //debug funcs
00306         void printTreeToStdOut( const int x, const void* par ) const;
00307 
00308 };
00309 
00310 
00311 //debug funcs:
00312 template< typename stored_type, typename tree_type >
00313 void Tree< stored_type, tree_type >::printTreeToStdOut( const int x, const void* par ) const {
00314         for( int i=0; i<x; i++ )
00315                 cout << " ";
00316         cout << "|";
00317         if ( parent != par )
00318                 cout << "*";
00319         else
00320                 cout << "-";
00321         cout << endl;
00322         if ( isLeaf() )
00323                 return;
00324         for( unsigned int i=0; i<children.size(); i++ ) {
00325                 children[i]->printTreeToStdOut( x+1, this );
00326         }
00327 }
00328 
00329 
00330 #ifdef _INDEX
00331 /*****************************************************
00332  *           ITERATOR SUBCLASS -- PUBLIC             *
00333  *****************************************************/
00334 
00335 template< typename stored_type, typename tree_type >
00336 Tree< stored_type, tree_type >::Iterator::Iterator( Tree< stored_type, tree_type > *current ) {
00337         _current = current;
00338         if ( _current != 0 )
00339                 id_at = current->_id;
00340         else
00341                 id_at = 0;
00342 }
00343 #endif
00344 
00345 
00346 /*****************************************************
00347  *                       PUBLIC                      *
00348  *****************************************************/
00349  
00350         
00351 template< typename stored_type, typename tree_type >
00352 Tree< stored_type, tree_type >::Tree() {
00353 #ifdef _INDEX
00354         _id = 0;
00355 #endif
00356         parent = 0;
00357 #ifdef _INDEX
00358         changed = true;
00359         index();
00360 #endif
00361 }
00362 
00363 template< typename stored_type, typename tree_type >
00364 Tree< stored_type, tree_type >::Tree( tree_type *par ) {
00365 #ifdef _INDEX
00366         _id = 0;
00367 #endif
00368         parent = par;
00369 #ifdef _INDEX
00370         changed = true;
00371         index();
00372 #endif
00373 }
00374 
00375 #ifdef _INDEX
00376 template< typename stored_type, typename tree_type >
00377 void Tree< stored_type, tree_type >::index() {
00378         index(1);
00379         changed = false;
00380 }
00381 #endif
00382 
00383 template< typename stored_type, typename tree_type >
00384 tree_type* Tree< stored_type, tree_type >::getRoot() {
00385         if ( !isRoot() )
00386                 return parent->getRoot();
00387         else
00388                 return this;
00389 }
00390 
00391 template< typename stored_type, typename tree_type >
00392 int Tree< stored_type, tree_type >::getDepth() const {
00393         if ( isRoot() )
00394                 return 0;
00395         else
00396                 return ( 1 + parent->getDepth() );
00397 }
00398 
00399 template< typename stored_type, typename tree_type >
00400 int Tree< stored_type, tree_type >::getHeight() const {
00401         if ( isLeaf() )
00402                 return getDepth();
00403         else {
00404                 int ret = children[0]->getHeight();
00405                 for ( unsigned int i = 1; i<children.size(); i++ ) {
00406                         int curHeight = children[i]->getHeight();
00407                         if ( curHeight > ret )
00408                                 ret = curHeight;
00409                         }
00410                 return ret;
00411         }
00412 }
00413 
00414 template< typename stored_type, typename tree_type >
00415 tree_type Tree< stored_type, tree_type >::getChild( int index ) {
00416         if ( index >= children.size() ) {
00417                 cerr << "Error: index too large in Tree::getChild()!" << endl;
00418                 exit(1);
00419         }
00420         
00421         return children[index];
00422 }
00423 
00424 template< typename stored_type, typename tree_type >
00425 stored_type Tree< stored_type, tree_type >::getElement( unsigned int index ) {
00426         if ( index >= elements.size() ) {
00427                 cerr << "Error: index too large in Tree::getChild()!" << endl;
00428                 exit(1);
00429         }
00430         
00431         return elements[index];
00432 }
00433 
00434 
00435 
00436 /*****************************************************
00437  *                      PROTECTED                    *
00438  *****************************************************/
00439 
00440 #ifdef _INDEX
00441 template< typename stored_type, typename tree_type >
00442 tree_type* Tree< stored_type, tree_type >::getNext( const int id_at ) {
00443         for ( unsigned int i=0; i<children.size(); i++ ) {
00444                 if ( children[i]->_id > id_at )
00445                         return children[_id];
00446         }
00447         if ( isRoot() )
00448                 return 0; //end element
00449         else
00450                 return parent->getNext( id_at );
00451 }
00452 
00453 template< typename stored_type, typename tree_type >
00454 int Tree< stored_type, tree_type >::index( int id_at ) {
00455         _id = id_at;
00456         int max = _id;
00457         for ( unsigned int i=0; i<children.size(); i++ ) {
00458                 max = children[i]->index( max + 1 );
00459         }
00460         return max;
00461 }
00462 #endif
00463 
00464 #endif

Generated on Sat Oct 13 17:34:42 2007 for R-Tree by  doxygen 1.5.2