|
ADS
Algorithms and Data Structures
|
The treap with finger search of exercise 7.9 of Morin, beyond the book: a treap [see Treap.h] whose nodes keep their parent, the size of their subtree and its smallest and largest node, so that a search may start from any node \( u \) rather than from the root: it climbs to the first ancestor \( w \) whose subtree spans the key, \( w.\mathit{min} \le k \le w.\mathit{max} \), and goes down from it as usual. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/dictionary/RandomHashing.h"#include <algorithm>#include <concepts>#include <cstddef>#include <cstdint>#include <optional>#include <utility>#include <vector>#include "ADS/tree/BinaryTreeAlgorithms.h"#include "ADS/tree/impl/FingerTreap.tpp"Go to the source code of this file.
Classes | |
| class | ADS::FingerTreap< K, V > |
| a DICTIONARY on a treap with the parents, the sizes and the extremes of the subtrees, searched from a finger More... | |
| class | ADS::FingerTreap< K, V >::Finger |
| a node of the treap, from which a search may start; valid until the node is removed More... | |
| struct | ADS::FingerTreap< K, V >::Shape |
| the shape of the tree, for PathIterator More... | |
Namespaces | |
| namespace | ADS |
The treap with finger search of exercise 7.9 of Morin, beyond the book: a treap [see Treap.h] whose nodes keep their parent, the size of their subtree and its smallest and largest node, so that a search may start from any node \( u \) rather than from the root: it climbs to the first ancestor \( w \) whose subtree spans the key, \( w.\mathit{min} \le k \le w.\mathit{max} \), and goes down from it as usual.
The climb and the descent take \( O( 1 + \log r ) \) expected time, \( r \) the keys between \( k \) and the key of \( u \): the lowest common ancestor of the two nodes in a treap is the node of largest priority among the \( r + 2 \) keys from one to the other, and the expected number of the ancestors of either node below it is \( O( \log r ) \), those outside that range included (as in the proof of Lemma 7.1 of Morin), which is the argument of Seidel and Aragon for their finger searches. fingerLookup() keeps the node of the last search as the finger (part 3 of the exercise), so that a sequence of searches of keys near each other costs \( O( 1 + \log r ) \) each instead of \( O( \log n ) \); lookupFrom() starts from a node given by finger() (part 2). select() and rank() read the sizes (exercise 7.10).
It is a DICTIONARY [see ADT.h] with the find(x) of an SSet (lowerBound()); an insertion puts the new node in a leaf and rotates it up while its priority beats its parent's, a removal rotates the node down to a leaf, and both then walk up to the root to correct the sizes and the extremes, \( O( \log n ) \) expected time, as a rotation corrects those of its two nodes in \( O( 1 ) \). A removal brings the finger back to the root. The priorities come from SplitMix64 [see RandomHashing.h].
Where it is meant to compare with the Treap of Morin, for the benchmarks to check: a node is 3 words larger (the size and the two extremes); the plain operations should cost a little more, and the searches of keys near the previous one much less, \( O( \log r ) \) against \( O( \log n ) \).
The contracts [see Contract.h]: a finger is a node of the treap, and 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, the heap order of the priorities, the parents, the sizes and the extremes, an \( O( n ) \) check at the level 3. The operations are defined in FingerTreap.tpp.