|
ADS
Algorithms and Data Structures
|
The TreapList of exercise 7.11 of Morin, beyond the book: a sequence with access, insertion and removal at any index in \( O( \log n ) \) expected time, on a treap [see Treap.h] whose symmetric order is the order of the sequence and whose nodes keep the size of their subtree, so that the index of a node is the size of the left subtrees passed on the right along its path (an implicit treap: the keys are the indices, never stored). More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/dictionary/RandomHashing.h"#include <cstddef>#include <cstdint>#include <memory>#include <utility>#include "ADS/sequence/impl/TreapList.tpp"Go to the source code of this file.
Classes | |
| class | ADS::TreapList< T > |
| a sequence of items of type T on an implicit treap, the TreapList of Morin More... | |
| struct | ADS::TreapList< T >::Shape |
| the shape of the tree, for PathIterator More... | |
Namespaces | |
| namespace | ADS |
The TreapList of exercise 7.11 of Morin, beyond the book: a sequence with access, insertion and removal at any index in \( O( \log n ) \) expected time, on a treap [see Treap.h] whose symmetric order is the order of the sequence and whose nodes keep the size of their subtree, so that the index of a node is the size of the left subtrees passed on the right along its path (an implicit treap: the keys are the indices, never stored).
An insertion at \( i \) goes down by the sizes to the leaf between the items \( i - 1 \) and \( i \) and rotates the new node up while its priority beats its parent's; a removal rotates the node down until it has one child at most. The shape is that of a random binary search tree on the indices, whatever the order of the operations, as for the keys of a treap.
truncate( i ) splits the treap at the index \( i \) and absorb( s ) joins the treap of another sequence at the end, both along two spines in \( O( \log n ) \) expected time, as SkipListSequence does on a skip list. The priorities come from SplitMix64 [see RandomHashing.h], seeded by default with a seed of its own per sequence, so that the sequences absorbed one into another do not repeat their priorities; a seed given makes them reproducible.
Morin gives no code for it: the benchmarks compare it with SkipListSequence, whose operations it has, and with his SkiplistList; a node here holds the item, a priority, a size and two links, one block of memory, and a search reads one node per level.
The contracts [see Contract.h]: an index is below size() (at most size() for an insertion and a truncation); the invariant, an \( O( n ) \) check at the level 3, is the heap order of the priorities and the sizes. The operations are defined in TreapList.tpp.