100template< std::totally_ordered K ,
class V >
104template< std::totally_ordered K ,
class V >
109template< std::totally_ordered K ,
class V >
110class Treap :
public Joinable< Treap< K , V > , TreapNodes< K , V > , K , V >
121 explicit Treap( std::uint64_t seed = 1 );
125 [[nodiscard]] std::optional< V >
lookup(
const K & k )
const;
148 void insert(
const K & k ,
const V & v );
180 [[nodiscard]] std::size_t
size(
void )
const;
194 [[nodiscard]] std::optional< std::pair< K , V > >
200 [[nodiscard]]
const K *
findKey(
const K & k )
const;
224 [[nodiscard]] std::pair< K , V >
select( std::size_t i )
const;
229 [[nodiscard]] std::size_t
rank(
const K & k )
const;
256 [[nodiscard]]
static Treap
258 std::uint64_t seed = 1 );
267 std::uint64_t priority;
269 std::unique_ptr< Node > left ,
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 ) {
322 using Ptr = std::unique_ptr< Node >;
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 );
332 static void visit(
const Ptr & t , F & f );
333 static bool valid(
const Ptr & t ,
const K * lo ,
const K * hi );
337 std::size_t f_size = 0;
350#include "ADS/tree/impl/Treap.tpp"
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
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