FastJet 3.0beta1
SearchTree.hh
00001 //STARTHEADER
00002 // $Id: SearchTree.hh 1761 2010-09-16 10:43:18Z soyez $
00003 //
00004 // Copyright (c) 2005-2006, Matteo Cacciari and Gavin Salam
00005 //
00006 //----------------------------------------------------------------------
00007 // This file is part of FastJet.
00008 //
00009 //  FastJet is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU General Public License as published by
00011 //  the Free Software Foundation; either version 2 of the License, or
00012 //  (at your option) any later version.
00013 //
00014 //  The algorithms that underlie FastJet have required considerable
00015 //  development and are described in hep-ph/0512210. If you use
00016 //  FastJet as part of work towards a scientific publication, please
00017 //  include a citation to the FastJet paper.
00018 //
00019 //  FastJet is distributed in the hope that it will be useful,
00020 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 //  GNU General Public License for more details.
00023 //
00024 //  You should have received a copy of the GNU General Public License
00025 //  along with FastJet; if not, write to the Free Software
00026 //  Foundation, Inc.:
00027 //      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 //----------------------------------------------------------------------
00029 //ENDHEADER
00030 
00031 
00032 #ifndef __FASTJET_SEARCHTREE_HH__
00033 #define __FASTJET_SEARCHTREE_HH__
00034 
00035 #include<vector>
00036 #include<cassert>
00037 #include<cstddef>
00038 #include "fastjet/internal/base.hh"
00039 
00040 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00041 
00042 
00043 //======================================================================
00044 /// \if internal_doc
00045 /// @ingroup internal
00046 /// \class SearchTree
00047 /// Efficient class for a search tree
00048 ///
00049 /// This is the class for a search tree designed to be especially efficient
00050 /// when looking for successors and predecessors (to be used in Chan's
00051 /// CP algorithm). It has the requirement that the maximum size of the
00052 /// search tree must be known in advance.
00053 /// \endif
00054 template<class T> class SearchTree {
00055 public:
00056 
00057   class Node;
00058   class circulator;
00059   class const_circulator;
00060 
00061   /// constructor for a search tree from an ordered vector
00062   SearchTree(const std::vector<T> & init);
00063 
00064   /// constructor for a search tree from an ordered vector allowing
00065   /// for future growth beyond the current size, up to max_size
00066   SearchTree(const std::vector<T> & init, unsigned int max_size);
00067 
00068   /// remove the node corresponding to node_index from the search tree
00069   void remove(unsigned node_index);
00070   void remove(typename SearchTree::Node * node);
00071   void remove(typename SearchTree::circulator & circ);
00072 
00073   /// insert the supplied value into the tree and return a pointer to
00074   /// the relevant SearchTreeNode.
00075   //Node * insert(const T & value);
00076   circulator insert(const T & value);
00077 
00078   const Node & operator[](int i) const {return _nodes[i];};
00079 
00080   /// return the number of elements currently in the search tree
00081   unsigned int size() const {return _nodes.size() - _available_nodes.size();}
00082 
00083   /// check that the structure we've obtained makes sense...
00084   void verify_structure();
00085   void verify_structure_linear() const;
00086   void verify_structure_recursive(const Node * , const Node * , const Node * ) const;
00087 
00088   /// print out all elements...
00089   void print_elements();
00090 
00091   // tracking the depth may have some speed overhead -- so leave it 
00092   // out for the time being...
00093 #ifdef TRACK_DEPTH
00094   /// the max depth the tree has ever reached
00095   inline unsigned int max_depth() const {return _max_depth;};
00096 #else
00097   inline unsigned int max_depth() const {return 0;};
00098 #endif
00099 
00100   int loc(const Node * node) const ;
00101 
00102   /// return predecessor by walking through the tree
00103   Node * _find_predecessor(const Node *);
00104   /// return successor by walking through the tree
00105   Node * _find_successor(const Node *);
00106 
00107   const Node & operator[](unsigned int i) const {return _nodes[i];};
00108 
00109   /// return a circulator to some place in the tree (with a circulator
00110   /// you don't care where...)
00111   const_circulator somewhere() const;
00112   circulator somewhere();
00113 
00114 private:
00115   
00116   void _initialize(const std::vector<T> & init);
00117 
00118   std::vector<Node> _nodes;
00119   std::vector<Node *> _available_nodes;
00120   Node * _top_node;
00121   unsigned int _n_removes;
00122 
00123   
00124   /// recursive routine for doing the initial connections assuming things
00125   /// are ordered. Assumes this_one's parent is labelled, and was
00126   /// generated at a scale "scale" -- connections will be carried out
00127   /// including left edge and excluding right edge
00128   void _do_initial_connections(unsigned int this_one, unsigned int scale,
00129                                unsigned int left_edge, unsigned int right_edge,
00130                                unsigned int depth);
00131 
00132   
00133 #ifdef TRACK_DEPTH
00134   unsigned int _max_depth;
00135 #endif
00136 
00137 };
00138 
00139 
00140 //======================================================================
00141 /// \if internal_doc
00142 /// @ingroup internal
00143 /// \class SearchTree::Node
00144 /// A node in the search tree
00145 /// \endif
00146 template<class T> class SearchTree<T>::Node{
00147 public:
00148   Node() {}; /// default constructor
00149   
00150   
00151   /// returns tree if all the tree-related links are set to null for this node
00152   bool treelinks_null() const {
00153     return ((parent==0) && (left==0) && (right==0));};
00154   
00155   /// set all the tree-related links are set to null for this node
00156   inline void nullify_treelinks() {
00157     parent = NULL; 
00158     left   = NULL; 
00159     right  = NULL;
00160   };
00161   
00162   /// if my parent exists, determine whether I am it's left or right
00163   /// node and set the relevant link equal to XX.
00164   void reset_parents_link_to_me(Node * XX);
00165   
00166   T      value;
00167   Node * left;
00168   Node * right;
00169   Node * parent;
00170   Node * successor;
00171   Node * predecessor;
00172 };
00173 
00174 //----------------------------------------------------------------------
00175 template<class T> void SearchTree<T>::Node::reset_parents_link_to_me(typename SearchTree<T>::Node * XX) {
00176   if (parent == NULL) {return;}
00177   if (parent->right == this) {parent->right = XX;}
00178   else {parent->left = XX;}
00179 }
00180 
00181 
00182 
00183 //======================================================================
00184 /// \if internal_doc
00185 /// @ingroup internal
00186 /// \class SearchTree::circulator
00187 /// circulator for the search tree
00188 /// \endif
00189 template<class T> class SearchTree<T>::circulator{
00190 public:
00191 
00192   // so that it can access out _node object;
00193   friend class SearchTree<T>::const_circulator;
00194   friend class SearchTree<T>;
00195 
00196   circulator() : _node(NULL) {}
00197 
00198   circulator(Node * node) : _node(node) {}
00199 
00200   const T * operator->() const {return &(_node->value);}
00201   T * operator->() {return &(_node->value);}
00202   const T & operator*() const {return _node->value;}
00203   T & operator*() {return _node->value;}
00204 
00205   /// prefix increment (structure copied from stl_bvector.h)
00206   circulator & operator++() {
00207     _node = _node->successor; 
00208     return *this;}
00209 
00210   /// postfix increment ["int" argument tells compiler it's postfix]
00211   /// (structure copied from stl_bvector.h)
00212   circulator operator++(int) {
00213     circulator tmp = *this;
00214     _node = _node->successor; 
00215     return tmp;}
00216 
00217   /// prefix decrement (structure copied from stl_bvector.h)
00218   circulator & operator--() {
00219     _node = _node->predecessor; 
00220     return *this;}
00221 
00222   /// postfix decrement ["int" argument tells compiler it's postfix]
00223   /// (structure copied from stl_bvector.h)
00224   circulator operator--(int) {
00225     circulator tmp = *this;
00226     _node = _node->predecessor; 
00227     return tmp;}
00228 
00229   /// return a circulator referring to the next node
00230   circulator next() const {
00231     return circulator(_node->successor);}
00232 
00233   /// return a circulator referring to the previous node
00234   circulator previous() const {
00235     return circulator(_node->predecessor);}
00236 
00237   bool operator!=(const circulator & other) const {return other._node != _node;}
00238   bool operator==(const circulator & other) const {return other._node == _node;}
00239 
00240 private:
00241   Node * _node;
00242 };
00243 
00244 
00245 //======================================================================
00246 /// \if internal_doc
00247 /// @ingroup internal
00248 /// \class SearchTree::const_circulator
00249 /// A const_circulator for the search tree
00250 /// \endif
00251 template<class T> class SearchTree<T>::const_circulator{
00252 public:
00253 
00254   const_circulator() : _node(NULL) {}
00255 
00256   const_circulator(const Node * node) : _node(node) {}
00257   const_circulator(const circulator & circ) :_node(circ._node) {}
00258 
00259   const T * operator->() {return &(_node->value);}
00260   const T & operator*() const {return _node->value;}
00261 
00262   /// prefix increment (structure copied from stl_bvector.h)
00263   const_circulator & operator++() {
00264     _node = _node->successor; 
00265     return *this;}
00266 
00267   /// postfix increment ["int" argument tells compiler it's postfix]
00268   /// (structure copied from stl_bvector.h)
00269   const_circulator operator++(int) {
00270     const_circulator tmp = *this;
00271     _node = _node->successor; 
00272     return tmp;}
00273 
00274 
00275   /// prefix decrement (structure copied from stl_bvector.h)
00276   const_circulator & operator--() {
00277     _node = _node->predecessor; 
00278     return *this;}
00279 
00280   /// postfix decrement ["int" argument tells compiler it's postfix]
00281   /// (structure copied from stl_bvector.h)
00282   const_circulator operator--(int) {
00283     const_circulator tmp = *this;
00284     _node = _node->predecessor; 
00285     return tmp;}
00286 
00287   /// return a circulator referring to the next node
00288   const_circulator next() const {
00289     return const_circulator(_node->successor);}
00290 
00291   /// return a circulator referring to the previous node
00292   const_circulator previous() const {
00293     return const_circulator(_node->predecessor);}
00294 
00295 
00296 
00297   bool operator!=(const const_circulator & other) const {return other._node != _node;}
00298   bool operator==(const const_circulator & other) const {return other._node == _node;}
00299 
00300 private:
00301   const Node * _node;
00302 };
00303 
00304 
00305 
00306 
00307 //----------------------------------------------------------------------
00308 /// initialise from a sorted initial array allowing for a larger
00309 /// maximum size of the array...
00310 template<class T> SearchTree<T>::SearchTree(const std::vector<T> & init,
00311                                             unsigned int max_size) :
00312   _nodes(max_size) {
00313 
00314   _available_nodes.reserve(max_size);
00315   _available_nodes.resize(max_size - init.size());
00316   for (unsigned int i = init.size(); i < max_size; i++) {
00317     _available_nodes[i-init.size()] = &(_nodes[i]);
00318   }
00319 
00320   _initialize(init);
00321 }
00322 
00323 //----------------------------------------------------------------------
00324 /// initialise from a sorted initial array
00325 template<class T> SearchTree<T>::SearchTree(const std::vector<T> & init) :
00326   _nodes(init.size()), _available_nodes(0) {
00327 
00328   // reserve space for the list of available nodes
00329   _available_nodes.reserve(init.size());
00330   _initialize(init);
00331 }
00332 
00333 //----------------------------------------------------------------------
00334 /// do the actual hard work of initialization
00335 template<class T> void SearchTree<T>::_initialize(const std::vector<T> & init) {
00336 
00337   _n_removes = 0;
00338   unsigned n = init.size();
00339   assert(n>=1);
00340 
00341   // reserve space for the list of available nodes
00342   //_available_nodes.reserve();
00343 
00344 #ifdef TRACK_DEPTH
00345   _max_depth     = 0;
00346 #endif
00347 
00348 
00349   // validate the input
00350   for (unsigned int i = 1; i<n; i++) {
00351     assert(!(init[i] < init[i-1]));
00352   }
00353   
00354   // now initialise the vector; link neighbours in the sequence
00355   for(unsigned int i = 0; i < n; i++) {
00356     _nodes[i].value = init[i];
00357     _nodes[i].predecessor = (& (_nodes[i])) - 1;
00358     _nodes[i].successor   = (& (_nodes[i])) + 1;
00359     _nodes[i].nullify_treelinks();
00360   }
00361   // make a loop structure so that we can circulate...
00362   _nodes[0].predecessor = (& (_nodes[n-1]));
00363   _nodes[n-1].successor = (& (_nodes[0]));
00364 
00365   // now label the rest of the nodes
00366   unsigned int scale = (n+1)/2;
00367   unsigned int top   = std::min(n-1,scale);
00368   _nodes[top].parent = NULL;
00369   _top_node = &(_nodes[top]);
00370   _do_initial_connections(top, scale, 0, n, 0);
00371 
00372   // make sure things are sensible...
00373   //verify_structure();
00374 }
00375 
00376 
00377 
00378 //----------------------------------------------------------------------
00379 template<class T> inline  int SearchTree<T>::loc(const Node * node) const {return node == NULL? 
00380       -999 : node - &(_nodes[0]);}
00381 
00382 
00383 //----------------------------------------------------------------------
00384 /// Recursive creation of connections, assuming the _nodes vector is
00385 /// completely filled and ordered
00386 template<class T> void SearchTree<T>::_do_initial_connections(
00387                                          unsigned int this_one, 
00388                                          unsigned int scale,
00389                                          unsigned int left_edge,
00390                                          unsigned int right_edge,
00391                                          unsigned int depth
00392                                          ) {
00393 
00394 #ifdef TRACK_DEPTH
00395   // keep track of tree depth for checking things stay reasonable...
00396   _max_depth = max(depth, _max_depth);
00397 #endif
00398 
00399   //std::cout << this_one << " "<< scale<< std::endl;
00400   unsigned int ref_new_scale = (scale+1)/2;
00401 
00402   // work through children to our left
00403   unsigned new_scale = ref_new_scale;
00404   bool     did_child  = false;
00405   while(true) {
00406     int left = this_one - new_scale; // be careful here to use signed int...
00407     // if there is something unitialised to our left, link to it
00408     if (left >= static_cast<int>(left_edge) 
00409                         && _nodes[left].treelinks_null() ) {
00410       _nodes[left].parent = &(_nodes[this_one]);
00411       _nodes[this_one].left = &(_nodes[left]);
00412       // create connections between left_edge and this_one
00413       _do_initial_connections(left, new_scale, left_edge, this_one, depth+1);
00414       did_child = true;
00415       break;
00416     }
00417     // reduce the scale so as to try again
00418     unsigned int old_new_scale = new_scale;
00419     new_scale = (old_new_scale + 1)/2;
00420     // unless we've reached end of tree
00421     if (new_scale == old_new_scale) break;
00422   }
00423   if (!did_child) {_nodes[this_one].left = NULL;}
00424 
00425 
00426   // work through children to our right
00427   new_scale = ref_new_scale;
00428   did_child  = false;
00429   while(true) {
00430     unsigned int right = this_one + new_scale;
00431     if (right < right_edge  && _nodes[right].treelinks_null()) {
00432       _nodes[right].parent = &(_nodes[this_one]);
00433       _nodes[this_one].right = &(_nodes[right]);
00434       // create connections between this_one+1 and right_edge
00435       _do_initial_connections(right, new_scale, this_one+1,right_edge,depth+1);
00436       did_child = true;
00437       break;
00438     }
00439     // reduce the scale so as to try again
00440     unsigned int old_new_scale = new_scale;
00441     new_scale = (old_new_scale + 1)/2;
00442     // unless we've reached end of tree
00443     if (new_scale == old_new_scale) break;
00444   }
00445   if (!did_child) {_nodes[this_one].right = NULL;}
00446 
00447 }
00448 
00449 
00450 
00451 //----------------------------------------------------------------------
00452 template<class T> void SearchTree<T>::remove(unsigned int node_index) {
00453   remove(&(_nodes[node_index]));
00454 }
00455 
00456 //----------------------------------------------------------------------
00457 template<class T> void SearchTree<T>::remove(circulator & circ) {
00458   remove(circ._node);
00459 }
00460 
00461 //----------------------------------------------------------------------
00462 // Useful reference for this:
00463 //   http://en.wikipedia.org/wiki/Binary_search_tree#Deletion
00464 template<class T> void SearchTree<T>::remove(typename SearchTree<T>::Node * node) {
00465 
00466   // we don't remove things from the tree if we've reached the last
00467   // elements... (is this wise?)
00468   assert(size() > 1); // switch this to throw...?
00469   assert(!node->treelinks_null());
00470 
00471   // deal with relinking predecessor and successor
00472   node->predecessor->successor = node->successor;
00473   node->successor->predecessor = node->predecessor;
00474 
00475   if (node->left == NULL && node->right == NULL) {
00476     // node has no children, so remove it by nullifying the pointer 
00477     // from the parent
00478     node->reset_parents_link_to_me(NULL); 
00479 
00480   } else if (node->left != NULL && node->right == NULL){
00481     // make parent point to my child
00482     node->reset_parents_link_to_me(node->left);
00483     // and child to parent
00484     node->left->parent = node->parent;         
00485     // sort out the top node...
00486     if (_top_node == node) {_top_node = node->left;}
00487 
00488   } else if (node->left == NULL && node->right != NULL){
00489     // make parent point to my child
00490     node->reset_parents_link_to_me(node->right);
00491     // and child to parent
00492     node->right->parent = node->parent;   
00493     // sort out the top node...
00494     if (_top_node == node) {_top_node = node->right;}
00495 
00496   } else {
00497     // we have two children; we will put a replacement in our place
00498     Node * replacement;
00499     //SearchTree<T>::Node * replacements_child;
00500     // chose predecessor or successor (one, then other, then first, etc...)
00501     bool use_predecessor = (_n_removes % 2 == 1);
00502     if (use_predecessor) {
00503       // Option 1: put predecessor in our place, and have its parent
00504       // point to its left child (as a predecessor it has no right child)
00505       replacement = node->predecessor;
00506       assert(replacement->right == NULL); // guaranteed if it's our predecessor
00507       // we have to be careful of replacing certain links when the 
00508       // replacement is this node's child
00509       if (replacement != node->left) {
00510         if (replacement->left != NULL) {
00511           replacement->left->parent = replacement->parent;}
00512         replacement->reset_parents_link_to_me(replacement->left);
00513         replacement->left   = node->left;
00514       }
00515       replacement->parent = node->parent;
00516       replacement->right  = node->right;
00517     } else {
00518       // Option 2: put successor in our place, and have its parent
00519       // point to its right child (as a successor it has no left child)
00520       replacement = node->successor;
00521       assert(replacement->left == NULL); // guaranteed if it's our successor
00522       if (replacement != node->right) {
00523         if (replacement->right != NULL) {
00524           replacement->right->parent = replacement->parent;}
00525         replacement->reset_parents_link_to_me(replacement->right);
00526         replacement->right  = node->right;
00527       }
00528       replacement->parent = node->parent;
00529       replacement->left   = node->left;
00530     }
00531     node->reset_parents_link_to_me(replacement);
00532 
00533     // make sure node's original children now point to the replacement
00534     if (node->left  != replacement) {node->left->parent  = replacement;}
00535     if (node->right != replacement) {node->right->parent = replacement;}
00536 
00537     // sort out the top node...
00538     if (_top_node == node) {_top_node = replacement;}
00539   }
00540 
00541   // make sure we leave something nice and clean...
00542   node->nullify_treelinks();
00543   node->predecessor = NULL;
00544   node->successor   = NULL;
00545 
00546   // for bookkeeping (and choosing whether to use pred. or succ.)
00547   _n_removes++;
00548   // for when we next need access to a free node...
00549   _available_nodes.push_back(node);
00550 }
00551 
00552 
00553 //----------------------------------------------------------------------
00554 //template<class T> typename SearchTree<T>::Node * SearchTree<T>::insert(const T & value) {
00555 
00556 //----------------------------------------------------------------------
00557 template<class T> typename SearchTree<T>::circulator SearchTree<T>::insert(const T & value) {
00558   // make sure we don't exceed allowed number of nodes...
00559   assert(_available_nodes.size() > 0);
00560 
00561   Node * node = _available_nodes.back();
00562   _available_nodes.pop_back();
00563   node->value = value;
00564 
00565   Node * location = _top_node;
00566   Node * old_location = NULL;
00567   bool             on_left = true; // (init not needed -- but soothes g++4)
00568   // work through tree until we reach its end
00569 #ifdef TRACK_DEPTH
00570   unsigned int depth = 0;
00571 #endif
00572   while(location != NULL) {
00573 #ifdef TRACK_DEPTH
00574     depth++;
00575 #endif
00576     old_location = location;
00577     on_left = value < location->value;
00578     if (on_left) {location = location->left;}
00579     else {location = location->right;}
00580   }
00581 #ifdef TRACK_DEPTH
00582   _max_depth = max(depth, _max_depth);
00583 #endif
00584   // now create tree links
00585   node->parent = old_location;
00586   if (on_left) {node->parent->left = node;} 
00587   else {node->parent->right = node;}
00588   node->left = NULL;
00589   node->right = NULL;
00590   // and create predecessor / successor links
00591   node->predecessor = _find_predecessor(node);
00592   if (node->predecessor != NULL) {
00593     // it exists, so make use of its info (will include a cyclic case,
00594     // when successor is round the bend)
00595     node->successor = node->predecessor->successor;
00596     node->predecessor->successor = node;
00597     node->successor->predecessor = node;
00598   } else {
00599     // deal with case when we are left-most edge of tree (then successor
00600     // will exist...)
00601     node->successor = _find_successor(node);
00602     assert(node->successor != NULL); // can only happen if we're sole element 
00603                                      // (but not allowed, since tree size>=1)
00604     node->predecessor = node->successor->predecessor;
00605     node->successor->predecessor = node;
00606     node->predecessor->successor = node;
00607   }
00608 
00609   return circulator(node);
00610 }
00611 
00612 
00613 //----------------------------------------------------------------------
00614 template<class T> void SearchTree<T>::verify_structure() {
00615   
00616   // do a check running through all elements
00617   verify_structure_linear();
00618 
00619   // do a recursive check down tree from top
00620 
00621   // first establish the extremities
00622   const Node * left_limit = _top_node;
00623   while (left_limit->left != NULL) {left_limit = left_limit->left;}
00624   const Node * right_limit = _top_node;
00625   while (right_limit->right != NULL) {right_limit = right_limit->right;}
00626 
00627   // then actually do recursion
00628   verify_structure_recursive(_top_node, left_limit, right_limit);
00629 }
00630 
00631 
00632 //----------------------------------------------------------------------
00633 template<class T> void SearchTree<T>::verify_structure_recursive(
00634                       const typename SearchTree<T>::Node * element, 
00635                       const typename SearchTree<T>::Node * left_limit,
00636                       const typename SearchTree<T>::Node * right_limit)  const {
00637 
00638   assert(!(element->value < left_limit->value));
00639   assert(!(right_limit->value < element->value));
00640 
00641   const Node * left = element->left;
00642   if (left != NULL) {
00643     assert(!(element->value < left->value));
00644     if (left != left_limit) {
00645       // recurse down the tree with this element as the right-hand limit
00646       verify_structure_recursive(left, left_limit, element);}
00647   }
00648   
00649   const Node * right = element->right;
00650   if (right != NULL) {
00651     assert(!(right->value < element->value));
00652     if (right != right_limit) {
00653       // recurse down the tree with this element as the left-hand limit
00654       verify_structure_recursive(right, element, right_limit);}
00655   }
00656 }
00657 
00658 //----------------------------------------------------------------------
00659 template<class T> void SearchTree<T>::verify_structure_linear() const {
00660 
00661   //print_elements();
00662 
00663   unsigned n_top = 0;
00664   unsigned n_null = 0;
00665   for(unsigned i = 0; i < _nodes.size(); i++) {
00666     const typename SearchTree<T>::Node * node = &(_nodes[i]);
00667     // make sure node is defined
00668     if (node->treelinks_null()) {n_null++; continue;}
00669 
00670     // make sure of the number of "top" nodes 
00671     if (node->parent == NULL) {
00672       n_top++;
00673       //assert(node->left != NULL);
00674       //assert(node->right != NULL);
00675     } else {
00676       // make sure that I am a child of my parent...
00677       //assert((node->parent->left == node) || (node->parent->right == node));
00678       assert((node->parent->left == node) ^ (node->parent->right == node));
00679     }
00680 
00681     // when there is a left child make sure it's value is ordered
00682     // (note use of !(b<a), to allow for a<=b while using just the <
00683     // operator)
00684     if (node->left != NULL) {
00685       assert(!(node->value < node->left->value ));}
00686 
00687     // when there is a right child make sure it's value is ordered
00688     if (node->right != NULL) {
00689       assert(!(node->right->value < node->value ));}
00690 
00691   }
00692   assert(n_top == 1 || (n_top == 0 && size() <= 1) );
00693   assert(n_null == _available_nodes.size() ||
00694          (n_null == _available_nodes.size() + 1 && size() == 1));
00695 }
00696 
00697 
00698 //----------------------------------------------------------------------
00699 template<class T> typename SearchTree<T>::Node * SearchTree<T>::_find_predecessor(const typename SearchTree<T>::Node * node) {
00700 
00701   typename SearchTree<T>::Node * newnode;
00702   if (node->left != NULL) {
00703     // go down left, and then down right as far as possible.
00704     newnode = node->left;
00705     while(newnode->right != NULL) {newnode = newnode->right;}
00706     return newnode;
00707   } else {
00708     const typename SearchTree<T>::Node * lastnode = node;
00709     newnode = node->parent;
00710     // go up the tree as long as we're going right (when we go left then
00711     // we've found something smaller, so stop)
00712     while(newnode != NULL) {
00713       if (newnode->right == lastnode) {return newnode;}
00714       lastnode = newnode;
00715       newnode = newnode->parent;
00716     }
00717     return newnode;
00718   }
00719 }
00720 
00721 
00722 //----------------------------------------------------------------------
00723 template<class T> typename SearchTree<T>::Node * SearchTree<T>::_find_successor(const typename SearchTree<T>::Node * node) {
00724 
00725   typename SearchTree<T>::Node * newnode;
00726   if (node->right != NULL) {
00727     // go down right, and then down left as far as possible.
00728     newnode = node->right;
00729     while(newnode->left != NULL) {newnode = newnode->left;}
00730     return newnode;
00731   } else {
00732     const typename SearchTree<T>::Node * lastnode = node;
00733     newnode = node->parent;
00734     // go up the tree as long as we're going left (when we go right then
00735     // we've found something larger, so stop)
00736     while(newnode != NULL) {
00737       if (newnode->left == lastnode) {return newnode;}
00738       lastnode = newnode;
00739       newnode = newnode->parent;
00740     }
00741     return newnode;
00742   }
00743 }
00744 
00745 
00746 //----------------------------------------------------------------------
00747 // print out all the elements for visual checking...
00748 template<class T> void SearchTree<T>::print_elements() {
00749   typename SearchTree<T>::Node * base_node = &(_nodes[0]);
00750   typename SearchTree<T>::Node * node = base_node;
00751   
00752   int n = _nodes.size();
00753   for(; node - base_node < n ; node++) {
00754     printf("%4d parent:%4d left:%4d right:%4d pred:%4d succ:%4d value:%10.6f\n",loc(node), loc(node->parent), loc(node->left), loc(node->right), loc(node->predecessor),loc(node->successor),node->value);
00755   }
00756 }
00757 
00758 //----------------------------------------------------------------------
00759 template<class T> typename SearchTree<T>::circulator SearchTree<T>::somewhere() {
00760   return circulator(_top_node);
00761 }
00762 
00763 
00764 //----------------------------------------------------------------------
00765 template<class T> typename SearchTree<T>::const_circulator SearchTree<T>::somewhere() const {
00766   return const_circulator(_top_node);
00767 }
00768 
00769 
00770 FASTJET_END_NAMESPACE
00771 
00772 #endif // __FASTJET_SEARCHTREE_HH__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends