|
ADS
Algorithms and Data Structures
|
The binary search tree (ABR) and the Red-Black tree of the book, as a DICTIONARY [see ADT.h] from keys to values. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/tree/JoinAlgorithms.h"#include <concepts>#include <cstddef>#include <iterator>#include <memory>#include <optional>#include <type_traits>#include <utility>#include <vector>#include "ADS/tree/impl/SearchTree.tpp"Go to the source code of this file.
Classes | |
| struct | ADS::NotJoinable |
| the base of a binary search tree that is not balanced: no operation based on join, whose recursion would be as deep as the tree More... | |
| class | ADS::SearchTree< K, V, RB > |
| a binary search tree, balanced as a Red-Black tree if RB, and then with the operations based on join [see Joinable] More... | |
| struct | ADS::SearchTree< K, V, RB >::Link |
| the links of a node; the sentinel leaf is a Link and nothing more More... | |
| struct | ADS::SearchTree< K, V, RB >::Node |
| a node of the tree, with its key and its value More... | |
| class | ADS::SearchTree< K, V, RB >::const_iterator |
| the constant iterator on the pairs, by increasing key More... | |
Namespaces | |
| namespace | ADS |
Typedefs | |
| template<std::totally_ordered K, class V> | |
| using | ADS::BinarySearchTree = SearchTree< K , V , false > |
| the binary search tree (ABR) of section 6.1 | |
| template<std::totally_ordered K, class V> | |
| using | ADS::RedBlackTree = SearchTree< K , V , true > |
| the Red-Black tree of section 6.2 | |
Enumerations | |
| enum class | ADS::Color : unsigned char { ADS::Color::RED , ADS::Color::BLACK } |
| the color of a node of a Red-Black tree More... | |
The binary search tree (ABR) and the Red-Black tree of the book, as a DICTIONARY [see ADT.h] from keys to values.
In a binary search tree the keys in the left subtree of a node \( u \) are smaller than the key of \( u \), and those in the right subtree larger, so that the visit in symmetric order examines the keys in increasing order; lookupNode(), insertNode() and removeNode() go down one path and take \( O( h ) \) time, \( h \) being the height of the tree, which is \( O( n ) \) in the worst case.
A Red-Black tree is a binary search tree whose nodes are red or black, with the constraints (V1) the root is black, (V2) the leaves are black, (V3) both children of a red node are black, (V4) all the paths from a node to the leaves below it have the same number of black nodes, its black height \( b( u ) \). The subtree of \( u \) has at least \( 2^{b( u )} - 1 \) internal nodes (Theorem 6.1), no path from the root to a leaf is longer than twice another one (Theorem 6.2), and the height is
\[ h \leq 2 \log_2( n + 1 ) \]
(Theorem 6.3). The procedures are those of the binary search tree, plus balanceInsert() after an insertion and balanceDelete() after the removal of a black node, which restore the constraints with \( O( \log n ) \) changes of color and at most 3 rotations, in \( O( \log n ) \) time.
The two trees are one class, SearchTree< K , V , RB >, the procedures of the book written once and the two balancing calls under if constexpr( RB ) where the book "highlights them in black"; BinarySearchTree< K , V > and RedBlackTree< K , V > are its two instances. The leaves are one sentinel node, black, which the book allows, shared by all the trees of the type and never written, so that the trees that a split makes, or that two threads join, may point to it at once. Four points of the pseudocode of the book are mended, each marked in the code: balanceDelete() is given the parent of \( t \), since \( t \) may be the sentinel, whose parent the book writes when the removed node had no children; it stops when \( t \) is the root rather than when \( t \) is the old root \( T \), which a rotation of the case (1) may have moved down; the case (4) climbs to the root instead of reading the old \( T \); and the Red-Black insertNode() returns the root also when the key was there already.
Each node keeps also the size of its subtree, which the insertions, the removals and the rotations update, so that the Red-Black tree has the operations based on join [see JoinAlgorithms.h]: split(), join(), join2(), extractRange(), the union, the intersection and the difference with another tree, the batches of insertions and of removals, filter() and mapReduce(), sequential or on the threads of an executor; its join is that of the Red-Black trees of Tarjan, by the black heights. The binary search tree, whose height may be \( n \), has none of them.
lowerBoundNode() is the successor search, the find( x ) of a sorted set of Morin (section 6.2.1), the smallest key not smaller than \( x \); lowerBound() gives it with its value, and findKey() and lowerBoundKey() give the address of the key found, so that DictionarySet over either tree is an SSet [see SetInterfaces.h].
The contracts [see Contract.h]: after insert() the key is there and after remove() it is not; key(), value(), successorNode() and predecessorNode() need a node; the invariant is the order of the keys, the parents, the sizes and, for a Red-Black tree, the constraints (V1)-(V4) [see valid()]. The operations are defined in SearchTree.tpp.