|
| | MeldableHeap (Less less=Less(), std::uint64_t seed=1, const A &alloc=A()) |
| | the empty heap of the order less, its coins drawn from the seed
|
| | MeldableHeap (MeldableHeap &&o) noexcept |
| | the heap of o, which is left empty
|
| MeldableHeap & | operator= (MeldableHeap &&o) noexcept |
| | the heap of o, which gets the items of this one
|
| | MeldableHeap (const MeldableHeap &)=delete |
| MeldableHeap & | operator= (const MeldableHeap &)=delete |
| | ~MeldableHeap () |
| | frees the nodes
|
| Handle | push (T x) |
| | adds \( x \), merging the heap with a heap of one node, in \( O( \log n ) \) expected time; the handle of its node
|
| const T & | top (void) const |
| | the smallest item
|
| const T * | secondTop (void) const |
| | the second smallest item, the smaller child of the root, or nil if there are fewer than two items (exercise 10.9 of Morin's book)
|
| T | pop (void) |
| | removes the smallest item and returns it, merging the two subtrees of the root, in \( O( \log n ) \) expected time
|
| T | erase (Handle h) |
| | removes the item of the handle \( h \) and returns it, merging the two subtrees of its node in its place, in \( O( \log n ) \) expected time
|
| void | meld (MeldableHeap &o) |
| | moves the items of o into this heap, in \( O( \log n ) \) expected time; o is left empty, and its handles become handles of this heap
|
| std::vector< T > | smallest (std::size_t k) const |
| | the \( k \) smallest items in increasing order, or all of them if fewer, in \( O( k \log k ) \) time (exercise 10.10 of Morin's book)
|
| std::size_t | size (void) const |
| | the number \( n \) of items
|
| bool | empty (void) const |
| | true if there are no items
|
| void | clear (void) |
| | removes every item
|
template<class T, class Less = std::less< T >, class A = std::allocator< T >>
class ADS::MeldableHeap< T, Less, A >
a randomized meldable heap, the smallest item by less on top
template<class T, class Less = std::less< T >, class A = std::allocator< T >>
removes the item of the handle \( h \) and returns it, merging the two subtrees of its node in its place, in \( O( \log n ) \) expected time
In pseudocode (exercise 10.8 of Morin's book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{erase}( u ) \cr
2 & \quad m \gets \mathrm{merge}( u.\mathit{left} , u.\mathit{right} )
\cr
3 & \quad \textbf{if}\ m \ne \textbf{nil}\ \textbf{then}
\ m.\mathit{parent} \gets u.\mathit{parent} \cr
4 & \quad \textbf{if}\ u = \mathit{root}\ \textbf{then}\ \mathit{root}
\gets m \cr
5 & \quad \textbf{else}\ \text{the link of } u.\mathit{parent}
\text{ to } u \gets m \cr
6 & \quad \textbf{return}\ u.x
\end{array}}
\]
since the order holds between \( u.\mathit{parent} \) and every node below \( u \); the walk of the merge starts at \( u \), with fewer than \( n \) nodes below it.
- Precondition
- \( h \) is the handle of an item of this heap
template<class T, class Less = std::less< T >, class A = std::allocator< T >>
the \( k \) smallest items in increasing order, or all of them if fewer, in \( O( k \log k ) \) time (exercise 10.10 of Morin's book)
In pseudocode (exercise 10.10 of Morin's book), \( Q \) a heap of nodes by their items, each node entering it after its parent:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{smallest}( k ) \cr
2 & \quad Q \gets \{ \mathit{root} \};\ S \gets () \cr
3 & \quad \textbf{while}\ |S| < k\ \textbf{and}\ Q \ne \emptyset
\ \textbf{do} \cr
4 & \qquad u \gets \mathrm{deleteMin}( Q );\ S \gets S \cdot ( u.x ) \cr
5 & \qquad \text{put the children of } u \text{ in } Q \cr
6 & \quad \textbf{return}\ S
\end{array}}
\]
where \( Q \) holds at most \( k + 1 \) nodes, since each step takes one and puts two.