ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Treap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Treap.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
62/*--------------------------------------------------------------------------*/
63/*----------------------------- DEFINITIONS --------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66#ifndef __ADS_Treap
67 #define __ADS_Treap
68/* self-identification: #endif constant */
69
70/*--------------------------------------------------------------------------*/
71/*------------------------------ INCLUDES ----------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74#include "ADS/Contract.h"
75#include "ADS/Iterators.h"
79
80#include <algorithm>
81#include <concepts>
82#include <cstddef>
83#include <cstdint>
84#include <memory>
85#include <optional>
86#include <utility>
87#include <vector>
88
89/*--------------------------------------------------------------------------*/
90/*--------------------------- NAMESPACE ------------------------------------*/
91/*--------------------------------------------------------------------------*/
92
93namespace ADS
94{
95
96/*--------------------------------------------------------------------------*/
97/*------------------------------ CLASS Treap -------------------------------*/
98/*--------------------------------------------------------------------------*/
99
100template< std::totally_ordered K , class V >
101class Treap;
102
104template< std::totally_ordered K , class V >
106
109template< std::totally_ordered K , class V >
110class Treap : public Joinable< Treap< K , V > , TreapNodes< K , V > , K , V >
111{
112
113 friend struct TreapNodes< K , V >;
114
115/*--------------------------------------------------------------------------*/
116 public:
117 using Key = K;
118 using Value = V;
119
121 explicit Treap( std::uint64_t seed = 1 );
122
125 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
126
148 void insert( const K & k , const V & v );
149
174 void remove( const K & k );
175
177 [[nodiscard]] bool contains( const K & k ) const;
178
180 [[nodiscard]] std::size_t size( void ) const;
181
183 [[nodiscard]] int height( void ) const;
184
186 template< class F >
187 void forEach( F && f ) const;
188
189/*-------------------------- THE EXERCISES OF MORIN ------------------------*/
190
194 [[nodiscard]] std::optional< std::pair< K , V > >
195 lowerBound( const K & k ) const;
196
200 [[nodiscard]] const K * findKey( const K & k ) const;
201
205 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
206
224 [[nodiscard]] std::pair< K , V > select( std::size_t i ) const;
225
229 [[nodiscard]] std::size_t rank( const K & k ) const;
230
256 [[nodiscard]] static Treap
257 fromSorted( const std::vector< std::pair< K , V > > & A ,
258 std::uint64_t seed = 1 );
259
260/*--------------------------------------------------------------------------*/
261 private:
263 struct Node
264 {
265 K key;
266 V value;
267 std::uint64_t priority;
268 std::size_t s = 1;
269 std::unique_ptr< Node > left ,
270 right;
271 };
272
273 public:
274/*------------------------- THE STANDARD INTERFACE -------------------------*/
275
276 // a constant view of the pairs ( key , value ) by increasing key,
277 // bidirectional, along the path from the root (the nodes have no parent)
278
280 struct Shape
281 {
283 static auto root( const auto & o ) { return( o.f_root.get() ); }
285 static auto left( const auto * n ) { return( n->left.get() ); }
287 static auto right( const auto * n ) { return( n->right.get() ); }
289 static auto read( const auto * n ) {
290 return( PairRef< const K & , const V & >( n->key , n->value ) );
291 }
292 };
293
295 using value_type = std::pair< K , V >;
297 using key_type = K;
299 using mapped_type = V;
300 using size_type = std::size_t;
301 using difference_type = std::ptrdiff_t;
307
309 [[nodiscard]] const_iterator begin( void ) const {
310 return( const_iterator( this , true ) );
311 }
312
313 [[nodiscard]] const_iterator end( void ) const {
314 return( const_iterator( this , false ) );
315 }
316
317 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
319 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
320
321 private:
322 using Ptr = std::unique_ptr< Node >;
323
324 static std::size_t sz( const Ptr & t ) { return( t ? t->s : 0 ); }
325 static void update( Ptr & t ) { t->s = 1 + sz( t->left ) + sz( t->right ); }
326 static void rotateRight( Ptr & t );
327 static void rotateLeft( Ptr & t );
328 bool insert( Ptr & t , const K & k , const V & v );
329 static bool remove( Ptr & t , const K & k );
330 static int height( const Ptr & t );
331 template< class F >
332 static void visit( const Ptr & t , F & f );
333 static bool valid( const Ptr & t , const K * lo , const K * hi );
334
335
336 Ptr f_root;
337 std::size_t f_size = 0;
338 SplitMix64 f_g;
339
340 }; // end( class Treap )
341
342/*--------------------------------------------------------------------------*/
343
344} // end( namespace ADS )
345
346/*--------------------------------------------------------------------------*/
347/*-------------------------- IMPLEMENTATION --------------------------------*/
348/*--------------------------------------------------------------------------*/
349
350#include "ADS/tree/impl/Treap.tpp"
351
352/*--------------------------------------------------------------------------*/
353
354#endif /* Treap.h included */
355
356/*--------------------------------------------------------------------------*/
357/*---------------------------- End File Treap.h ----------------------------*/
358/*--------------------------------------------------------------------------*/
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,...
Hashing with random tables, beyond the book:
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
K Key
Definition Treap.h:117
std::pair< K, V > select(std::size_t i) const
the pair of rank , i.e., with smaller keys, in expected time (exercise 7.10 of Morin)
const_iterator cend(void) const
as end()
Definition Treap.h:319
static Treap fromSorted(const std::vector< std::pair< K, V > > &A, std::uint64_t seed=1)
the treap of the pairs of , by increasing keys, in time (exercise 7.8 of Morin): the same tree that ...
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
reference const_reference
Definition Treap.h:303
std::size_t rank(const K &k) const
the number of keys smaller than , the sizes of the left subtrees passed on the right along the search...
Treap(std::uint64_t seed=1)
std::size_t size(void) const
the number of keys
V mapped_type
Definition Treap.h:299
void insert(const K &k, const V &v)
associates to , in expected time
bool contains(const K &k) const
true if is there
int height(void) const
the height, -1 if empty
const_iterator end(void) const
the iterator past the largest key
Definition Treap.h:313
std::size_t size_type
Definition Treap.h:300
const_iterator begin(void) const
the iterator on the smallest key
Definition Treap.h:309
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),...
std::ptrdiff_t difference_type
Definition Treap.h:301
const_iterator cbegin(void) const
as begin()
Definition Treap.h:317
PathIterator< Treap, Shape > const_iterator
Definition Treap.h:305
std::optional< V > lookup(const K &k) const
the value of , or nothing, by the descent of a binary search tree, in expected time
std::pair< K, V > value_type
Definition Treap.h:295
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 remove(const K &k)
removes , if it is there, in expected time
K key_type
Definition Treap.h:297
V Value
Definition Treap.h:118
const_iterator iterator
Definition Treap.h:306
PairRef< const K &, const V & > reference
Definition Treap.h:302
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...
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 a Treap [see JoinAdaptor]
Definition Treap.h:105
the shape of the tree, for PathIterator
Definition Treap.h:281
static auto left(const auto *n)
the left child of the node n
Definition Treap.h:285
static auto right(const auto *n)
the right child of the node n
Definition Treap.h:287
static auto read(const auto *n)
the pair of the node n
Definition Treap.h:289
static auto root(const auto &o)
the root of the tree o
Definition Treap.h:283