00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef neo_core_avltree_h
00022 #define neo_core_avltree_h
00023
00027 #include "platform.h"
00028 #include "exception.h"
00029 #include "queue.h"
00030 #include "util.h"
00031
00032 namespace neo {
00033 namespace core {
00034
00035 template < typename T, typename Cmp > class AVLTree;
00036
00044 template < typename T > class AVLTreeNode
00045 {
00046 public:
00047
00049 enum Balance
00050 {
00052 LEFTHEAVY,
00053
00055 BALANCED,
00056
00058 RIGHTHEAVY
00059 };
00060
00062
00064 inline AVLTreeNode( AVLTreeNode* p_parent, const T& obj );
00065
00067 Balance _balance;
00068
00070 AVLTreeNode* _p_left;
00071
00073 AVLTreeNode* _p_right;
00074
00076 AVLTreeNode* _p_parent;
00077
00079 T _obj;
00080 };
00081
00088 template < class T > class AVLTreeIterator
00089 {
00090 public:
00091
00092 typedef std::forward_iterator_tag iterator_category;
00093 typedef T value_type;
00094 typedef std::ptrdiff_t difference_type;
00095 typedef T* pointer;
00096 typedef T& reference;
00097
00098 typedef AVLTreeNode< T > Node;
00099
00101
00102 inline AVLTreeIterator( Node* p_node = 0 );
00103
00105
00106 inline AVLTreeIterator( const AVLTreeIterator& iter );
00107
00109
00110 inline T& operator *();
00111
00113
00114 inline T* operator ->();
00115
00117
00118 inline const T& operator *() const;
00119
00121
00122 inline const T* operator ->() const;
00123
00125
00126 inline AVLTreeIterator& operator ++() const;
00127
00129
00130 inline AVLTreeIterator operator ++(int) const;
00131
00133
00135 inline bool operator ==( const AVLTreeIterator< T >& rhs ) const;
00136
00138
00140 inline bool operator !=( const AVLTreeIterator< T >& rhs ) const;
00141
00143
00145 inline AVLTreeIterator& operator = ( const AVLTreeIterator< T >& rhs ) const;
00146
00148
00149 inline operator bool () const;
00150
00152
00153 inline AVLTreeIterator left() const;
00154
00156
00157 inline AVLTreeIterator right() const;
00158
00160
00161 inline AVLTreeIterator parent() const;
00162
00164
00165 inline bool isLeaf() const;
00166
00168 mutable Node* _p_node;
00169
00170 private:
00171
00173 mutable Queue< Node* > _nodes;
00174 };
00175
00196 template < class T, class Cmp = Comparator< T, T > > class AVLTree : public Noncopyable
00197 {
00198 public:
00199
00201 typedef AVLTreeIterator< T > iterator;
00202
00204 typedef const iterator const_iterator;
00205
00206
00207 typedef AVLTreeNode< T > Node;
00208
00209
00211 AVLTree();
00212
00214 virtual ~AVLTree();
00215
00217
00219 inline void insert( const T& obj );
00220
00222
00224 inline void erase( const T& obj );
00225
00227
00230 inline void erase( const_iterator& pos );
00231
00233
00234 void clear();
00235
00237
00239 inline unsigned int size() const;
00240
00242
00243 inline bool empty() const;
00244
00246
00248 inline iterator begin();
00249
00251
00253 inline const_iterator begin() const;
00254
00256
00259 inline iterator end();
00260
00262
00265 inline const_iterator end() const;
00266
00268
00270 inline void swap( AVLTree< T, Cmp >& tree );
00271
00272 #if !NEO_COMPILER_MSVC7
00273
00274 private:
00275 #endif
00276
00278 enum Result
00279 {
00281 OK,
00282
00284 BALANCE,
00285
00287 INVALID
00288 };
00289
00290
00292 Node* _p_root;
00293
00295 unsigned int _size;
00296
00297
00299
00301 inline void rotateLeft( Node** pp_node );
00302
00304
00306 inline void rotateRight( Node** pp_node );
00307
00309
00312 inline Result balanceLeftGrown( Node** pp_node );
00313
00315
00318 inline Result balanceRightGrown( Node** pp_node );
00319
00321
00324 inline Result balanceLeftShrunk( Node** pp_node );
00325
00327
00330 inline Result balanceRightShrunk( Node** pp_node );
00331
00333
00338 inline bool replaceWithHighest( Node* p_target, Node** pp_subtree, Result* p_result );
00339
00341
00346 inline bool replaceWithLowest( Node* p_target, Node** pp_subtree, Result* p_result );
00347
00349
00354 Result insert( Node** pp_parent, Node** pp_node, const T& data );
00355
00357
00361 Result erase( Node** pp_node, const T& data );
00362
00364
00366 void deleteSubtree( Node* p_node );
00367
00369
00370 Node** getParent( Node* p_node );
00371
00372 #if !NEO_COMPILER_MSVC7
00373
00374 template < typename TT, typename TKey, typename TCmp > friend typename AVLTree< TT, TCmp >::iterator searchBestFit( AVLTree< TT, TCmp >& tree, const TKey& key );
00375 #endif
00376 };
00377
00379
00411 template < typename T, typename Key, typename Cmp > typename AVLTree< T, Cmp >::iterator searchBestFit( AVLTree< T, Cmp >& tree, const Key& key );
00412
00413
00414
00415 #include "avltree.inl"
00416
00418
00421 template < typename T, typename Cmp > inline void swap( AVLTree< T, Cmp >& lval, AVLTree< T, Cmp >& rval ) { lval.swap( rval ); }
00422
00423 }
00424 }
00425
00426 #endif