ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Treap.h File Reference

The treap of Seidel and Aragon, beyond the book: a binary search tree on the keys that is also a heap on random priorities given to the keys when they are inserted, so that its shape is that of a binary search tree built by inserting the keys in a random order, whatever the order of the operations; the expected depth of a node is \( O( \log n ) \), and an insertion or a removal takes \( O( \log n ) \) expected time and makes fewer than 2 rotations on average. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/dictionary/RandomHashing.h"
#include "ADS/tree/BinaryTreeAlgorithms.h"
#include "ADS/tree/JoinAlgorithms.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "ADS/tree/impl/Treap.tpp"

Go to the source code of this file.

Classes

class  ADS::Treap< K, V >
 a DICTIONARY on a treap, with the operations based on join [see Joinable] More...
struct  ADS::Treap< K, V >::Shape
 the shape of the tree, for PathIterator More...

Namespaces

namespace  ADS

Detailed description

The treap of Seidel and Aragon, beyond the book: a binary search tree on the keys that is also a heap on random priorities given to the keys when they are inserted, so that its shape is that of a binary search tree built by inserting the keys in a random order, whatever the order of the operations; the expected depth of a node is \( O( \log n ) \), and an insertion or a removal takes \( O( \log n ) \) expected time and makes fewer than 2 rotations on average.

An insertion puts the key in a leaf and rotates it up while its priority beats its parent's, a removal rotates the key down until it is a leaf. It is a DICTIONARY [see ADT.h]; the priorities come from SplitMix64 [see RandomHashing.h], seeded, so that a run is reproducible.

Each node keeps also the size of its subtree, so that the treap has the operations based on join [see JoinAlgorithms.h]: split(), join(), join2(), extractRange(), the union, the intersection and the difference with another treap (those of Blelloch and Reid-Miller), the batches of insertions and of removals, filter() and mapReduce(), sequential or on the threads of an executor; its join merges the two spines by the priorities, in \( O( \log n ) \) expected time.

It is also the Treap of Morin (section 7.2) and has the operations of his exercises: lowerBound() is the find(x) of an SSet, select() and rank() the access by rank of exercise 7.10, by the sizes, fromSorted() the construction from sorted keys in \( O( n ) \) of exercise 7.8, and split() and join2() the split(x) and the absorb(t) of exercises 7.12 and 7.13. Where it is meant to be faster than his code, for the benchmarks to check: the priorities come from SplitMix64 instead of rand(), an insertion goes down and rotates on the way back up with no link to the parent (his nodes have three links, and his insertion walks the parents up), and the sizes cost one addition per level of a rotation.

The contracts [see Contract.h]: insert() ensures that the key is there and remove() that it is not; the invariant is the order of the keys, the sizes and the heap order of the priorities, an \( O( n ) \) check at the level 3. The operations are defined in Treap.tpp.

See also
R. Seidel, C. R. Aragon, Randomized search trees, Algorithmica 16(4-5), 464-497, 1996, doi:10.1007/BF01940876
G. L. Steele, D. Lea, C. H. Flood, Fast splittable pseudorandom number generators, Proceedings of OOPSLA 2014, 453-472, 2014, doi:10.1145/2660193.2660195 (SplitMix64)
G. E. Blelloch, M. Reid-Miller, Fast set operations using treaps, Proceedings of the 10th ACM Symposium on Parallel Algorithms and Architectures (SPAA), 16-26, 1998, doi:10.1145/277651.277660
J. Vuillemin, A unifying look at data structures, Communications of the ACM 23(4), 229-239, 1980, doi:10.1145/358841.358852
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 7.2 and exercises 7.8, 7.10, 7.12 and 7.13
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa