|
ADS
Algorithms and Data Structures
|
The randomized meldable heap of Gambin and Malinowski, beyond the book, as section 10.2 of Morin's book presents it: a binary tree, of any shape, in which each item is not smaller than its parent, and whose only operation is the merge of two such trees: the smaller root stays the root, and the other tree is merged, recursively, with one of its two subtrees chosen by a fair coin. More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/dictionary/RandomHashing.h"#include <algorithm>#include <cstddef>#include <cstdint>#include <functional>#include <memory>#include <utility>#include <vector>#include "ADS/priority/impl/MeldableHeap.tpp"Go to the source code of this file.
Classes | |
| class | ADS::MeldableHeap< T, Less, A > |
| a randomized meldable heap, the smallest item by less on top More... | |
Namespaces | |
| namespace | ADS |
The randomized meldable heap of Gambin and Malinowski, beyond the book, as section 10.2 of Morin's book presents it: a binary tree, of any shape, in which each item is not smaller than its parent, and whose only operation is the merge of two such trees: the smaller root stays the root, and the other tree is merged, recursively, with one of its two subtrees chosen by a fair coin.
The path followed is a random walk from the root, whose expected length in a tree of \( n \) nodes is at most \( \log_2( n + 1 ) \) whatever its shape (Lemma 10.1 of Morin's book), so that a merge of two heaps of \( n_1 \) and \( n_2 \) items takes \( O( \log n_1 + \log n_2 ) \) expected time; push() merges a heap of one item, pop() merges the two subtrees of the root, erase( h ) (exercise 10.8 of Morin's book) merges the two subtrees of the node of the handle \( h \) and hangs the result where the node was, and meld() merges a whole heap, all in \( O( \log n ) \) expected time.
Beyond top(), the second smallest item is the smaller of the two children of the root, \( O( 1 ) \) (exercise 10.9), and the \( k \) smallest items in order come from a second heap of the nodes whose parent has been taken, which starts from the root and holds at most \( k + 1 \) of them, \( O( k \log k ) \) time (exercise 10.10).
Against the code of Morin's book: the merge is a loop, with no recursion, that hangs each node by a pointer to the link that will hold it; the coins are the bits of one word of SplitMix64 [see RandomHashing.h] used one at a time, 64 coins per word, where the book calls rand() for each (where it is meant to be faster); the nodes come from the allocator A, e.g., a PoolAllocator [see PoolAllocator.h], and two heaps meld only if their allocators are equal, so that each node goes back to the allocator that gave it; and the items are moved, not copied, into the nodes and out of them.
The contracts [see Contract.h]: top(), pop() need a heap that is not empty, erase() a handle of an item of the heap, meld() an equal allocator; the invariant (the order of the heap, the parents and the count of the items) is checked in \( O( n ) \) time at the level 3 while \( n \le 256 \). The operations are defined in MeldableHeap.tpp.