ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::MeldableHeap< T, Less, A > Class Template Reference

a randomized meldable heap, the smallest item by less on top More...

#include <ADS/priority/MeldableHeap.h>

Public types

using Item = T
 the type of the items
using Handle = Node *
 what push() gives, for erase()

Public member functions

 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

Static public member functions

static const T & get (Handle h)
 the item of the handle \( h \)

Detailed description

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

Member typedef documentation

◆ Handle

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
using ADS::MeldableHeap< T, Less, A >::Handle = Node *

what push() gives, for erase()

◆ Item

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
using ADS::MeldableHeap< T, Less, A >::Item = T

the type of the items

Constructor and destructor documentation

◆ MeldableHeap() [1/3]

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
ADS::MeldableHeap< T, Less, A >::MeldableHeap ( Less less = Less(),
std::uint64_t seed = 1,
const A & alloc = A() )
explicit

the empty heap of the order less, its coins drawn from the seed

◆ MeldableHeap() [2/3]

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
ADS::MeldableHeap< T, Less, A >::MeldableHeap ( MeldableHeap< T, Less, A > && o)
noexcept

the heap of o, which is left empty

◆ MeldableHeap() [3/3]

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
ADS::MeldableHeap< T, Less, A >::MeldableHeap ( const MeldableHeap< T, Less, A > & )
delete

◆ ~MeldableHeap()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
ADS::MeldableHeap< T, Less, A >::~MeldableHeap ( )

frees the nodes

Member function documentation

◆ clear()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
void ADS::MeldableHeap< T, Less, A >::clear ( void )

removes every item

◆ empty()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
bool ADS::MeldableHeap< T, Less, A >::empty ( void ) const
inlinenodiscard

true if there are no items

◆ erase()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
T ADS::MeldableHeap< T, Less, A >::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

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

◆ get()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
const T & ADS::MeldableHeap< T, Less, A >::get ( Handle h)
staticnodiscard

the item of the handle \( h \)

◆ meld()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
void ADS::MeldableHeap< T, Less, A >::meld ( MeldableHeap< T, Less, A > & 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

Precondition
the allocators of the two heaps are equal

◆ operator=() [1/2]

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
MeldableHeap & ADS::MeldableHeap< T, Less, A >::operator= ( const MeldableHeap< T, Less, A > & )
delete

◆ operator=() [2/2]

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
MeldableHeap & ADS::MeldableHeap< T, Less, A >::operator= ( MeldableHeap< T, Less, A > && o)
noexcept

the heap of o, which gets the items of this one

◆ pop()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
T ADS::MeldableHeap< T, Less, A >::pop ( void )

removes the smallest item and returns it, merging the two subtrees of the root, in \( O( \log n ) \) expected time

Precondition
the heap is not empty

◆ push()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
Handle ADS::MeldableHeap< T, Less, A >::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

◆ secondTop()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
const T * ADS::MeldableHeap< T, Less, A >::secondTop ( void ) const
nodiscard

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)

◆ size()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
std::size_t ADS::MeldableHeap< T, Less, A >::size ( void ) const
inlinenodiscard

the number \( n \) of items

◆ smallest()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
std::vector< T > ADS::MeldableHeap< T, Less, A >::smallest ( std::size_t k) const
nodiscard

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.

◆ top()

template<class T, class Less = std::less< T >, class A = std::allocator< T >>
const T & ADS::MeldableHeap< T, Less, A >::top ( void ) const
nodiscard

the smallest item

Precondition
the heap is not empty

The documentation for this class was generated from the following file: