ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MeasuredSearchTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File MeasuredSearchTree.h -----------------------*/
3/*--------------------------------------------------------------------------*/
45/*--------------------------------------------------------------------------*/
46/*----------------------------- DEFINITIONS --------------------------------*/
47/*--------------------------------------------------------------------------*/
48
49#ifndef __ADS_MeasuredSearchTree
50 #define __ADS_MeasuredSearchTree
51/* self-identification: #endif constant */
52
53/*--------------------------------------------------------------------------*/
54/*------------------------------ INCLUDES ----------------------------------*/
55/*--------------------------------------------------------------------------*/
56
57#include "ADS/Contract.h"
58#include "ADS/Iterators.h"
60
61#include <algorithm>
62#include <concepts>
63#include <cstddef>
64#include <optional>
65#include <utility>
66#include <vector>
67
68/*--------------------------------------------------------------------------*/
69/*--------------------------- NAMESPACE ------------------------------------*/
70/*--------------------------------------------------------------------------*/
71
72namespace ADS
73{
74
75/*--------------------------------------------------------------------------*/
76/*------------------------ CLASS MeasuredSearchTree ------------------------*/
77/*--------------------------------------------------------------------------*/
78
81template< std::totally_ordered K , class V >
83{
84
85/*--------------------------------------------------------------------------*/
86 public:
87 using Key = K;
88 using Value = V;
89
91 struct Measures
92 {
93 std::size_t size;
94 std::size_t depth;
95 long height;
96 };
97
99 MeasuredSearchTree( void ) = default;
100
103
106
109 : f_root( other.f_root ) , f_size( other.f_size ) {
110 other.f_root = nullptr;
111 other.f_size = 0;
112 }
113
116
118 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
119
137 void insert( const K & k , const V & v );
138
160 void remove( const K & k );
161
163 [[nodiscard]] bool contains( const K & k ) const;
164
166 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
167
169 [[nodiscard]] long height( void ) const;
170
172 [[nodiscard]] std::optional< Measures > measures( const K & k ) const;
173
176 [[nodiscard]] std::optional< std::pair< K , V > >
177 lowerBound( const K & k ) const;
178
182 [[nodiscard]] const K * findKey( const K & k ) const;
183
187 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
188
192 [[nodiscard]] std::vector< std::pair< K , V > > getLE( const K & x ) const;
193
195 template< class F >
196 void forEach( F && f ) const;
197
198/*--------------------------------------------------------------------------*/
199 private:
201 struct Node
202 {
203 K key;
204 V value;
205 Node * left = nullptr;
206 Node * right = nullptr;
207 Node * parent = nullptr;
208 std::size_t size = 1;
209 std::size_t depth = 0;
210 long height = 0;
211 };
212
213 public:
214/*------------------------- THE STANDARD INTERFACE -------------------------*/
215
217 struct Shape
218 {
220 static auto root( const auto & o ) { return( o.f_root ); }
222 static auto left( const auto * n ) { return( n->left ); }
224 static auto right( const auto * n ) { return( n->right ); }
226 static auto read( const auto * n ) {
227 return( PairRef< const K & , const V & >( n->key , n->value ) );
228 }
229 };
230
232 using value_type = std::pair< K , V >;
234 using key_type = K;
236 using mapped_type = V;
237 using size_type = std::size_t;
238 using difference_type = std::ptrdiff_t;
244
246 [[nodiscard]] const_iterator begin( void ) const {
247 return( const_iterator( this , true ) );
248 }
249
250 [[nodiscard]] const_iterator end( void ) const {
251 return( const_iterator( this , false ) );
252 }
253
254 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
256 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
257
258 private:
260 Node * findLast( const K & k ) const;
261
263 static long fresh( const Node * u ) {
264 return( 1 + std::max( u->left ? u->left->height : -1L ,
265 u->right ? u->right->height : -1L ) );
266 }
267
269 [[nodiscard]] bool valid( void ) const;
270
271
272 Node * f_root = nullptr;
273 std::size_t f_size = 0;
274
275 }; // end( class MeasuredSearchTree )
276
277/*--------------------------------------------------------------------------*/
278
279} // end( namespace ADS )
280
281/*--------------------------------------------------------------------------*/
282/*-------------------------- IMPLEMENTATION --------------------------------*/
283/*--------------------------------------------------------------------------*/
284
285#include "ADS/tree/impl/MeasuredSearchTree.tpp"
286
287/*--------------------------------------------------------------------------*/
288
289#endif /* MeasuredSearchTree.h included */
290
291/*--------------------------------------------------------------------------*/
292/*---------------------- End File MeasuredSearchTree.h ---------------------*/
293/*--------------------------------------------------------------------------*/
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...
MeasuredSearchTree(MeasuredSearchTree &&other) noexcept
the nodes of other, which is left empty
Definition MeasuredSearchTree.h:108
const_iterator iterator
the constant iterator
Definition MeasuredSearchTree.h:243
const_iterator end(void) const
the iterator past the largest key
Definition MeasuredSearchTree.h:250
const_iterator cbegin(void) const
as begin()
Definition MeasuredSearchTree.h:254
std::optional< Measures > measures(const K &k) const
the measures of the node of , or nothing, read in it
V Value
the type of the values
Definition MeasuredSearchTree.h:88
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
void insert(const K &k, const V &v)
associates to , in time
const_iterator cend(void) const
as end()
Definition MeasuredSearchTree.h:256
std::size_t size(void) const
the number of keys
Definition MeasuredSearchTree.h:166
~MeasuredSearchTree()
frees the nodes
std::size_t size_type
the type of the sizes
Definition MeasuredSearchTree.h:237
K Key
the type of the keys
Definition MeasuredSearchTree.h:87
MeasuredSearchTree & operator=(const MeasuredSearchTree &)=delete
no copy, since the nodes are linked by pointers
MeasuredSearchTree(const MeasuredSearchTree &)=delete
no copy, since the nodes are linked by pointers
PathIterator< MeasuredSearchTree, Shape > const_iterator
the constant iterator by increasing key, along the path from the root
Definition MeasuredSearchTree.h:242
const_iterator begin(void) const
the iterator on the smallest key
Definition MeasuredSearchTree.h:246
PairRef< const K &, const V & > reference
references, by value
Definition MeasuredSearchTree.h:239
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...
MeasuredSearchTree(void)=default
the empty tree
long height(void) const
the height, if empty, read at the root in
V mapped_type
the type of the values
Definition MeasuredSearchTree.h:236
bool contains(const K &k) const
true if is there
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...
void remove(const K &k)
removes , if it is there, in time
std::vector< std::pair< K, V > > getLE(const K &x) const
the pairs whose keys are not larger than , in increasing order, by lessOrEqual(), in time for pairs...
reference const_reference
the same
Definition MeasuredSearchTree.h:240
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),...
std::ptrdiff_t difference_type
the type of the distances
Definition MeasuredSearchTree.h:238
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition MeasuredSearchTree.h:232
std::optional< V > lookup(const K &k) const
the value of , or nothing, in time
K key_type
the type of the keys
Definition MeasuredSearchTree.h:234
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 measures of a node
Definition MeasuredSearchTree.h:92
std::size_t depth
the edges from the root to it
Definition MeasuredSearchTree.h:94
long height
the height of its subtree, 0 for a leaf
Definition MeasuredSearchTree.h:95
std::size_t size
the nodes of its subtree
Definition MeasuredSearchTree.h:93
the shape of the tree, for PathIterator
Definition MeasuredSearchTree.h:218
static auto right(const auto *n)
the right child of the node n
Definition MeasuredSearchTree.h:224
static auto left(const auto *n)
the left child of the node n
Definition MeasuredSearchTree.h:222
static auto read(const auto *n)
the pair of the node n
Definition MeasuredSearchTree.h:226
static auto root(const auto &o)
the root of the tree o
Definition MeasuredSearchTree.h:220