|
ADS
Algorithms and Data Structures
|
an interval 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 | |
| IntervalHeap (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, the \( \mathit{lo} \) of the root | |
| const T & | max (void) const |
| the worst item, the \( \mathit{hi} \) 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 | |
an interval heap of plain items: the best and the worst by better()
| using ADS::IntervalHeap< T, Better >::Item = T |
the type of the items
|
inlineexplicit |
the empty queue of the order better
|
inline |
removes all the items
|
inlinenodiscard |
whether the queue has no item
|
nodiscard |
the worst item, the \( \mathit{hi} \) of the root
|
nodiscard |
the best item, the \( \mathit{lo} \) of the root
| T ADS::IntervalHeap< T, Better >::popMax | ( | void | ) |
removes the worst item and returns it, in \( O( \log n ) \) amortized time
In pseudocode (van Leeuwen and Wood, beyond the book): the walk of popMin() on the \( \mathit{hi} \) of the nodes with the order reversed, the \( \mathit{hi} \) of a node of one item being its \( \mathit{lo} \), and the item carried down swapped with the \( \mathit{lo} \) of a node when better than it.
\[\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 \textbf{if}\ n \le 2\ \textbf{then}\ n \gets n - 1; \ \textbf{return}\ A[ n ] \cr 5 & \quad x \gets A[ 1 ];\ n \gets n - 1;\ y \gets A[ n ];\ h \gets 1;\ j \gets 0 \cr 6 & \quad \textbf{loop} \cr 7 & \qquad c \gets \text{the child of } j \text{ of worse } \mathit{hi} \text{, if any};\ h' \gets \text{the place of its } \mathit{hi} \cr 8 & \qquad \textbf{if}\ c = \textbf{nil}\ \textbf{or not} \ \mathrm{better}( y , A[ h' ] )\ \textbf{then}\ \textbf{break} \cr 9 & \qquad A[ h ] \gets A[ h' ];\ h \gets h';\ j \gets c \cr 10 & \qquad \textbf{if}\ h = 2 j + 1\ \textbf{and}\ \mathrm{better}( y , A[ 2 j ] )\ \textbf{then}\ \text{swap } y \text{ and } A[ 2 j ] \cr 11 & \quad A[ h ] \gets y;\ \textbf{return}\ x \end{array}} \]
| T ADS::IntervalHeap< T, Better >::popMin | ( | void | ) |
removes the best item and returns it, in \( O( \log n ) \) amortized time
In pseudocode (van Leeuwen and Wood, beyond the book), the hole of the \( \mathit{lo} \) of the root moved down by the better \( \mathit{lo} \) of the children:
\[\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 ] \cr 5 & \quad \textbf{if}\ n \le 2\ \textbf{then}\ \text{remove } A[ 0 ] \text{, the other item to its place};\ \textbf{return}\ x \cr 6 & \quad n \gets n - 1;\ y \gets A[ n ];\ j \gets 0 \cr 7 & \quad \textbf{loop} \cr 8 & \qquad c \gets \text{the child of } j \text{ of better } \mathit{lo} \text{, if any} \cr 9 & \qquad \textbf{if}\ c = \textbf{nil}\ \textbf{or not} \ \mathrm{better}( A[ 2 c ] , y )\ \textbf{then}\ \textbf{break} \cr 10 & \qquad A[ 2 j ] \gets A[ 2 c ];\ j \gets c \cr 11 & \qquad \textbf{if}\ 2 j + 1 < n\ \textbf{and}\ \mathrm{better}( A[ 2 j + 1 ] , y )\ \textbf{then}\ \text{swap } y \text{ and } A[ 2 j + 1 ] \cr 12 & \quad A[ 2 j ] \gets y;\ \textbf{return}\ x \end{array}} \]
| void ADS::IntervalHeap< T, Better >::push | ( | const T & | x | ) |
pushes \( x \), in \( O( \log n ) \) time
In pseudocode (van Leeuwen and Wood, beyond the book), the node \( j \) in the places \( 2 j \) (its \( \mathit{lo} \)) and \( 2 j + 1 \) (its \( \mathit{hi} \)), \( q \) the parent of \( j \):
\[\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 the nodes of the } j \text{-th best and worst} \cr 3 & \quad i \gets n;\ A[ i ] \gets x;\ n \gets n + 1;\ j \gets \lfloor i / 2 \rfloor \cr 4 & \quad \textbf{if}\ i \text{ is odd}\ \textbf{then}\quad \triangleright\ \text{the second item of its node} \cr 5 & \qquad \mathit{low} \gets \mathrm{better}( A[ i ] , A[ i - 1 ] ); \ \textbf{if}\ \mathit{low}\ \textbf{then}\ \text{swap } A[ i ] \text{ and } A[ i - 1 ] \cr 6 & \quad \textbf{else if}\ j = 0\ \textbf{then return} \cr 7 & \quad \textbf{else if}\ \mathrm{better}( x , A[ 2 q ] ) \ \textbf{then}\ \mathit{low} \gets \textbf{true} \cr 8 & \quad \textbf{else if}\ \mathrm{better}( A[ 2 q + 1 ] , x ) \ \textbf{then}\ \mathit{low} \gets \textbf{false} \cr 9 & \quad \textbf{else return}\quad \triangleright\ \text{inside} \text{ the interval of its parent} \cr 10 & \quad \textbf{if}\ \mathit{low}\ \textbf{then}\ \text{move the} \text{ item up by the } \mathit{lo} \text{ of the ancestors while} \text{ better} \cr 11 & \quad \textbf{else}\ \text{move it up by the } \mathit{hi} \text{ of the ancestors while worse} \end{array}} \]
|
inline |
room for \( n \) items, so that the pushes up to \( n \) do not reallocate
|
inlinenodiscard |
the number of items, \( n \)