80#ifndef __ADS_LeftLeaningRedBlackTree
81 #define __ADS_LeftLeaningRedBlackTree
112template< std::totally_ordered K ,
class V >
133 : f_root( other.f_root ) , f_n( other.f_n ) {
134 other.f_root =
nullptr;
142 [[nodiscard]] std::optional< V >
lookup(
const K & k )
const;
165 void insert(
const K & k ,
const V & v );
214 [[nodiscard]] std::size_t
size(
void )
const {
return( f_n ); }
228 [[nodiscard]] std::optional< std::pair< K , V > >
234 [[nodiscard]]
const K *
findKey(
const K & k )
const;
268 static constexpr signed char kRed = 0;
269 static constexpr signed char kBlack = 1;
274 Link * left =
nullptr;
275 Link * right =
nullptr;
276 Link * parent =
nullptr;
277 signed char color = kRed;
286 Node(
const K & k ,
const V & v ) : key( k ) , value( v ) { }
296 static const Node *
root(
const auto & o ) {
297 return(
static_cast< const Node *
>( o.f_root ) );
300 static const Node *
left(
const Node * n ) {
301 return(
static_cast< const Node *
>( n->left ) );
304 static const Node *
right(
const Node * n ) {
305 return(
static_cast< const Node *
>( n->right ) );
308 static auto read(
const Node * n ) {
341 static signed char color(
const Link * u ) {
342 return( u ? u->color : kBlack );
344 static const K & key(
const Link * u ) {
345 return(
static_cast< const Node *
>( u )->key );
349 Link *& place( Link * u ) {
352 : ( u->parent->left == u ? u->parent->left : u->parent->right ) );
354 void rotateLeft( Link * u );
355 void rotateRight( Link * u );
356 static void pushBlack( Link * u ) {
361 static void pullBlack( Link * u ) {
366 void flipLeft( Link * u ) {
367 std::swap( u->color , u->right->color );
370 void flipRight( Link * u ) {
371 std::swap( u->color , u->left->color );
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;
381 [[nodiscard]]
bool valid(
void )
const;
384 Link * f_root =
nullptr;
398#include "ADS/tree/impl/LeftLeaningRedBlackTree.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...
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
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