AVLTree.cpp

Go to the documentation of this file.
00001 /** @file AVLTree.cpp
00002 * @author Gabor Madl
00003 * @date Created 10/2006
00004 * @brief AVL-tree template class.
00005 *
00006 *
00007 * =================================================================
00008 * DREAM License v2.0
00009 * 
00010 * DREAM - Distributed Real-time Embedded Analysis Method
00011 * http://dre.sourceforge.net.
00012 * Copyright (c) 2005-2007 Gabor Madl, All Rights Reserved.
00013 * 
00014 * This file is part of DREAM.
00015 * 
00016 * DREAM is free software; you can redistribute it and/or modify it
00017 * under the terms of the GNU General Public License version 2 as
00018 * published by the Free Software Foundation. No future versions of
00019 * the GPL license may be automatically applied to DREAM. It is in
00020 * the sole discretion of the copyright holder to determine whether
00021 * DREAM may be released under a different license or terms. There
00022 * are no restrictions on the use of DREAM for any purpose.
00023 * 
00024 * DREAM is distributed in the hope that it will be useful,
00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00027 * GNU General Public License for more details.
00028 * 
00029 * You should have received a copy of the GNU General Public License
00030 * along with this program; if not, write to the Free Software
00031 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00032 * MA 02110-1301, USA.
00033 * 
00034 * By submitting comments, suggestions, code, code snippets,
00035 * techniques (including that of usage), and algorithms, submitters
00036 * acknowledge that they have the right to do so, that any such
00037 * submissions are given freely and unreservedly, and that they
00038 * waive any claims to copyright or ownership. In addition,
00039 * submitters acknowledge that any such submission might become
00040 * part of the copyright maintained on the overall body of code,
00041 * which comprises DREAM. By making a submission, submitter agrees
00042 * to these terms. Furthermore, submitters acknowledge that the
00043 * incorporation or modification of such submissions is entirely
00044 * at the discretion of the moderators of the DREAM project.
00045 * 
00046 * DREAM links to the Libxml2 third party library. Please see 
00047 * COPYING-libxml for the copyright information of Libxml2.
00048 * =================================================================
00049 */
00050 
00051 #ifndef DREAM_AVLTREECPP
00052 #define DREAM_AVLTREECPP
00053 
00054 #include "AVLTree.h"
00055 
00056 namespace DREAM
00057 {
00058 
00059 /////////////////////////////////////////////////////////////////////////////
00060 //
00061 // AVLTreeNode
00062 //
00063 /////////////////////////////////////////////////////////////////////////////
00064 
00065 template <class key_type, class item_type>
00066 AVLTreeNode <key_type, item_type>::AVLTreeNode (key_type key, item_type item, AVLTreeNode <key_type, item_type>* parent) 
00067 : first (key), second (item), left_ (NULL), right_ (NULL), parent_ (parent), depth_ (1), depth_count_ (0)
00068 {
00069 };
00070 
00071 template <class key_type, class item_type>
00072 AVLTreeNode <key_type, item_type>::~AVLTreeNode ()
00073 {
00074         if (left_)
00075                 delete left_;
00076         if (right_)
00077                 delete right_;
00078 }
00079 
00080 template <class key_type, class item_type>
00081 void AVLTreeNode <key_type, item_type>::calculate_depth ()
00082 {
00083         uint left_depth = 0, right_depth = 0;
00084 
00085         if (left_)
00086                 left_depth = left_->depth_;
00087 
00088         if (right_)
00089                 right_depth = right_->depth_;
00090 
00091         if (left_depth == right_depth)
00092         {
00093                 if (left_depth == 0)
00094                         depth_count_ = 1;
00095                 else
00096                         depth_count_ = left_->depth_count_ + right_->depth_count_;
00097 
00098                 depth_ = left_depth + 1;
00099         }
00100         else if (left_depth > right_depth)
00101         {
00102                 depth_ = left_depth + 1;
00103                 depth_count_ = left_->depth_count_;
00104         }
00105         else
00106         {
00107                 depth_ = right_depth + 1;
00108                 depth_count_ = right_->depth_count_;
00109         }
00110 
00111         if (parent_)
00112                 parent_->calculate_depth ();
00113 }
00114 
00115 template <class key_type, class item_type>
00116 inline uint AVLTreeNode <key_type, item_type>::depth ()
00117 {
00118         return depth_;
00119 }
00120 
00121 template <class key_type, class item_type>
00122 AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::iter_next ()
00123 {
00124         AVLTreeNode <key_type, item_type>* node_ptr;
00125 
00126         // Let's find the next largest element
00127         if (right_)
00128         {
00129                 node_ptr = right_;
00130 
00131                 // find leftmost child
00132                 while (node_ptr->left_)
00133                         node_ptr = node_ptr->left_;
00134 
00135                 return node_ptr;
00136         }
00137         else
00138         {
00139                 if (parent_)
00140                         node_ptr = parent_;
00141                 else
00142                         return NULL;
00143 
00144                 while ((node_ptr->parent_) && (node_ptr->first < first))
00145                         node_ptr = node_ptr->parent_;
00146 
00147                 return ((!node_ptr->parent_) && (node_ptr->first < first)) ? NULL : node_ptr;
00148         }
00149 }
00150 
00151 template <class key_type, class item_type>
00152 AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::iter_previous ()
00153 {
00154         AVLTreeNode <key_type, item_type>* node_ptr;
00155 
00156         // Let's find the next smallest element
00157         if (left_)
00158         {
00159                 node_ptr = left_;
00160 
00161                 // find rightmost child
00162                 while (node_ptr->right_)
00163                         node_ptr = node_ptr->right_;
00164 
00165                 return node_ptr;
00166         }
00167         else
00168         {
00169                 if (parent_)
00170                         node_ptr = parent_;
00171                 else
00172                         return NULL;
00173 
00174                 while ((node_ptr->parent_) && (node_ptr->first > first))
00175                         node_ptr = node_ptr->parent_;
00176 
00177                 return ((!node_ptr->parent_) && (node_ptr->first > first)) ? NULL : node_ptr;
00178         }
00179 }
00180 
00181 template <class key_type, class item_type>
00182 const AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::iter_next () const
00183 {
00184         const AVLTreeNode <key_type, item_type>* node_ptr;
00185 
00186         // Let's find the next largest element
00187         if (right_)
00188         {
00189                 node_ptr = right_;
00190 
00191                 // find leftmost child
00192                 while (node_ptr->left_)
00193                         node_ptr = node_ptr->left_;
00194 
00195                 return node_ptr;
00196         }
00197         else
00198         {
00199                 if (parent_)
00200                         node_ptr = parent_;
00201                 else
00202                         return NULL;
00203 
00204                 while ((node_ptr->parent_) && (node_ptr->first < first))
00205                         node_ptr = node_ptr->parent_;
00206 
00207                 return ((!node_ptr->parent_) && (node_ptr->first < first)) ? NULL : node_ptr;
00208         }
00209 }
00210 
00211 template <class key_type, class item_type>
00212 const AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::iter_previous () const
00213 {
00214         const AVLTreeNode <key_type, item_type>* node_ptr;
00215 
00216         // Let's find the next smallest element
00217         if (left_)
00218         {
00219                 node_ptr = left_;
00220 
00221                 // find rightmost child
00222                 while (node_ptr->right_)
00223                         node_ptr = node_ptr->right_;
00224 
00225                 return node_ptr;
00226         }
00227         else
00228         {
00229                 if (parent_)
00230                         node_ptr = parent_;
00231                 else
00232                         return NULL;
00233 
00234                 while ((node_ptr->parent_) && (node_ptr->first > first))
00235                         node_ptr = node_ptr->parent_;
00236 
00237                 return ((!node_ptr->parent_) && (node_ptr->first > first)) ? NULL : node_ptr;
00238         }
00239 }
00240 
00241 template <class key_type, class item_type>
00242 inline AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::left ()
00243 {
00244         return left_;
00245 }
00246 
00247 template <class key_type, class item_type>
00248 inline const AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::left () const
00249 {
00250         return left_;
00251 }
00252 
00253 template <class key_type, class item_type>
00254 inline void AVLTreeNode <key_type, item_type>::left (AVLTreeNode <key_type, item_type>* node_ptr)
00255 {
00256         left_ = node_ptr;
00257 }
00258 
00259 template <class key_type, class item_type>
00260 inline AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::right ()
00261 {
00262         return right_;
00263 }
00264 
00265 template <class key_type, class item_type>
00266 inline const AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::right () const
00267 {
00268         return right_;
00269 }
00270 
00271 template <class key_type, class item_type>
00272 inline void AVLTreeNode <key_type, item_type>::right (AVLTreeNode <key_type, item_type>* node_ptr)
00273 {
00274         right_ = node_ptr;
00275 }
00276 
00277 template <class key_type, class item_type>
00278 inline AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::parent ()
00279 {
00280         return parent_;
00281 }
00282 
00283 template <class key_type, class item_type>
00284 inline const AVLTreeNode <key_type, item_type>* AVLTreeNode <key_type, item_type>::parent () const
00285 {
00286         return parent_;
00287 }
00288 
00289 template <class key_type, class item_type>
00290 inline void AVLTreeNode <key_type, item_type>::parent (AVLTreeNode <key_type, item_type>* node_ptr)
00291 {
00292         parent_ = node_ptr;
00293 }
00294 
00295 template <class key_type, class item_type>
00296 AVLTreeNode <key_type, item_type>& AVLTreeNode <key_type, item_type>::operator= (AVLTreeNode <key_type, item_type>& node)
00297 {
00298         first = node.first;
00299         second = node.second;
00300         left_ = node.left_;
00301         right_ = node.right_;
00302         parent_ = node.parent_;
00303         return *this;
00304 }
00305 
00306 /////////////////////////////////////////////////////////////////////////////
00307 //
00308 // AVLTree
00309 //
00310 /////////////////////////////////////////////////////////////////////////////
00311 
00312 template <class key_type, class item_type>
00313 AVLTree <key_type, item_type>::AVLTree ()
00314 : root_ptr_ (NULL), last_ptr_ (NULL), size_ (0) {};
00315 
00316 template <class key_type, class item_type>
00317 AVLTree <key_type, item_type>::~AVLTree ()
00318 {
00319         if (root_ptr_)
00320                 delete root_ptr_;
00321 }
00322 
00323 template <class key_type, class item_type>
00324 void AVLTree <key_type, item_type>::assign (const AVLTreeNode <key_type, item_type>* begin_node, const AVLTreeNode <key_type, item_type>* end_node)
00325 {
00326         Const_Iterator <AVLTreeNode <key_type, item_type> > iter, begin_iter, end_iter;
00327         begin_iter = begin_node;
00328         end_iter = end_node;
00329 
00330         for (iter = begin_iter; iter != end_iter; iter++)
00331                 insert (iter->first, iter->second);
00332 }
00333 
00334 
00335 template <class key_type, class item_type>
00336 void AVLTree <key_type, item_type>::assign (Iterator <AVLTreeNode <key_type, item_type> >& begin_iter, Iterator <AVLTreeNode <key_type, item_type> >& end_iter)
00337 {
00338         Iterator <AVLTreeNode <key_type, item_type> > iter;
00339 
00340         for (iter = begin_iter; iter != end_iter; iter++)
00341                 insert (iter->first, iter->second);
00342 }
00343 
00344 template <class key_type, class item_type>
00345 void AVLTree <key_type, item_type>::assign (Const_Iterator <AVLTreeNode <key_type, item_type> >& begin_iter, Const_Iterator <AVLTreeNode <key_type, item_type> >& end_iter)
00346 {
00347         Const_Iterator <AVLTreeNode <key_type, item_type> > iter;
00348 
00349         for (iter = begin_iter; iter != end_iter; iter++)
00350                 insert (iter->first, iter->second);
00351 }
00352 
00353 template <class key_type, class item_type>
00354 void AVLTree <key_type, item_type>::balance (AVLTreeNode <key_type, item_type>* node_ptr)
00355 {
00356         uint left = 0, right = 0;
00357         
00358         if (node_ptr->left_)
00359                 left = node_ptr->left_->depth_;
00360         if (node_ptr->right_)
00361                 right = node_ptr->right_->depth_;
00362 
00363         if (left > right + 1)
00364         {
00365                 // Left subtree is too deep, balance right
00366                 balance_right (node_ptr);
00367         }
00368         else if (right > left + 1)
00369         {
00370                 // Right subtree is too deep, balance left
00371                 balance_left (node_ptr);
00372         }
00373 
00374         if (node_ptr->parent_)
00375                 balance (node_ptr->parent_);
00376 }
00377 
00378 template <class key_type, class item_type>
00379 void AVLTree <key_type, item_type>::balance_left (AVLTreeNode <key_type, item_type>* node_ptr)
00380 {
00381         AVLTreeNode <key_type, item_type>* right_subtree = node_ptr->right_;
00382         AVLTreeNode <key_type, item_type>* middle_subtree = right_subtree->left_;
00383         AVLTreeNode <key_type, item_type>* parent_ptr = node_ptr->parent_;
00384 
00385         uint middle_depth = 0, right_depth = 0;
00386         if (middle_subtree)
00387                 middle_depth = middle_subtree->depth_;
00388         if (right_subtree->right_)
00389                 right_depth = right_subtree->right_->depth_;
00390 
00391         if (right_depth > middle_depth)
00392         {
00393                 // Simple rotation
00394                 node_ptr->right_ = middle_subtree;
00395                 if (middle_subtree)
00396                         middle_subtree->parent_ = node_ptr;
00397 
00398                 right_subtree->left_ = node_ptr;
00399                 node_ptr->parent_ = right_subtree;
00400 
00401                 if (parent_ptr)
00402                 {
00403                         // Non-root node
00404                         if (parent_ptr->left_ == node_ptr)
00405                         {
00406                                 // Node is the left child of its parent
00407                                 parent_ptr->left_ = right_subtree;
00408                         }
00409                         else
00410                         {
00411                                 // Node is the right child of its parent
00412                                 parent_ptr->right_ = right_subtree;
00413                         }
00414                         right_subtree->parent_ = parent_ptr;
00415                 }
00416                 else
00417                 {
00418                         // Root node
00419                         root_ptr_ = right_subtree;
00420                         right_subtree->parent_ = NULL;
00421                 }
00422 
00423                 node_ptr->calculate_depth ();
00424                 balance (node_ptr);
00425         }
00426         else
00427         {
00428                 // Middle tree has to be the new root, multiple rotations
00429                 AVLTreeNode <key_type, item_type>* middle_left = middle_subtree->left_;
00430                 AVLTreeNode <key_type, item_type>* middle_right = middle_subtree->right_;
00431 
00432                 // Left side of the tree
00433                 middle_subtree->left_ = node_ptr;
00434                 node_ptr->parent_ = middle_subtree;
00435 
00436                 node_ptr->right_ = middle_left;
00437                 if (middle_left)
00438                         middle_left->parent_ = node_ptr;
00439 
00440                 // Right side of the tree
00441                 middle_subtree->right_ = right_subtree;
00442                 if (right_subtree)
00443                         right_subtree->parent_ = middle_subtree;
00444 
00445                 if (right_subtree)
00446                         right_subtree->left_ = middle_right;
00447                 if (middle_right)
00448                         middle_right->parent_ = right_subtree;
00449 
00450                 // Reconstruct tree
00451                 if (parent_ptr)
00452                 {
00453                         // Non-root node
00454                         if (parent_ptr->left_ == node_ptr)
00455                         {
00456                                 // Node is the left child of its parent
00457                                 parent_ptr->left_ = middle_subtree;
00458                         }
00459                         else
00460                         {
00461                                 // Node is the right child of its parent
00462                                 parent_ptr->right_ = middle_subtree;
00463                         }
00464                         middle_subtree->parent_ = parent_ptr;
00465                 }
00466                 else
00467                 {
00468                         // Root node
00469                         root_ptr_ = middle_subtree;
00470                         middle_subtree->parent_ = NULL;
00471                 }
00472 
00473                 if (right_subtree)
00474                         right_subtree->calculate_depth ();
00475                 node_ptr->calculate_depth ();
00476 
00477                 if (right_subtree)
00478                         balance (right_subtree);
00479                 balance (node_ptr);
00480         }
00481 }
00482 
00483 template <class key_type, class item_type>
00484 void AVLTree <key_type, item_type>::balance_right (AVLTreeNode <key_type, item_type>* node_ptr)
00485 {
00486         AVLTreeNode <key_type, item_type>* left_subtree = node_ptr->left_;
00487         AVLTreeNode <key_type, item_type>* middle_subtree = left_subtree->right_;
00488         AVLTreeNode <key_type, item_type>* parent_ptr = node_ptr->parent_;
00489 
00490         uint middle_depth = 0, left_depth = 0;
00491         if (middle_subtree)
00492                 middle_depth = middle_subtree->depth_;
00493         if (left_subtree->left_)
00494                 left_depth = left_subtree->left_->depth_;
00495 
00496         if (left_depth > middle_depth)
00497         {
00498                 // Simple rotation
00499                 node_ptr->left_ = middle_subtree;
00500                 if (middle_subtree)
00501                         middle_subtree->parent_ = node_ptr;
00502 
00503                 left_subtree->right_ = node_ptr;
00504                 node_ptr->parent_ = left_subtree;
00505 
00506                 if (parent_ptr)
00507                 {
00508                         // Non-root node
00509                         if (parent_ptr->left_ == node_ptr)
00510                         {
00511                                 // Node is the left child of its parent
00512                                 parent_ptr->left_ = left_subtree;
00513                         }
00514                         else
00515                         {
00516                                 // Node is the right child of its parent
00517                                 parent_ptr->right_ = left_subtree;
00518                         }
00519                         left_subtree->parent_ = parent_ptr;
00520                 }
00521                 else
00522                 {
00523                         // Root node
00524                         root_ptr_ = left_subtree;
00525                         left_subtree->parent_ = NULL;
00526                 }
00527 
00528                 node_ptr->calculate_depth ();
00529                 balance (node_ptr);
00530         }
00531         else
00532         {
00533                 // Middle tree has to be the new root, multiple rotations
00534                 AVLTreeNode <key_type, item_type>* middle_left = middle_subtree->left_;
00535                 AVLTreeNode <key_type, item_type>* middle_right = middle_subtree->right_;
00536 
00537                 // Right side of the tree
00538                 middle_subtree->right_ = node_ptr;
00539                 node_ptr->parent_ = middle_subtree;
00540 
00541                 node_ptr->left_ = middle_right;
00542                 if (middle_right)
00543                         middle_right->parent_ = node_ptr;
00544 
00545                 // Left side of the tree
00546                 middle_subtree->left_ = left_subtree;
00547                 if (left_subtree)
00548                         left_subtree->parent_ = middle_subtree;
00549 
00550                 if (left_subtree)
00551                         left_subtree->right_ = middle_left;
00552                 if (middle_left)
00553                         middle_left->parent_ = left_subtree;
00554 
00555                 // Reconstruct tree
00556                 if (parent_ptr)
00557                 {
00558                         // Non-root node
00559                         if (parent_ptr->left_ == node_ptr)
00560                         {
00561                                 // Node is the left child of its parent
00562                                 parent_ptr->left_ = middle_subtree;
00563                         }
00564                         else
00565                         {
00566                                 // Node is the right child of its parent
00567                                 parent_ptr->right_ = middle_subtree;
00568                         }
00569                         middle_subtree->parent_ = parent_ptr;
00570                 }
00571                 else
00572                 {
00573                         // Root node
00574                         root_ptr_ = middle_subtree;
00575                         middle_subtree->parent_ = NULL;
00576                 }
00577 
00578                 if (left_subtree)
00579                         left_subtree->calculate_depth ();
00580                 node_ptr->calculate_depth ();
00581 
00582                 if (left_subtree)
00583                         balance (left_subtree);
00584                 balance (node_ptr);
00585         }
00586 }
00587 
00588 template <class key_type, class item_type>
00589 AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::begin ()
00590 {
00591         if (!root_ptr_)
00592                 return NULL;
00593 
00594         AVLTreeNode <key_type, item_type>* node_ptr = root_ptr_;
00595 
00596         while (node_ptr->left_)
00597                 node_ptr = node_ptr->left_;
00598 
00599         return node_ptr;
00600 }
00601 
00602 template <class key_type, class item_type>
00603 const AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::begin () const
00604 {
00605         if (!root_ptr_)
00606                 return NULL;
00607 
00608         AVLTreeNode <key_type, item_type>* node_ptr = root_ptr_;
00609 
00610         while (node_ptr->left_)
00611                 node_ptr = node_ptr->left_;
00612 
00613         return node_ptr;
00614 }
00615 
00616 template <class key_type, class item_type>
00617 inline void AVLTree <key_type, item_type>::clear ()
00618 {
00619         if (root_ptr_)
00620         {
00621                 delete root_ptr_;
00622                 size_ = 0;
00623                 root_ptr_ = NULL;
00624         }
00625 }
00626 
00627 template <class key_type, class item_type>
00628 void AVLTree <key_type, item_type>::destroy ()
00629 {
00630         if (root_ptr_)
00631         {
00632                 Iterator <AVLTreeNode <key_type, item_type> > iter;
00633                 for (iter = begin (); iter != end (); iter++)
00634                         delete iter->second;
00635 
00636                 delete root_ptr_;
00637                 size_ = 0;
00638                 root_ptr_ = NULL;
00639         }
00640 }
00641 
00642 template <class key_type, class item_type>
00643 inline bool AVLTree <key_type, item_type>::empty ()
00644 {
00645         return (size_ == 0) ? true : false;
00646 }
00647 
00648 template <class key_type, class item_type>
00649 inline bool AVLTree <key_type, item_type>::empty () const
00650 {
00651         return (size_ == 0) ? true : false;
00652 }
00653 
00654 template <class key_type, class item_type>
00655 inline AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::end ()
00656 {
00657         return NULL;
00658 }
00659 
00660 template <class key_type, class item_type>
00661 inline const AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::end () const
00662 {
00663         return NULL;
00664 }
00665 
00666 template <class key_type, class item_type>
00667 inline void AVLTree <key_type, item_type>::erase (key_type key)
00668 {
00669         erase (find (key));
00670 }
00671 
00672 template <class key_type, class item_type>
00673 void AVLTree <key_type, item_type>::erase (Iterator <AVLTreeNode <key_type, item_type> >& iter)
00674 {
00675         AVLTreeNode <key_type, item_type>* parent_ptr = iter->parent_;
00676         AVLTreeNode <key_type, item_type>* left_ptr = iter->left_;
00677         AVLTreeNode <key_type, item_type>* right_ptr = iter->right_;
00678         --size_;
00679 
00680         // No children
00681         if ((!left_ptr) && (!right_ptr))
00682         {
00683                 erase_leaf (*iter, parent_ptr);
00684         }
00685         else if ((!left_ptr) || (!right_ptr))
00686         {
00687                 // Only one child
00688                 if (!left_ptr)
00689                 {
00690                         erase_right_child (*iter, parent_ptr, right_ptr);
00691                 }
00692                 else
00693                 {
00694                         erase_left_child (*iter, parent_ptr, left_ptr);
00695                 }
00696         }
00697         else
00698         {
00699                 erase (*iter, parent_ptr, left_ptr, right_ptr);
00700         }
00701 }
00702 
00703 template <class key_type, class item_type>
00704 void AVLTree <key_type, item_type>::erase (AVLTreeNode <key_type, item_type>* node_ptr, 
00705                 AVLTreeNode <key_type, item_type>* parent_ptr, AVLTreeNode <key_type, item_type>* left_ptr,
00706                 AVLTreeNode <key_type, item_type>* right_ptr)
00707 {
00708         // Node has two children
00709         if (parent_ptr)
00710         {
00711                 // Non-root node
00712                 if (parent_ptr->left_ == node_ptr)
00713                 {
00714                         // The node is the left child of its parent
00715                         // Could start with the larger node as well
00716                         AVLTreeNode <key_type, item_type>* new_node_ptr = 
00717                                 get_smaller_node (node_ptr, left_ptr, right_ptr);
00718 
00719                         // Reconstruct tree
00720                         parent_ptr->left_ = new_node_ptr;
00721                         new_node_ptr->parent_ = parent_ptr;
00722                         new_node_ptr->calculate_depth ();
00723                         balance (new_node_ptr);
00724                 }
00725                 else
00726                 {
00727                         // The node is the right child of its parent
00728                         // Could start with the smaller node as well
00729                         AVLTreeNode <key_type, item_type>* new_node_ptr = 
00730                                 get_larger_node (node_ptr, left_ptr, right_ptr);
00731 
00732                         // Reconstruct tree
00733                         parent_ptr->right_ = new_node_ptr;
00734                         new_node_ptr->parent_ = parent_ptr;
00735                         new_node_ptr->calculate_depth ();
00736                         balance (new_node_ptr);
00737                 }
00738         }
00739         else
00740         {
00741                 // Root node
00742                 // Could start with the larger node as well
00743                 AVLTreeNode <key_type, item_type>* new_node_ptr = 
00744                         get_smaller_node (node_ptr, left_ptr, right_ptr);
00745 
00746                 // Reconstruct tree
00747                 root_ptr_ = new_node_ptr;
00748                 root_ptr_->parent_ = NULL;
00749                 root_ptr_->calculate_depth ();
00750                 balance (root_ptr_);
00751         }
00752 
00753         // Prevent recursive delete
00754         node_ptr->left_ = NULL;
00755         node_ptr->right_ = NULL;
00756         delete node_ptr;
00757 }
00758 
00759 template <class key_type, class item_type>
00760 void AVLTree <key_type, item_type>::erase_leaf (AVLTreeNode <key_type, item_type>* node_ptr, AVLTreeNode <key_type, item_type>* parent_ptr)
00761 {
00762         if (parent_ptr)
00763         {
00764                 // Simple leaf
00765                 if (parent_ptr->left_ == node_ptr)
00766                 {
00767                         // leaf is left child
00768                         parent_ptr->left_ = NULL;
00769                 }
00770                 else
00771                         // leaf is right child
00772                         parent_ptr->right_ = NULL;
00773 
00774                 parent_ptr->calculate_depth ();
00775                 balance (parent_ptr);
00776         }
00777         else
00778                 root_ptr_ = NULL;
00779 
00780         delete node_ptr;
00781         return;
00782 }
00783 
00784 template <class key_type, class item_type>
00785 void AVLTree <key_type, item_type>::erase_left_child (AVLTreeNode <key_type, item_type>* node_ptr, 
00786         AVLTreeNode <key_type, item_type>* parent_ptr, AVLTreeNode <key_type, item_type>* left_ptr)
00787 {
00788         if (parent_ptr)
00789         {
00790                 // Left child
00791                 // Find out whether the node pointed to by iter is a left or right child
00792                 if (parent_ptr->left_ == node_ptr)
00793                 {
00794                         // Left child
00795                         parent_ptr->left_ = left_ptr;
00796                 }
00797                 else
00798                 {
00799                         // Right child
00800                         parent_ptr->right_ = left_ptr;
00801                 }
00802         }
00803         else
00804         {
00805                 root_ptr_ = left_ptr;
00806         }
00807 
00808         left_ptr->parent_ = parent_ptr;
00809         left_ptr->calculate_depth ();
00810         balance (left_ptr);
00811         // Prevent recursive delete
00812         node_ptr->left_ = NULL;
00813         delete node_ptr;
00814 }
00815 
00816 template <class key_type, class item_type>
00817 void AVLTree <key_type, item_type>::erase_right_child (AVLTreeNode <key_type, item_type>* node_ptr, 
00818         AVLTreeNode <key_type, item_type>* parent_ptr, AVLTreeNode <key_type, item_type>* right_ptr)
00819 {
00820         if (parent_ptr)
00821         {
00822                 // Right child
00823                 // Find out whether the node pointed to by iter is a left or right child
00824                 if (parent_ptr->left_ == node_ptr)
00825                 {
00826                         // Left child
00827                         parent_ptr->left_ = right_ptr;
00828                 }
00829                 else
00830                 {
00831                         // Right child
00832                         parent_ptr->right_ = right_ptr;
00833                 }
00834         }
00835         else
00836         {
00837                 root_ptr_ = right_ptr;
00838         }
00839 
00840         right_ptr->parent_ = parent_ptr;
00841         right_ptr->calculate_depth ();
00842         balance (right_ptr);
00843         // Prevent recursive delete
00844         node_ptr->right_ = NULL;
00845         delete node_ptr;
00846 }
00847 
00848 template <class key_type, class item_type>
00849 AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::find (const key_type& key)
00850 {
00851         if (!root_ptr_)
00852                 return NULL;
00853 
00854         AVLTreeNode <key_type, item_type>* node_ptr = root_ptr_;
00855 
00856         // Non-recursive implementation for faster performance
00857         while (((node_ptr->first < key) && (node_ptr->right_)) || ((key < node_ptr->first) && (node_ptr->left_)))
00858         {
00859                 node_ptr = (node_ptr->first < key) ? node_ptr->right_ : node_ptr->left_;
00860         }
00861 
00862         return (node_ptr->first == key) ? node_ptr : NULL;
00863 }
00864 
00865 template <class key_type, class item_type>
00866 const AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::find (const key_type& key) const
00867 {
00868         if (!root_ptr_)
00869                 return NULL;
00870 
00871         AVLTreeNode <key_type, item_type>* node_ptr = root_ptr_;
00872 
00873         // Non-recursive implementation for faster performance
00874         while (((node_ptr->first < key) && (node_ptr->right_)) || ((key < node_ptr->first) && (node_ptr->left_)))
00875         {
00876                 node_ptr = (node_ptr->first < key) ? node_ptr->right_ : node_ptr->left_;
00877         }
00878 
00879         return (node_ptr->first == key) ? node_ptr : NULL;
00880 }
00881 
00882 template <class key_type, class item_type>
00883 AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::get_smaller_node (AVLTreeNode <key_type, item_type>* node_ptr,
00884         AVLTreeNode <key_type, item_type>* left_ptr, AVLTreeNode <key_type, item_type>* right_ptr)
00885 {
00886         // Get the next smaller element
00887         AVLTreeNode <key_type, item_type>* new_node_ptr = node_ptr->left_;
00888 
00889         if (new_node_ptr->right_)
00890         {
00891                 // Find rightmost child
00892                 while (new_node_ptr->right_)
00893                         new_node_ptr = new_node_ptr->right_;
00894 
00895                 if (new_node_ptr->left_)
00896                 {
00897                         new_node_ptr->parent_->right_ = new_node_ptr->left_;
00898                         new_node_ptr->left_->parent_ = new_node_ptr->parent_;
00899                         new_node_ptr->left_->calculate_depth ();
00900                         balance (new_node_ptr->left_);
00901                 }
00902                 else
00903                 {
00904                         new_node_ptr->parent_->right_ = NULL;
00905                         new_node_ptr->parent_->calculate_depth ();
00906                         balance (new_node_ptr->parent_);
00907                 }
00908 
00909                 new_node_ptr->left_ = left_ptr;
00910                 left_ptr->parent_ =new_node_ptr;
00911         }
00912 
00913         // Reconstruct tree
00914         new_node_ptr->right_ = right_ptr;
00915         right_ptr->parent_ = new_node_ptr;
00916 
00917         return new_node_ptr;
00918 }
00919 
00920 template <class key_type, class item_type>
00921 AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::get_larger_node (AVLTreeNode <key_type, item_type>* node_ptr,
00922         AVLTreeNode <key_type, item_type>* left_ptr, AVLTreeNode <key_type, item_type>* right_ptr)
00923 {
00924         // Get the next larger element
00925         AVLTreeNode <key_type, item_type>* new_node_ptr = node_ptr->right_;
00926 
00927         if (new_node_ptr->left_)
00928         {
00929                 // Find rightmost child
00930                 while (new_node_ptr->left_)
00931                         new_node_ptr = new_node_ptr->left_;
00932 
00933                 if (new_node_ptr->right_)
00934                 {
00935                         new_node_ptr->parent_->left_ = new_node_ptr->right_;
00936                         new_node_ptr->right_->parent_ = new_node_ptr->parent_;
00937                         new_node_ptr->right_->calculate_depth ();
00938                         balance (new_node_ptr->right_);
00939                 }
00940                 else
00941                 {
00942                         new_node_ptr->parent_->left_ = NULL;
00943                         new_node_ptr->parent_->calculate_depth ();
00944                         balance (new_node_ptr->parent_);
00945                 }
00946 
00947                 new_node_ptr->right_ = right_ptr;
00948                 right_ptr->parent_ = new_node_ptr;
00949         }
00950 
00951         // Reconstruct tree
00952         left_ptr->parent_ = new_node_ptr;
00953         new_node_ptr->left_ = left_ptr;
00954 
00955         return new_node_ptr;
00956 }
00957 
00958 template <class key_type, class item_type>
00959 void AVLTree <key_type, item_type>::insert (key_type key, item_type item)
00960 {
00961         ++size_;
00962         if (root_ptr_)
00963         {
00964                 AVLTreeNode <key_type, item_type>* node_ptr = root_ptr_;
00965 
00966                 // Prefer to grow to the left if keys equal
00967                 // Non-recursive implementation for faster performance
00968                 while (((node_ptr->first < key) && (node_ptr->right_)) || ((key < node_ptr->first) && (node_ptr->left_)))
00969                         node_ptr = (node_ptr->first < key) ? node_ptr->right_ : node_ptr->left_;
00970 
00971                 // Found leaf
00972                 if (node_ptr->first == key)
00973                         node_ptr->second = item;
00974                 else if (node_ptr->first < key)
00975                 {
00976                         node_ptr->right_ = new AVLTreeNode <key_type, item_type> (key, item, node_ptr);
00977                         node_ptr->right_->calculate_depth ();
00978                         if (node_ptr == last_ptr_)
00979                                 last_ptr_ = last_ptr_->right_;
00980                         balance (node_ptr->right_);
00981                 }
00982                 else
00983                 {
00984                         node_ptr->left_ = new AVLTreeNode <key_type, item_type> (key, item, node_ptr);
00985                         node_ptr->left_->calculate_depth ();
00986                         balance (node_ptr->left_);
00987                 }
00988         }
00989         else
00990         {
00991                 // Create root node
00992                 root_ptr_ = new AVLTreeNode <key_type, item_type> (key, item);
00993                 last_ptr_ = root_ptr_;
00994                 root_ptr_->calculate_depth ();
00995                 balance (root_ptr_);
00996         }
00997 }
00998 
00999 template <class key_type, class item_type>
01000 inline AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::last ()
01001 {
01002         return last_ptr_;
01003 }
01004 
01005 template <class key_type, class item_type>
01006 inline const AVLTreeNode <key_type, item_type>* AVLTree <key_type, item_type>::last () const
01007 {
01008         return last_ptr_;
01009 }
01010 
01011 template <class key_type, class item_type>
01012 item_type& AVLTree <key_type, item_type>::operator [] (uint i)
01013 {
01014         // This function is NOT similar to std::map, but to std::vector
01015         // Quite inefficient...
01016         Iterator <AVLTreeNode <key_type, item_type> > iter;
01017         iter = begin ();
01018 
01019         uint j;
01020         for (j = 0; j < i; j++)
01021                 ++iter;
01022 
01023         return iter->second;
01024 }
01025 
01026 template <class key_type, class item_type>
01027 const item_type& AVLTree <key_type, item_type>::operator [] (uint i) const
01028 {
01029         // This function is NOT similar to std::map, but to std::vector
01030         // Quite inefficient...
01031         Const_Iterator <AVLTreeNode <key_type, item_type> > iter;
01032         iter = begin ();
01033 
01034         uint j;
01035         for (j = 0; j < i; j++)
01036                 ++iter;
01037 
01038         return iter->second;
01039 }
01040 
01041 template <class key_type, class item_type>
01042 inline uint AVLTree <key_type, item_type>::size () const
01043 {
01044         return size_;
01045 }
01046 
01047 };
01048 
01049 #endif

Generated on Fri Jul 27 18:30:03 2007 for DREAM by  doxygen 1.5.1