ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LeftLeaningRedBlackTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------- File LeftLeaningRedBlackTree.h ---------------------*/
3/*--------------------------------------------------------------------------*/
76/*--------------------------------------------------------------------------*/
77/*----------------------------- DEFINITIONS --------------------------------*/
78/*--------------------------------------------------------------------------*/
79
80#ifndef __ADS_LeftLeaningRedBlackTree
81 #define __ADS_LeftLeaningRedBlackTree
82/* self-identification: #endif constant */
83
84/*--------------------------------------------------------------------------*/
85/*------------------------------ INCLUDES ----------------------------------*/
86/*--------------------------------------------------------------------------*/
87
88#include "ADS/Contract.h"
89#include "ADS/Iterators.h"
90
91#include <array>
92#include <concepts>
93#include <cstddef>
94#include <cstdint>
95#include <optional>
96#include <utility>
97#include <vector>
98
99/*--------------------------------------------------------------------------*/
100/*--------------------------- NAMESPACE ------------------------------------*/
101/*--------------------------------------------------------------------------*/
102
103namespace ADS
104{
105
106/*--------------------------------------------------------------------------*/
107/*--------------------- CLASS LeftLeaningRedBlackTree ----------------------*/
108/*--------------------------------------------------------------------------*/
109
112template< std::totally_ordered K , class V >
114{
115
116/*--------------------------------------------------------------------------*/
117 public:
118 using Key = K;
119 using Value = V;
120
122 LeftLeaningRedBlackTree( void ) = default;
123
126
130
133 : f_root( other.f_root ) , f_n( other.f_n ) {
134 other.f_root = nullptr;
135 other.f_n = 0;
136 }
137
140
142 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
143
165 void insert( const K & k , const V & v );
166
208 void remove( const K & k );
209
211 [[nodiscard]] bool contains( const K & k ) const;
212
214 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
215
217 [[nodiscard]] long height( void ) const;
218
220 [[nodiscard]] long blackHeight( void ) const;
221
224 [[nodiscard]] std::array< std::size_t , 3 > twoFourDegrees( void ) const;
225
228 [[nodiscard]] std::optional< std::pair< K , V > >
229 lowerBound( const K & k ) const;
230
234 [[nodiscard]] const K * findKey( const K & k ) const;
235
239 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
240
261
263 template< class F >
264 void forEach( F && f ) const;
265
266/*--------------------------------------------------------------------------*/
267 private:
268 static constexpr signed char kRed = 0;
269 static constexpr signed char kBlack = 1;
270
272 struct Link
273 {
274 Link * left = nullptr;
275 Link * right = nullptr;
276 Link * parent = nullptr;
277 signed char color = kRed;
278 };
279
281 struct Node : Link
282 {
283 K key;
284 V value;
286 Node( const K & k , const V & v ) : key( k ) , value( v ) { }
287 };
288
289 public:
290/*------------------------- THE STANDARD INTERFACE -------------------------*/
291
293 struct Shape
294 {
296 static const Node * root( const auto & o ) {
297 return( static_cast< const Node * >( o.f_root ) );
298 }
299
300 static const Node * left( const Node * n ) {
301 return( static_cast< const Node * >( n->left ) );
302 }
303
304 static const Node * right( const Node * n ) {
305 return( static_cast< const Node * >( n->right ) );
306 }
307
308 static auto read( const Node * n ) {
309 return( PairRef< const K & , const V & >( n->key , n->value ) );
310 }
311 };
312
314 using value_type = std::pair< K , V >;
316 using key_type = K;
318 using mapped_type = V;
319 using size_type = std::size_t;
320 using difference_type = std::ptrdiff_t;
326
328 [[nodiscard]] const_iterator begin( void ) const {
329 return( const_iterator( this , true ) );
330 }
331
332 [[nodiscard]] const_iterator end( void ) const {
333 return( const_iterator( this , false ) );
334 }
335
336 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
338 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
339
340 private:
341 static signed char color( const Link * u ) {
342 return( u ? u->color : kBlack );
343 }
344 static const K & key( const Link * u ) {
345 return( static_cast< const Node * >( u )->key );
346 }
349 Link *& place( Link * u ) {
350 return( ! u->parent
351 ? f_root
352 : ( u->parent->left == u ? u->parent->left : u->parent->right ) );
353 }
354 void rotateLeft( Link * u );
355 void rotateRight( Link * u );
356 static void pushBlack( Link * u ) {
357 --u->color;
358 ++u->left->color;
359 ++u->right->color;
360 }
361 static void pullBlack( Link * u ) {
362 ++u->color;
363 --u->left->color;
364 --u->right->color;
365 }
366 void flipLeft( Link * u ) {
367 std::swap( u->color , u->right->color );
368 rotateLeft( u );
369 }
370 void flipRight( Link * u ) {
371 std::swap( u->color , u->left->color );
372 rotateRight( u );
373 }
374 void addFixup( Link * u );
375 void removeFixup( Link * u );
376 Link * removeCase2( Link * u );
377 Link * removeCase3( Link * u );
378 Link * findLast( const K & k ) const;
379
381 [[nodiscard]] bool valid( void ) const;
382
383
384 Link * f_root = nullptr;
385 std::size_t f_n = 0;
386
387 }; // end( class LeftLeaningRedBlackTree )
388
389/*--------------------------------------------------------------------------*/
390
391} // end( namespace ADS )
392
393/*--------------------------------------------------------------------------*/
394/*-------------------------- IMPLEMENTATION --------------------------------*/
395/*--------------------------------------------------------------------------*/
396
398#include "ADS/tree/impl/LeftLeaningRedBlackTree.tpp"
399
400/*--------------------------------------------------------------------------*/
401
402#endif /* LeftLeaningRedBlackTree.h included */
403
404/*--------------------------------------------------------------------------*/
405/*------------------- End File LeftLeaningRedBlackTree.h -------------------*/
406/*--------------------------------------------------------------------------*/
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...
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
const_iterator end(void) const
the iterator past the largest key
Definition LeftLeaningRedBlackTree.h:332
std::ptrdiff_t difference_type
the type of the distances
Definition LeftLeaningRedBlackTree.h:320
LeftLeaningRedBlackTree(void)=default
the empty tree
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition LeftLeaningRedBlackTree.h:314
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::size_t size(void) const
the number of keys
Definition LeftLeaningRedBlackTree.h:214
void remove(const K &k)
removes , if it is there, in time
const_iterator iterator
the constant iterator
Definition LeftLeaningRedBlackTree.h:325
long blackHeight(void) const
the black height, the black nodes on a path from the root to nil
void absorb(LeftLeaningRedBlackTree &t)
the pairs of appended and left empty, in time (exercises 9.10 and 9.11 of Morin)
PathIterator< LeftLeaningRedBlackTree, Shape > const_iterator
the constant iterator by increasing key, along the path from the root
Definition LeftLeaningRedBlackTree.h:324
const_iterator cend(void) const
as end()
Definition LeftLeaningRedBlackTree.h:338
reference const_reference
the same
Definition LeftLeaningRedBlackTree.h:322
std::array< std::size_t, 3 > twoFourDegrees(void) const
the numbers of the nodes of degree 2, 3 and 4 of the 2-4 tree that the tree simulates (exercises 9....
void insert(const K &k, const V &v)
associates to , in time
~LeftLeaningRedBlackTree()
frees the nodes
LeftLeaningRedBlackTree(LeftLeaningRedBlackTree &&other) noexcept
the nodes of other, which is left empty
Definition LeftLeaningRedBlackTree.h:132
std::size_t size_type
the type of the sizes
Definition LeftLeaningRedBlackTree.h:319
K key_type
the type of the keys
Definition LeftLeaningRedBlackTree.h:316
V Value
the type of the values
Definition LeftLeaningRedBlackTree.h:119
const_iterator cbegin(void) const
as begin()
Definition LeftLeaningRedBlackTree.h:336
LeftLeaningRedBlackTree & operator=(const LeftLeaningRedBlackTree &)=delete
no copy, since the nodes are linked by pointers
LeftLeaningRedBlackTree(const LeftLeaningRedBlackTree &)=delete
no copy, since the nodes are linked by pointers
V mapped_type
the type of the values
Definition LeftLeaningRedBlackTree.h:318
K Key
the type of the keys
Definition LeftLeaningRedBlackTree.h:118
bool contains(const K &k) const
true if is there
PairRef< const K &, const V & > reference
references, by value
Definition LeftLeaningRedBlackTree.h:321
const_iterator begin(void) const
the iterator on the smallest key
Definition LeftLeaningRedBlackTree.h:328
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...
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...
long height(void) const
the height, if empty, in
std::optional< V > lookup(const K &k) const
the value of , or nothing, in time
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 shape of the tree, for PathIterator
Definition LeftLeaningRedBlackTree.h:294
static auto read(const Node *n)
the pair of the node n
Definition LeftLeaningRedBlackTree.h:308
static const Node * right(const Node *n)
the right child of the node n
Definition LeftLeaningRedBlackTree.h:304
static const Node * left(const Node *n)
the left child of the node n
Definition LeftLeaningRedBlackTree.h:300
static const Node * root(const auto &o)
the root of the tree o
Definition LeftLeaningRedBlackTree.h:296