ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
AVLTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File AVLTree.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
48/*--------------------------------------------------------------------------*/
49/*----------------------------- DEFINITIONS --------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52#ifndef __ADS_AVLTree
53 #define __ADS_AVLTree
54/* self-identification: #endif constant */
55
56/*--------------------------------------------------------------------------*/
57/*------------------------------ INCLUDES ----------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#include "ADS/Contract.h"
61#include "ADS/Iterators.h"
64
65#include <algorithm>
66#include <concepts>
67#include <cstddef>
68#include <memory>
69#include <optional>
70#include <utility>
71
72/*--------------------------------------------------------------------------*/
73/*--------------------------- NAMESPACE ------------------------------------*/
74/*--------------------------------------------------------------------------*/
75
76namespace ADS
77{
78
79/*--------------------------------------------------------------------------*/
80/*---------------------------- CLASS AVLTree -------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83template< std::totally_ordered K , class V >
84class AVLTree;
85
87template< std::totally_ordered K , class V >
89
92template< std::totally_ordered K , class V >
93class AVLTree : public Joinable< AVLTree< K , V > , AVLTreeNodes< K , V > , K ,
94 V >
95{
96
97 friend struct AVLTreeNodes< K , V >;
98
99/*--------------------------------------------------------------------------*/
100 public:
101 using Key = K;
102 using Value = V;
103
106 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
107
138 void insert( const K & k , const V & v );
139
165 void remove( const K & k );
166
168 [[nodiscard]] bool contains( const K & k ) const;
169
172 [[nodiscard]] std::optional< std::pair< K , V > >
173 lowerBound( const K & k ) const;
174
178 [[nodiscard]] const K * findKey( const K & k ) const;
179
183 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
184
186 [[nodiscard]] std::size_t size( void ) const;
187
189 [[nodiscard]] bool empty( void ) const;
190
192 [[nodiscard]] int height( void ) const;
193
195 template< class F >
196 void forEach( F && f ) const;
197
201 [[nodiscard]] std::pair< std::size_t , std::size_t >
202 rebalancings( void ) const {
203 return(
204 std::pair< std::size_t , std::size_t >( f_rebalances , f_rotations ) );
205 }
206
207/*--------------------------------------------------------------------------*/
208 private:
210 struct Node
211 {
212 K key;
213 V value;
214 int h = 0;
215 std::size_t s = 1;
216 std::unique_ptr< Node > left ,
217 right;
218 };
219
220 public:
221/*------------------------- THE STANDARD INTERFACE -------------------------*/
222
223 // a constant view of the pairs ( key , value ) by increasing key,
224 // bidirectional, along the path from the root (the nodes have no parent)
225
227 struct Shape
228 {
230 static auto root( const auto & o ) { return( o.f_root.get() ); }
232 static auto left( const auto * n ) { return( n->left.get() ); }
234 static auto right( const auto * n ) { return( n->right.get() ); }
236 static auto read( const auto * n ) {
237 return( PairRef< const K & , const V & >( n->key , n->value ) );
238 }
239 };
240
242 using value_type = std::pair< K , V >;
244 using key_type = K;
246 using mapped_type = V;
247 using size_type = std::size_t;
248 using difference_type = std::ptrdiff_t;
254
256 [[nodiscard]] const_iterator begin( void ) const {
257 return( const_iterator( this , true ) );
258 }
259
260 [[nodiscard]] const_iterator end( void ) const {
261 return( const_iterator( this , false ) );
262 }
263
264 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
266 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
267
268 private:
269 using Ptr = std::unique_ptr< Node >;
270
271 static int h( const Ptr & t ) { return( t ? t->h : -1 ); }
272 static std::size_t sz( const Ptr & t ) { return( t ? t->s : 0 ); }
273 static void update( Ptr & t );
274 static void rotateRight( Ptr & t );
275 static void rotateLeft( Ptr & t );
276 void balance( Ptr & t );
277 bool insert( Ptr & t , const K & k , const V & v );
278 bool remove( Ptr & t , const K & k );
279 template< class F >
280 static void visit( const Ptr & t , F & f );
281
284 static bool valid( const Ptr & t , const K * lo , const K * hi );
285
286
287 Ptr f_root;
288 std::size_t f_size = 0;
289 std::size_t f_rebalances = 0;
290 std::size_t f_rotations = 0;
291
292 }; // end( class AVLTree )
293
294/*--------------------------------------------------------------------------*/
295
296} // end( namespace ADS )
297
298/*--------------------------------------------------------------------------*/
299/*-------------------------- IMPLEMENTATION --------------------------------*/
300/*--------------------------------------------------------------------------*/
301
302#include "ADS/tree/impl/AVLTree.tpp"
303
304/*--------------------------------------------------------------------------*/
305
306#endif /* AVLTree.h included */
307
308/*--------------------------------------------------------------------------*/
309/*--------------------------- End File AVLTree.h ---------------------------*/
310/*--------------------------------------------------------------------------*/
The classic problems on binary trees and on binary search trees, written once for any representation ...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
The balanced binary search trees whose only balancing operation is join, beyond the book,...
a DICTIONARY on an AVL tree, with the operations based on join [see Joinable]
Definition AVLTree.h:95
const_iterator cbegin(void) const
as begin()
Definition AVLTree.h:264
bool empty(void) const
true if there is no key
V mapped_type
the type of the values
Definition AVLTree.h:246
const_iterator end(void) const
the iterator past the largest key
Definition AVLTree.h:260
std::ptrdiff_t difference_type
the type of the distances
Definition AVLTree.h:248
V Value
the type of the values
Definition AVLTree.h:102
const_iterator begin(void) const
the iterator on the smallest key
Definition AVLTree.h:256
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
K Key
the type of the keys
Definition AVLTree.h:101
PathIterator< AVLTree, Shape > const_iterator
the constant iterator by increasing key, along the path from the root
Definition AVLTree.h:252
std::optional< V > lookup(const K &k) const
the value of , or nothing, by the descent of a binary search tree, in time
PairRef< const K &, const V & > reference
references, by value
Definition AVLTree.h:249
std::size_t size(void) const
the number of keys
const_iterator iterator
the constant iterator
Definition AVLTree.h:253
std::optional< std::pair< K, V > > lowerBound(const K &k) const
the smallest key not smaller than and its value, or nothing (the find( x ) of an SSet of Morin),...
int height(void) const
the height of the tree, -1 if it is empty
bool contains(const K &k) const
true if is there
const K * findKey(const K &k) const
the address of the key , nil if absent, in time: the find( x ) of a USet of Morin [see DictionarySet...
const_iterator cend(void) const
as end()
Definition AVLTree.h:266
std::pair< std::size_t, std::size_t > rebalancings(void) const
the rebalancings of insert() and remove() so far, i.e., the nodes found unbalanced on their paths and...
Definition AVLTree.h:202
const K * lowerBoundKey(const K &k) const
the address of the smallest key not smaller than , nil if none, in time: the find( x ) of an SSet of...
K key_type
the type of the keys
Definition AVLTree.h:244
void insert(const K &k, const V &v)
associates to , in time
reference const_reference
the same
Definition AVLTree.h:250
void remove(const K &k)
removes , if it is there, in time
std::size_t size_type
the type of the sizes
Definition AVLTree.h:247
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition AVLTree.h:242
the operations based on join of the tree T of keys Key and values Value, whose nodes A adapts [see Jo...
Definition JoinAlgorithms.h:860
a bidirectional iterator on the nodes of a binary tree of the owner O in symmetric order,...
Definition Iterators.h:867
std::conditional_t< detail::plainPair< A, B, VA, VB >, std::pair< A, B >, detail::PairProxy< A, B, VA, VB > > PairRef
the pair that an iterator reads by value, in general two references into the container,...
Definition Iterators.h:648
Definition ADT.h:96
the adaptor of the nodes of an AVLTree [see JoinAdaptor]
Definition AVLTree.h:88
the shape of the tree, for PathIterator
Definition AVLTree.h:228
static auto read(const auto *n)
the pair of the node n
Definition AVLTree.h:236
static auto left(const auto *n)
the left child of the node n
Definition AVLTree.h:232
static auto root(const auto &o)
the root of the tree o
Definition AVLTree.h:230
static auto right(const auto *n)
the right child of the node n
Definition AVLTree.h:234