ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::MinMaxHeap< T, Better > Class Template Reference

a min-max heap of plain items: the best and the worst by better() More...

#include <ADS/priority/MinMaxHeap.h>

Public types

using Item = T
 the type of the items

Public member functions

 MinMaxHeap (Better better=Better())
 the empty queue of the order better
void push (const T &x)
 pushes \( x \), in \( O( \log n ) \) time
const T & min (void) const
 the best item, at the root
const T & max (void) const
 the worst item, the worse of the two children of the root
T popMin (void)
 removes the best item and returns it, in \( O( \log n ) \) amortized time
T popMax (void)
 removes the worst item and returns it, in \( O( \log n ) \) amortized time
bool empty (void) const
 whether the queue has no item
std::size_t size (void) const
 the number of items, \( n \)
void reserve (std::size_t n)
 room for \( n \) items, so that the pushes up to \( n \) do not reallocate
void clear (void)
 removes all the items

Detailed description

template<class T, class Better = std::less< T >>
class ADS::MinMaxHeap< T, Better >

a min-max heap of plain items: the best and the worst by better()

Member typedef documentation

◆ Item

template<class T, class Better = std::less< T >>
using ADS::MinMaxHeap< T, Better >::Item = T

the type of the items

Constructor and destructor documentation

◆ MinMaxHeap()

template<class T, class Better = std::less< T >>
ADS::MinMaxHeap< T, Better >::MinMaxHeap ( Better better = Better())
inlineexplicit

the empty queue of the order better

Member function documentation

◆ clear()

template<class T, class Better = std::less< T >>
void ADS::MinMaxHeap< T, Better >::clear ( void )
inline

removes all the items

◆ empty()

template<class T, class Better = std::less< T >>
bool ADS::MinMaxHeap< T, Better >::empty ( void ) const
inlinenodiscard

whether the queue has no item

◆ max()

template<class T, class Better = std::less< T >>
const T & ADS::MinMaxHeap< T, Better >::max ( void ) const
nodiscard

the worst item, the worse of the two children of the root

Precondition
the queue is not empty

◆ min()

template<class T, class Better = std::less< T >>
const T & ADS::MinMaxHeap< T, Better >::min ( void ) const
nodiscard

the best item, at the root

Precondition
the queue is not empty

◆ popMax()

template<class T, class Better = std::less< T >>
T ADS::MinMaxHeap< T, Better >::popMax ( void )

removes the worst item and returns it, in \( O( \log n ) \) amortized time

In pseudocode (Atkinson, Sack, Santoro and Strothotte, beyond the book), with \( \mathrm{down}_{\max} \) the walk of popMin() with the order reversed:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{popMax}() \cr 2 & \quad \textbf{if}\ \text{a run of removals reached} \max\{ 64 , n / 8 \} \text{ items }\ \textbf{then} \ \text{sort the items} \cr 3 & \quad \textbf{if}\ \text{the items are sorted }\ \textbf{then} \ \textbf{return}\ \text{the last of them, removed} \cr 4 & \quad k \gets \text{the place of the worst item, } 0 , 1 \text{ or } 2 \cr 5 & \quad x \gets A[ k ];\ n \gets n - 1 \cr 6 & \quad \textbf{if}\ k < n\ \textbf{then}\ A[ k ] \gets A[ n ]; \ \mathrm{down}_{\max}( k ) \cr 7 & \quad \textbf{return}\ x \end{array}} \]

Precondition
the queue is not empty

◆ popMin()

template<class T, class Better = std::less< T >>
T ADS::MinMaxHeap< T, Better >::popMin ( void )

removes the best item and returns it, in \( O( \log n ) \) amortized time

In pseudocode (Atkinson, Sack, Santoro and Strothotte, beyond the book), \( \mathrm{down}_{\min} \) the walk down for the best; \( \mathrm{down}_{\max} \) is the same with the order reversed:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{popMin}() \cr 2 & \quad \textbf{if}\ \text{a run of removals reached} \max\{ 64 , n / 8 \} \text{ items }\ \textbf{then} \ \text{sort the items} \cr 3 & \quad \textbf{if}\ \text{the items are sorted }\ \textbf{then} \ \textbf{return}\ \text{the first of them, removed} \cr 4 & \quad x \gets A[ 0 ];\ n \gets n - 1;\ A[ 0 ] \gets A[ n ]; \ \mathrm{down}_{\min}( 0 );\ \textbf{return}\ x \cr 5 & \textbf{procedure}\ \mathrm{down}_{\min}( i ) \cr 6 & \quad \textbf{while}\ 2 i + 1 < n\ \textbf{do} \cr 7 & \qquad m \gets \text{the best of the children and the} \text{ grandchildren of } i \cr 8 & \qquad \textbf{if not}\ \mathrm{better}( A[ m ] , A[ i ] ) \ \textbf{then return} \cr 9 & \qquad \text{swap } A[ m ] \text{ and } A[ i ] \cr 10 & \qquad \textbf{if}\ m \text{ is a child of } i\ \textbf{then} \ \textbf{return} \cr 11 & \qquad p \gets \lfloor ( m - 1 ) / 2 \rfloor \cr 12 & \qquad \textbf{if}\ \mathrm{better}( A[ p ] , A[ m ] ) \ \textbf{then}\ \text{swap } A[ p ] \text{ and } A[ m ] \cr 13 & \qquad i \gets m \end{array}} \]

Precondition
the queue is not empty

◆ push()

template<class T, class Better = std::less< T >>
void ADS::MinMaxHeap< T, Better >::push ( const T & x)

pushes \( x \), in \( O( \log n ) \) time

In pseudocode (Atkinson, Sack, Santoro and Strothotte, beyond the book), \( \mathrm{up}_{\min} \) and \( \mathrm{up}_{\max} \) moving an item up by its grandparents while better (worse) than them:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( x ) \cr 2 & \quad \textbf{if}\ \text{the items are sorted }\ \textbf{then} \ \text{make them a heap from the bottom up} \cr 3 & \quad i \gets n;\ A[ i ] \gets x;\ n \gets n + 1;\ p \gets \lfloor ( i - 1 ) / 2 \rfloor \cr 4 & \quad \textbf{if}\ i = 0\ \textbf{then return} \cr 5 & \quad \textbf{if}\ i \text{ is on a level for the best} \ \textbf{then} \cr 6 & \qquad \textbf{if}\ \mathrm{better}( A[ p ] , A[ i ] ) \ \textbf{then}\ \text{swap } A[ i ] \text{ and } A[ p ]; \ \mathrm{up}_{\max}( p ) \cr 7 & \qquad \textbf{else}\ \mathrm{up}_{\min}( i ) \cr 8 & \quad \textbf{else if}\ \mathrm{better}( A[ i ] , A[ p ] ) \ \textbf{then}\ \text{swap } A[ i ] \text{ and } A[ p ]; \ \mathrm{up}_{\min}( p ) \cr 9 & \quad \textbf{else}\ \mathrm{up}_{\max}( i ) \end{array}} \]

◆ reserve()

template<class T, class Better = std::less< T >>
void ADS::MinMaxHeap< T, Better >::reserve ( std::size_t n)
inline

room for \( n \) items, so that the pushes up to \( n \) do not reallocate

◆ size()

template<class T, class Better = std::less< T >>
std::size_t ADS::MinMaxHeap< T, Better >::size ( void ) const
inlinenodiscard

the number of items, \( n \)


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