|
ADS
Algorithms and Data Structures
|
The randomized binary search tree of Martínez and Roura (exercise 7.14 of Morin), beyond the book: a binary search tree that stores no priorities but the size of each subtree, and stays a random binary search tree, i.e., of the shape that the insertions of its keys in a uniformly random order give, whatever the order of the operations; the expected depth of a node is \( O( \log n ) \), as in a treap [see Treap.h], and the sizes give the access by rank for free (select(), rank()). More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/dictionary/RandomHashing.h"#include "ADS/numeric/RandomGenerators.h"#include <algorithm>#include <concepts>#include <cstddef>#include <cstdint>#include <memory>#include <optional>#include <utility>#include "ADS/tree/BinaryTreeAlgorithms.h"#include "ADS/tree/impl/RandomizedSearchTree.tpp"Go to the source code of this file.
Classes | |
| class | ADS::RandomizedSearchTree< K, V > |
| a DICTIONARY on a randomized binary search tree of Martínez and Roura More... | |
| struct | ADS::RandomizedSearchTree< K, V >::Shape |
| the shape of the tree, for PathIterator More... | |
Namespaces | |
| namespace | ADS |
The randomized binary search tree of Martínez and Roura (exercise 7.14 of Morin), beyond the book: a binary search tree that stores no priorities but the size of each subtree, and stays a random binary search tree, i.e., of the shape that the insertions of its keys in a uniformly random order give, whatever the order of the operations; the expected depth of a node is \( O( \log n ) \), as in a treap [see Treap.h], and the sizes give the access by rank for free (select(), rank()).
An insertion into the subtree \( t \) of \( m \) nodes puts the new key at its root with probability \( 1 / ( m + 1 ) \), splitting \( t \) by the key into its two subtrees, and goes down into the left or the right subtree otherwise: the new key is the root of a random tree with exactly that probability, the event that its priority, in a treap, beats the \( m \) others. A removal replaces the node by the join of its two subtrees, of sizes \( a \) and \( b \), whose root is the root of the left one with probability \( a / ( a + b ) \) and of the right one otherwise, recursively along the two spines. Morin describes the same choices made by rotations (section 7.3): a new leaf rotated up to the root of the subtree where the coin falls, a node rotated down by the left or by the right with the probabilities of the sizes; the split and the join are the original form of Martínez and Roura, with no rotation.
The random choices are words of SplitMix64 [see RandomHashing.h] turned into an integer below \( m + 1 \) by uniformBelow() [see RandomGenerators.h], with no bias. It is a DICTIONARY [see ADT.h] with the find(x) of an SSet (lowerBound()).
Where it is meant to compare with the Treap of Morin, for the benchmarks to check (exercise 7.14 asks for this comparison): a node has a size instead of a priority, the same space, and the access by rank comes with it; an insertion draws a random number per level until the coin falls, \( O( 1 ) \) expected at each level but \( O( \log n ) \) in all, against one per insertion in a treap, which is where it should lose.
The contracts [see Contract.h]: select() needs a rank below size(); insert() ensures that the key is there and remove() that it is not; the invariant is the order of the keys and the sizes, an \( O( n ) \) check at the level 3. The operations are defined in RandomizedSearchTree.tpp.