ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RandomizedSearchTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------- File RandomizedSearchTree.h ----------------------*/
3/*--------------------------------------------------------------------------*/
56/*--------------------------------------------------------------------------*/
57/*----------------------------- DEFINITIONS --------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#ifndef __ADS_RandomizedSearchTree
61 #define __ADS_RandomizedSearchTree
62/* self-identification: #endif constant */
63
64/*--------------------------------------------------------------------------*/
65/*------------------------------ INCLUDES ----------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68#include "ADS/Contract.h"
69#include "ADS/Iterators.h"
72
73#include <algorithm>
74#include <concepts>
75#include <cstddef>
76#include <cstdint>
77#include <memory>
78#include <optional>
79#include <utility>
80
81/*--------------------------------------------------------------------------*/
82/*--------------------------- NAMESPACE ------------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85namespace ADS
86{
87
88/*--------------------------------------------------------------------------*/
89/*----------------------- CLASS RandomizedSearchTree -----------------------*/
90/*--------------------------------------------------------------------------*/
91
93template< std::totally_ordered K , class V >
95{
96
97/*--------------------------------------------------------------------------*/
98 public:
99 using Key = K;
100 using Value = V;
101
103 explicit RandomizedSearchTree( std::uint64_t seed = 1 ) : f_g( seed ) { }
104
106 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
107
135 void insert( const K & k , const V & v );
136
155 void remove( const K & k );
156
158 [[nodiscard]] bool contains( const K & k ) const;
159
161 [[nodiscard]] std::size_t size( void ) const { return( sz( f_root ) ); }
162
164 [[nodiscard]] int height( void ) const;
165
168 [[nodiscard]] std::optional< std::pair< K , V > >
169 lowerBound( const K & k ) const;
170
174 [[nodiscard]] const K * findKey( const K & k ) const;
175
179 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
180
183 [[nodiscard]] std::pair< K , V > select( std::size_t i ) const;
184
186 [[nodiscard]] std::size_t rank( const K & k ) const;
187
189 template< class F >
190 void forEach( F && f ) const;
191
192/*--------------------------------------------------------------------------*/
193 private:
195 struct Node
196 {
197 K key;
198 V value;
199 std::size_t s = 1;
200 std::unique_ptr< Node > left ,
201 right;
202 };
203
204 public:
205/*------------------------- THE STANDARD INTERFACE -------------------------*/
206
208 struct Shape
209 {
211 static auto root( const auto & o ) { return( o.f_root.get() ); }
213 static auto left( const auto * n ) { return( n->left.get() ); }
215 static auto right( const auto * n ) { return( n->right.get() ); }
217 static auto read( const auto * n ) {
218 return( PairRef< const K & , const V & >( n->key , n->value ) );
219 }
220 };
221
223 using value_type = std::pair< K , V >;
225 using key_type = K;
227 using mapped_type = V;
228 using size_type = std::size_t;
229 using difference_type = std::ptrdiff_t;
235
237 [[nodiscard]] const_iterator begin( void ) const {
238 return( const_iterator( this , true ) );
239 }
240
241 [[nodiscard]] const_iterator end( void ) const {
242 return( const_iterator( this , false ) );
243 }
244
245 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
247 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
248
249 private:
250 using Ptr = std::unique_ptr< Node >;
251
252 static std::size_t sz( const Ptr & t ) { return( t ? t->s : 0 ); }
253 static void update( Ptr & t ) { t->s = 1 + sz( t->left ) + sz( t->right ); }
254 Node * find( const K & k ) const;
255 void insert( Ptr & t , Ptr & u );
256 static std::pair< Ptr , Ptr > split( Ptr t , const K & k );
257 Ptr join( Ptr a , Ptr b );
258 bool remove( Ptr & t , const K & k );
259 static int height( const Ptr & t );
260 static bool valid( const Ptr & t , const K * lo , const K * hi );
261
262
263 Ptr f_root;
264 SplitMix64 f_g;
265
266 }; // end( class RandomizedSearchTree )
267
268/*--------------------------------------------------------------------------*/
269
270} // end( namespace ADS )
271
272/*--------------------------------------------------------------------------*/
273/*-------------------------- IMPLEMENTATION --------------------------------*/
274/*--------------------------------------------------------------------------*/
275
277#include "ADS/tree/impl/RandomizedSearchTree.tpp"
278
279/*--------------------------------------------------------------------------*/
280
281#endif /* RandomizedSearchTree.h included */
282
283/*--------------------------------------------------------------------------*/
284/*--------------------- End File RandomizedSearchTree.h --------------------*/
285/*--------------------------------------------------------------------------*/
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 engineered random numbers, beyond the book: generators of 64 bits that cost a few instructions an...
Hashing with random tables, beyond the book:
a bidirectional iterator on the nodes of a binary tree of the owner O in symmetric order,...
Definition Iterators.h:867
const_iterator begin(void) const
the iterator on the smallest key
Definition RandomizedSearchTree.h:237
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
K Key
the type of the keys
Definition RandomizedSearchTree.h:99
PairRef< const K &, const V & > reference
references, by value
Definition RandomizedSearchTree.h:230
const_iterator cbegin(void) const
as begin()
Definition RandomizedSearchTree.h:245
std::size_t size(void) const
the number of keys
Definition RandomizedSearchTree.h:161
std::size_t rank(const K &k) const
the number of keys smaller than , by the sizes
std::pair< K, V > select(std::size_t i) const
the pair of rank , by the sizes, as Treap::select()
std::ptrdiff_t difference_type
the type of the distances
Definition RandomizedSearchTree.h:229
int height(void) const
the height, if empty, in
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition RandomizedSearchTree.h:223
reference const_reference
the same
Definition RandomizedSearchTree.h:231
std::optional< V > lookup(const K &k) const
the value of , or nothing, in expected time
V mapped_type
the type of the values
Definition RandomizedSearchTree.h:227
bool contains(const K &k) const
true if is there
const_iterator end(void) const
the iterator past the largest key
Definition RandomizedSearchTree.h:241
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)
const_iterator iterator
the constant iterator
Definition RandomizedSearchTree.h:234
std::size_t size_type
the type of the sizes
Definition RandomizedSearchTree.h:228
PathIterator< RandomizedSearchTree, Shape > const_iterator
the constant iterator by increasing key, along the path from the root
Definition RandomizedSearchTree.h:233
V Value
the type of the values
Definition RandomizedSearchTree.h:100
const K * findKey(const K &k) const
the address of the key , nil if absent, in expected time: the find( x ) of a USet of Morin [see Dict...
void insert(const K &k, const V &v)
associates to , in expected time
K key_type
the type of the keys
Definition RandomizedSearchTree.h:225
const K * lowerBoundKey(const K &k) const
the address of the smallest key not smaller than , nil if none, in expected time: the find( x ) of a...
void remove(const K &k)
removes , if it is there, in expected time
RandomizedSearchTree(std::uint64_t seed=1)
the empty tree, its random choices drawn from the seed
Definition RandomizedSearchTree.h:103
const_iterator cend(void) const
as end()
Definition RandomizedSearchTree.h:247
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 shape of the tree, for PathIterator
Definition RandomizedSearchTree.h:209
static auto read(const auto *n)
the pair of the node n
Definition RandomizedSearchTree.h:217
static auto root(const auto &o)
the root of the tree o
Definition RandomizedSearchTree.h:211
static auto right(const auto *n)
the right child of the node n
Definition RandomizedSearchTree.h:215
static auto left(const auto *n)
the left child of the node n
Definition RandomizedSearchTree.h:213