99#ifndef __ADS_SearchTree
100 #define __ADS_SearchTree
116#include <type_traits>
137template< std::totally_ordered K ,
class V ,
bool RB >
141template< std::totally_ordered K ,
class V ,
bool RB >
151template< std::totally_ordered K ,
class V ,
bool RB >
153 :
public std::conditional_t<
155 Joinable< SearchTree< K , V , RB > , SearchTreeNodes< K , V , RB > ,
207 [[nodiscard]] std::optional< V >
lookup(
const K & k )
const;
270 void insert(
const K & k ,
const V & v );
334 [[nodiscard]] std::optional< std::pair< K , V > >
341 [[nodiscard]]
const K *
findKey(
const K & k )
const;
350 [[nodiscard]] std::size_t
size(
void )
const;
353 [[nodiscard]]
bool empty(
void )
const;
364 [[nodiscard]] Link *
root(
void )
const;
367 [[nodiscard]]
bool isNil(
const Link * u )
const;
373 [[nodiscard]]
static const K &
key(
const Link * u );
377 [[nodiscard]]
static const V &
value(
const Link * u );
418 [[nodiscard]] Link *
min(
void )
const;
422 [[nodiscard]] Link *
max(
void )
const;
482 return(
reference( f_t->key( f_x ) , f_t->value( f_x ) ) );
486 f_x = f_t->successorNode( f_x );
491 f_x = f_t->isNil( f_x ) ? f_t->max() : f_t->predecessorNode( f_x );
509 return( f_x == o.f_x );
513 [[nodiscard]]
Link *
node(
void )
const {
return( f_x ); }
517 Link * f_x =
nullptr;
533 [[nodiscard]] const_iterator
begin(
void )
const {
534 return( const_iterator(
this ,
min() ) );
537 [[nodiscard]] const_iterator
end(
void )
const {
538 return( const_iterator(
this , nil() ) );
541 [[nodiscard]] const_iterator
cbegin(
void )
const {
return(
begin() ); }
543 [[nodiscard]] const_iterator
cend(
void )
const {
return(
end() ); }
549 [[nodiscard]]
bool valid(
void )
const;
551 [[nodiscard]] Link * nil(
void )
const;
553 static K & key_ref( Link * u );
555 static V & value_ref( Link * u );
557 Link * new_node(
const K & x ,
const V & v );
566 Link *
lookupNode( Link * T ,
const K & x )
const;
568 Link *
min( Link * T )
const;
570 Link *
max( Link * T )
const;
575 void link( Link * p , Link * u ,
const K & x );
577 Link * insertNode( Link * T ,
const K & x ,
const V & v );
579 Link * removeNode( Link * T ,
const K & x0 );
587 Link * rotateLeft( Link * x );
590 Link * rotateRight( Link * x );
595 void balanceInsert( Link * t );
599 void balanceDelete( Link * t , Link * p );
606 Link * f_root = &s_nil;
607 std::size_t f_size = 0;
614template< std::totally_ordered K ,
class V >
618template< std::totally_ordered K ,
class V >
629#include "ADS/tree/impl/SearchTree.tpp"
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,...
the constant iterator on the pairs, by increasing key
Definition SearchTree.h:463
const_iterator & operator--(void)
to the previous key, from the end to the largest one
Definition SearchTree.h:490
std::pair< K, V > value_type
the pairs, by value
Definition SearchTree.h:467
Link * node(void) const
the node, the sentinel at the end
Definition SearchTree.h:513
bool operator==(const const_iterator &o) const
equal on the same node
Definition SearchTree.h:508
const_iterator operator--(int)
to the previous key, returning the iterator before the step
Definition SearchTree.h:501
PairRef< const K &, const V & > reference
a pair of references to a key and its value
Definition SearchTree.h:466
const_iterator & operator++(void)
to the next key, successorNode()
Definition SearchTree.h:485
std::input_iterator_tag iterator_category
input for the older algorithms, since *it is a value
Definition SearchTree.h:472
const_iterator operator++(int)
to the next key, returning the iterator before the step
Definition SearchTree.h:495
std::ptrdiff_t difference_type
the type of the distances
Definition SearchTree.h:468
const_iterator(const SearchTree *t, Link *x)
on the node of the tree
Definition SearchTree.h:478
reference operator*(void) const
the pair of the node
Definition SearchTree.h:481
std::bidirectional_iterator_tag iterator_concept
bidirectional for the concepts of C++20
Definition SearchTree.h:470
const_iterator(void)=default
an iterator on no tree
a binary search tree, balanced as a Red-Black tree if RB, and then with the operations based on join ...
Definition SearchTree.h:158
Link * root(void) const
the root, from which the nodes can be walked as in the book, along the fields parent,...
const K * findKey(const K &k) const
the address of the key in the tree, nil if absent, in time: the find( x ) of a USet of Morin,...
SearchTree(const SearchTree &o)
a copy, node by node with the same shape, in time
void forEach(F &&f) const
the foreach of the book: f( k , v ) on the pairs in increasing order of the keys, time,...
static const V & value(const Link *u)
the value of the node
bool isNil(const Link *u) const
true if is the sentinel leaf (or the parent of the root)
Link * max(void) const
the node of the largest key, nil if the tree is empty, by the right path from the root
std::size_t size_type
the type of the sizes
Definition SearchTree.h:526
bool contains(const K &k) const
true if the key is present
SearchTree & operator=(SearchTree o) noexcept
the nodes of o, by copy and swap
SearchTree(SearchTree &&o) noexcept
the nodes of o, which is left empty, in time
void insert(const K &k, const V &v)
associates the value to the key , in time
std::size_t size(void) const
the number of keys
PairRef< const K &, const V & > reference
references, by value
Definition SearchTree.h:528
std::optional< V > lookup(const K &k) const
the value of the key , or nothing
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition SearchTree.h:521
Link * lowerBoundNode(const K &x) const
the node of the smallest key not smaller than , nil if none, in time (the successor search of the fi...
const_iterator cbegin(void) const
as begin()
Definition SearchTree.h:541
Link * lookupNode(const K &x) const
the node of the key , nil if absent, in time
reference const_reference
the same
Definition SearchTree.h:529
void remove(const K &k)
removes the key , if present, in time
K Key
the type of the keys
Definition SearchTree.h:164
K key_type
the type of the keys
Definition SearchTree.h:523
std::ptrdiff_t difference_type
the type of the distances
Definition SearchTree.h:527
const_iterator iterator
the constant iterator
Definition SearchTree.h:530
V mapped_type
the type of the values
Definition SearchTree.h:525
const_iterator end(void) const
the iterator past the largest key, the sentinel
Definition SearchTree.h:537
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),...
~SearchTree()
frees the nodes
Link * successorNode(Link *t) const
the node of the next key, nil after the largest one and from nil, in time
const_iterator cend(void) const
as end()
Definition SearchTree.h:543
Link * predecessorNode(Link *t) const
the node of the previous key, nil before the first one and from nil, successorNode() with left and ri...
SearchTree(void)
an empty tree
V Value
the type of the values
Definition SearchTree.h:165
void swap(SearchTree &o) noexcept
exchanges the nodes with o
const K * lowerBoundKey(const K &k) const
the address of the smallest key not smaller than , nil if none, by lowerBoundNode() in time: the fin...
bool empty(void) const
true if there is no key
const_iterator begin(void) const
the iterator on the smallest key
Definition SearchTree.h:533
static const K & key(const Link *u)
the key of the node
void clear(void)
removes all the nodes
Link * min(void) const
the node of the smallest key, nil if the tree is empty, by the left path from the root
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
BinaryTree< T > * copy(const BinaryTree< T > *t)
a deep copy of the subtree , nil if is nil; the caller owns it
Color
the color of a node of a Red-Black tree
Definition SearchTree.h:132
SearchTree< K, V, false > BinarySearchTree
the binary search tree (ABR) of section 6.1
Definition SearchTree.h:615
SearchTree< K, V, true > RedBlackTree
the Red-Black tree of section 6.2
Definition SearchTree.h:619
@ BLACK
a black node
Definition SearchTree.h:134
@ RED
a red node
Definition SearchTree.h:133
the base of a binary search tree that is not balanced: no operation based on join,...
Definition SearchTree.h:147
the adaptor of the nodes of a Red-Black tree [see JoinAdaptor]
Definition SearchTree.h:142
the links of a node; the sentinel leaf is a Link and nothing more
Definition SearchTree.h:169
Color color
the color, read only if RB
Definition SearchTree.h:173
Link * right
the right child
Definition SearchTree.h:172
std::size_t size
the nodes of the subtree, 0 for nil
Definition SearchTree.h:174
Link * parent
the parent
Definition SearchTree.h:170
Link * left
the left child
Definition SearchTree.h:171
a node of the tree, with its key and its value
Definition SearchTree.h:179
V value
the value
Definition SearchTree.h:181
K key
the key
Definition SearchTree.h:180