ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SearchTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File SearchTree.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
95/*--------------------------------------------------------------------------*/
96/*----------------------------- DEFINITIONS --------------------------------*/
97/*--------------------------------------------------------------------------*/
98
99#ifndef __ADS_SearchTree
100 #define __ADS_SearchTree
101/* self-identification: #endif constant */
102
103/*--------------------------------------------------------------------------*/
104/*------------------------------ INCLUDES ----------------------------------*/
105/*--------------------------------------------------------------------------*/
106
107#include "ADS/Contract.h"
108#include "ADS/Iterators.h"
110
111#include <concepts>
112#include <cstddef>
113#include <iterator>
114#include <memory>
115#include <optional>
116#include <type_traits>
117#include <utility>
118#include <vector>
119
120/*--------------------------------------------------------------------------*/
121/*--------------------------- NAMESPACE ------------------------------------*/
122/*--------------------------------------------------------------------------*/
123
124namespace ADS
125{
126
127/*--------------------------------------------------------------------------*/
128/*-------------------------- CLASS SearchTree ------------------------------*/
129/*--------------------------------------------------------------------------*/
130
132enum class Color : unsigned char {
135 };
136
137template< std::totally_ordered K , class V , bool RB >
138class SearchTree;
139
141template< std::totally_ordered K , class V , bool RB >
143
147{ };
148
151template< std::totally_ordered K , class V , bool RB >
153 : public std::conditional_t<
154 RB ,
155 Joinable< SearchTree< K , V , RB > , SearchTreeNodes< K , V , RB > ,
156 K , V > ,
157 NotJoinable >
158{
159
160 friend struct SearchTreeNodes< K , V , RB >;
161
162/*--------------------------------------------------------------------------*/
163 public:
164 using Key = K;
165 using Value = V;
166
168 struct Link
169 {
174 std::size_t size = 0;
175 };
176
178 struct Node : Link
179 {
180 K key;
182 };
183
185 SearchTree( void );
186
188 SearchTree( const SearchTree & o );
189
191 SearchTree( SearchTree && o ) noexcept;
192
195
198
200 void swap( SearchTree & o ) noexcept;
201
202/*--------------------------------------------------------------------------*/
203/*------------------------- DICTIONARY OPERATIONS --------------------------*/
204/*--------------------------------------------------------------------------*/
205
207 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
208
270 void insert( const K & k , const V & v );
271
326 void remove( const K & k );
327
329 [[nodiscard]] bool contains( const K & k ) const;
330
334 [[nodiscard]] std::optional< std::pair< K , V > >
335 lowerBound( const K & k ) const;
336
341 [[nodiscard]] const K * findKey( const K & k ) const;
342
347 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
348
350 [[nodiscard]] std::size_t size( void ) const;
351
353 [[nodiscard]] bool empty( void ) const;
354
356 void clear( void );
357
358/*--------------------------------------------------------------------------*/
359/*---------------------------- NAVIGATION ----------------------------------*/
360/*--------------------------------------------------------------------------*/
364 [[nodiscard]] Link * root( void ) const;
365
367 [[nodiscard]] bool isNil( const Link * u ) const;
368
373 [[nodiscard]] static const K & key( const Link * u );
374
377 [[nodiscard]] static const V & value( const Link * u );
378
392 [[nodiscard]] Link * lookupNode( const K & x ) const;
393
414 [[nodiscard]] Link * lowerBoundNode( const K & x ) const;
415
418 [[nodiscard]] Link * min( void ) const;
419
422 [[nodiscard]] Link * max( void ) const;
423
443 [[nodiscard]] Link * successorNode( Link * t ) const;
444
447 template< class F >
448 void forEach( F && f ) const;
449
453 [[nodiscard]] Link * predecessorNode( Link * t ) const;
454
455/*------------------------- THE STANDARD INTERFACE -------------------------*/
456
457 // a constant view of the pairs ( key , value ) by increasing key,
458 // bidirectional by the links to the parents, the sentinel leaf being the
459 // end
460
463 {
464 public:
467 using value_type = std::pair< K , V >;
468 using difference_type = std::ptrdiff_t;
470 using iterator_concept = std::bidirectional_iterator_tag;
472 using iterator_category = std::input_iterator_tag;
473
475 const_iterator( void ) = default;
476
478 const_iterator( const SearchTree * t , Link * x ) : f_t( t ) , f_x( x ) { }
479
481 reference operator*( void ) const {
482 return( reference( f_t->key( f_x ) , f_t->value( f_x ) ) );
483 }
484
486 f_x = f_t->successorNode( f_x );
487 return( *this );
488 }
489
491 f_x = f_t->isNil( f_x ) ? f_t->max() : f_t->predecessorNode( f_x );
492 return( *this );
493 }
494
496 auto t = *this;
497 ++*this;
498 return( t );
499 }
500
502 auto t = *this;
503 --*this;
504 return( t );
505 }
506
508 bool operator==( const const_iterator & o ) const {
509 return( f_x == o.f_x );
510 }
511
513 [[nodiscard]] Link * node( void ) const { return( f_x ); }
514
515 private:
516 const SearchTree * f_t = nullptr;
517 Link * f_x = nullptr;
518 };
519
521 using value_type = std::pair< K , V >;
523 using key_type = K;
525 using mapped_type = V;
526 using size_type = std::size_t;
527 using difference_type = std::ptrdiff_t;
531
533 [[nodiscard]] const_iterator begin( void ) const {
534 return( const_iterator( this , min() ) );
535 }
536
537 [[nodiscard]] const_iterator end( void ) const {
538 return( const_iterator( this , nil() ) );
539 }
540
541 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
543 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
544
545/*--------------------------------------------------------------------------*/
546 private:
549 [[nodiscard]] bool valid( void ) const;
550
551 [[nodiscard]] Link * nil( void ) const;
552
553 static K & key_ref( Link * u );
554
555 static V & value_ref( Link * u );
556
557 Link * new_node( const K & x , const V & v );
558
560 Link * copy( const SearchTree & o , Link * u , Link * p );
561
562/*--------------------------------------------------------------------------*/
563/*------------------- THE PROCEDURES OF SECTION 6.1 ------------------------*/
564/*--------------------------------------------------------------------------*/
565
566 Link * lookupNode( Link * T , const K & x ) const;
567
568 Link * min( Link * T ) const;
569
570 Link * max( Link * T ) const;
571
575 void link( Link * p , Link * u , const K & x );
576
577 Link * insertNode( Link * T , const K & x , const V & v );
578
579 Link * removeNode( Link * T , const K & x0 );
580
581/*--------------------------------------------------------------------------*/
582/*------------------- THE PROCEDURES OF SECTION 6.2 ------------------------*/
583/*--------------------------------------------------------------------------*/
584
587 Link * rotateLeft( Link * x );
588
590 Link * rotateRight( Link * x );
591
592 static constexpr Color RED = Color::RED;
593 static constexpr Color BLACK = Color::BLACK;
594
595 void balanceInsert( Link * t );
596
599 void balanceDelete( Link * t , Link * p );
600
601/*--------------------------------------------------------------------------*/
602
604 static Link s_nil;
605
606 Link * f_root = &s_nil;
607 std::size_t f_size = 0;
608
609 }; // end( class SearchTree )
610
611/*--------------------------------------------------------------------------*/
612
614template< std::totally_ordered K , class V >
616
618template< std::totally_ordered K , class V >
620
621/*--------------------------------------------------------------------------*/
622
623} // end( namespace ADS )
624
625/*--------------------------------------------------------------------------*/
626/*-------------------------- IMPLEMENTATION --------------------------------*/
627/*--------------------------------------------------------------------------*/
628
629#include "ADS/tree/impl/SearchTree.tpp"
630
631/*--------------------------------------------------------------------------*/
632
633#endif /* SearchTree.h included */
634
635/*--------------------------------------------------------------------------*/
636/*------------------------- End File SearchTree.h --------------------------*/
637/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96
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
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