AVLTree.h

Go to the documentation of this file.
00001 /** @file AVLTree.h
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 #if (_MSC_VER >= 1400)                          // VC8+
00052 #pragma warning (disable:4996)          // Disable all deprecation warnings
00053 #endif 
00054 
00055 #ifndef DREAM_AVLTREE
00056 #define DREAM_AVLTREE
00057 
00058 /** Unsigned int type definition.
00059 * Redefining the basic type allows easy portability to 8 or 16 bit machines. 
00060 */
00061 typedef unsigned int uint;
00062 #define maxuint 4294967295
00063 
00064 #include "Iterator.h"
00065 #include "Iterator.cpp"
00066 
00067 namespace DREAM
00068 {
00069 
00070 template <class key_type, class item_type> class AVLTree;
00071 
00072 /** Class for one element in the AVLTree. */
00073 template <class key_type, class item_type>
00074 class AVLTreeNode
00075 {
00076 // friend class declarations and typedefs confuse Doxygen... Starting with protected members first.
00077 protected:
00078         /** ++ operator. 
00079         * This operator does not actually change the node's value and should only be used from within Iterators.
00080         */
00081         AVLTreeNode <key_type, item_type>* iter_next ();
00082 
00083         /** -- operator. 
00084         * This operator does not actually change the node's value and should only be used from within Iterators.
00085         */
00086         AVLTreeNode <key_type, item_type>* iter_previous ();
00087 
00088         /** ++ operator. 
00089         * This operator does not actually change the node's value and should only be used from within Iterators.
00090         */
00091         const AVLTreeNode <key_type, item_type>* iter_next () const;
00092 
00093         /** -- operator. 
00094         * This operator does not actually change the node's value and should only be used from within Iterators.
00095         */
00096         const AVLTreeNode <key_type, item_type>* iter_previous () const;
00097 
00098         /** The left child of the node. */
00099         AVLTreeNode <key_type, item_type>* left_;
00100         
00101         /** The right child of the node. */
00102         AVLTreeNode <key_type, item_type>* right_;
00103 
00104         /** The parent of the node. */
00105         AVLTreeNode <key_type, item_type>* parent_;
00106 
00107         /** Stores the depth of the node's subtree. */
00108         uint depth_;
00109 
00110         /** Stores the number of nodes with the given depth.
00111         * The use of this variable allows us to decrease the depth of trees when no more nodes are available with the give depth.
00112         */
00113         uint depth_count_;
00114 
00115 public:
00116         /** Constructor. */
00117         AVLTreeNode (key_type key, item_type item, AVLTreeNode <key_type, item_type>* parent = NULL);
00118 
00119         /** Destructor. */
00120         ~AVLTreeNode ();
00121 
00122         /** Called when the node is on the path to a newly inserted or removed node. 
00123         * It manages the depth_ and depth_count_ variables...
00124         */
00125         void calculate_depth ();
00126 
00127         /** Returns the depth of the subtree from the given point */
00128         uint depth ();
00129 
00130         /** Return the left child of the node. */
00131         AVLTreeNode <key_type, item_type>* left ();
00132 
00133         /** Return the left child of the node. */
00134         const AVLTreeNode <key_type, item_type>* left () const ;
00135 
00136         /** Sets the left child of the node. */
00137         void left (AVLTreeNode <key_type, item_type>* node_ptr);
00138 
00139         /** Return the right child of the node. */
00140         AVLTreeNode <key_type, item_type>* right ();
00141 
00142         /** Return the right child of the node. */
00143         const AVLTreeNode <key_type, item_type>* right () const;
00144 
00145         /** Sets the right child of the node. */
00146         void right (AVLTreeNode <key_type, item_type>* node_ptr);
00147 
00148         /** Returns the parent of the node. */
00149         AVLTreeNode <key_type, item_type>* parent ();
00150 
00151         /** Returns the parent of the node. */
00152         const AVLTreeNode <key_type, item_type>* parent () const;
00153 
00154         /** Sets the parent of the node. */
00155         void parent (AVLTreeNode <key_type, item_type>* node_ptr);
00156 
00157         /** Assignment operator */
00158         AVLTreeNode <key_type, item_type>& operator= (AVLTreeNode <key_type, item_type>& node);
00159 
00160         /** The key of the node. */
00161         key_type first;
00162 
00163         /** The item of the node. */
00164         item_type second;
00165 
00166         /** Allow iterators to manipulate protected members. */
00167         friend class Iterator <AVLTreeNode <key_type, item_type> >;
00168 
00169         /** Allow iterators to manipulate protected members. */
00170         friend class Const_Iterator <AVLTreeNode <key_type, item_type> >;
00171 
00172         /** Allow AVLTree to manipulate protected members. */
00173         friend class AVLTree <key_type, item_type>;
00174 };
00175 
00176 
00177 
00178 /** This class combines STL vectors and maps providing a fast implementation for find (), operator++ (), operator-- (), 
00179 * and operator[] () in a common class. This is necessary since we want random access to elements in the tree, which
00180 * requires a mix of STL maps and vectors. We prefer faster find and insert  operations, therefore the underlying data
00181 * structure is an AVL tree as it allows for the easy and efficient implementation of all these operators.
00182 */
00183 template <class key_type, class item_type>
00184 class AVLTree
00185 {
00186 // friend class declarations and typedefs confuse Doxygen... Starting with protected members first.
00187 protected:
00188         /** Balances the AVLTree from the given node if necessary. */
00189         void balance (AVLTreeNode <key_type, item_type>* node_ptr);
00190 
00191         /** Balances the AVLTree to the right from the given node. */
00192         void balance_left (AVLTreeNode <key_type, item_type>* node_ptr);
00193 
00194         /** Balances the AVLTree to the right from the given node. */
00195         void balance_right (AVLTreeNode <key_type, item_type>* node_ptr);
00196 
00197         /** Finds the next smaller node in the subtree of node_ptr, removes it, and reconstructs the tree. */
00198         AVLTreeNode <key_type, item_type>* get_smaller_node (AVLTreeNode <key_type, item_type>* node_ptr,
00199                 AVLTreeNode <key_type, item_type>* left_ptr, AVLTreeNode <key_type, item_type>* right_ptr);
00200 
00201         /** Finds the next larger node in the subtree of node_ptr, removes it, and reconstructs the tree. */
00202         AVLTreeNode <key_type, item_type>* get_larger_node (AVLTreeNode <key_type, item_type>* node_ptr,
00203                 AVLTreeNode <key_type, item_type>* left_ptr, AVLTreeNode <key_type, item_type>* right_ptr);
00204 
00205         /** Erase a leaf from the AVLTree. */
00206         void erase_leaf (AVLTreeNode <key_type, item_type>* node_ptr, AVLTreeNode <key_type, item_type>* parent_ptr);
00207 
00208         /** Erase a node with only a left child from the AVLTree. */
00209         void erase_left_child (AVLTreeNode <key_type, item_type>* node_ptr, 
00210                 AVLTreeNode <key_type, item_type>* parent_ptr, AVLTreeNode <key_type, item_type>* left_ptr);
00211 
00212         /** Erase a node with only a right child from the AVLTree. */
00213         void erase_right_child (AVLTreeNode <key_type, item_type>* node_ptr, 
00214                 AVLTreeNode <key_type, item_type>* parent_ptr, AVLTreeNode <key_type, item_type>* right_ptr);
00215 
00216         /** Erase a node with both children present from the AVLTree. */
00217         void erase (AVLTreeNode <key_type, item_type>* node_ptr, AVLTreeNode <key_type, item_type>* parent_ptr, 
00218                 AVLTreeNode <key_type, item_type>* left_ptr, AVLTreeNode <key_type, item_type>* right_ptr);
00219 
00220         /** Pointer to the root of the tree. */
00221         AVLTreeNode <key_type, item_type>* root_ptr_;
00222 
00223         /** Pointer to the last element of the tree. */
00224         AVLTreeNode <key_type, item_type>* last_ptr_;
00225 
00226         /** Stores the number of items in the AVLTree. */
00227         uint size_;
00228 
00229 public:
00230         /** Constructor. */
00231         AVLTree ();
00232 
00233         /** Destructor. */
00234         ~AVLTree ();
00235 
00236         /** Assign operator. */
00237         void assign (const AVLTreeNode <key_type, item_type>* begin_node, const AVLTreeNode <key_type, item_type>* end_node);
00238 
00239         /** Assign operator. */
00240         void assign (Iterator <AVLTreeNode <key_type, item_type> >& begin_iter, Iterator <AVLTreeNode <key_type, item_type> >& end_iter);
00241 
00242         /** Assign operator. */
00243         void assign (Const_Iterator <AVLTreeNode <key_type, item_type> >& begin_iter, Const_Iterator <AVLTreeNode <key_type, item_type> >& end_iter);
00244 
00245         /** Returns a pointer to the smallest element in the AVLTree. */
00246         AVLTreeNode <key_type, item_type>* begin ();
00247 
00248         /** Returns a pointer to the smallest element in the AVLTree. */
00249         const AVLTreeNode <key_type, item_type>* begin () const;
00250 
00251         /** Erases elements in the AVLTree. */
00252         void clear ();
00253 
00254         /** Erases elements in the AVLTree, and calls delete on the item_type.
00255         * Use this function whenever you would like to iterate through the AVLTree, and erase dynamically allocated data.
00256         */
00257         void destroy ();
00258 
00259         /** Returns true if the AVLTree is empty. */
00260         bool empty ();
00261 
00262         /** Returns true if the AVLTree is empty. */
00263         bool empty () const;
00264 
00265         /** Returns NULL. */
00266         AVLTreeNode <key_type, item_type>* end ();
00267 
00268         /** Returns NULL. */
00269         const AVLTreeNode <key_type, item_type>* end () const;
00270 
00271         /** Erase a node from the AVLTree. */
00272         void erase (Iterator <AVLTreeNode <key_type, item_type> >& iter);
00273 
00274         /** Erase a node from the AVLTree. */
00275         void erase (key_type key);
00276 
00277         /** Finds an AVLTree node in the AVLTree. */
00278         AVLTreeNode <key_type, item_type>* find (const key_type& key);
00279 
00280         /** Finds an AVLTree node in the AVLTree. */
00281         const AVLTreeNode <key_type, item_type>* find (const key_type& key) const;
00282 
00283         /** Inserts an AVLTree node in the AVLTree. */
00284         void insert (key_type key, item_type item);
00285 
00286         /** Returns an iterator to the last element of the AVLTree. */
00287         AVLTreeNode <key_type, item_type>* last ();
00288 
00289         /** Returns an iterator to the last element of the AVLTree. */
00290         const AVLTreeNode <key_type, item_type>* last () const;
00291 
00292         /** operator [].*/
00293         item_type& operator [] (uint i);
00294 
00295         /** operator [].*/
00296         const item_type& operator [] (uint i) const;
00297 
00298         /** Return the number of items in the AVLTree. */
00299         uint size () const;
00300 
00301         /** iterator class for convenience. */
00302         typedef Iterator <AVLTreeNode <key_type, item_type> > iterator;
00303 
00304         /** const_iterator class for convenience. */
00305         typedef Const_Iterator <AVLTreeNode <key_type, item_type> > const_iterator;
00306 
00307         /** Allow iterators to manipulate protected members. */
00308         friend class Iterator <AVLTreeNode <key_type, item_type> >;
00309 
00310         /** Allow iterators to manipulate protected members. */
00311         friend class Const_Iterator <AVLTreeNode <key_type, item_type> >;
00312 };
00313 
00314 };
00315 
00316 #endif

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