|
ADS
Algorithms and Data Structures
|
the tree of losers of \( k \) players, the winner the smallest key by less More...
#include <ADS/priority/LoserTree.h>
Public member functions | |
| LoserTree (std::vector< std::optional< T > > keys, C less=C()) | |
| the tree of the players with their first keys, an empty key for an exhausted one, its \( k - 1 \) matches played in \( O( k ) \) time | |
| std::size_t | players (void) const |
| the number of players, \( k \) | |
| bool | empty (void) const |
| whether all the players are exhausted | |
| std::size_t | winner (void) const |
| the player with the smallest key | |
| const T & | top (void) const |
| the key of the winner | |
| void | replace (std::optional< T > key) |
| the winner takes the key, or is exhausted if it is empty, and the matches on its path are replayed, \( \lceil \log_2 k \rceil \) comparisons at most | |
the tree of losers of \( k \) players, the winner the smallest key by less
|
explicit |
the tree of the players with their first keys, an empty key for an exhausted one, its \( k - 1 \) matches played in \( O( k ) \) time
In pseudocode (Knuth 5.4.1, beyond the book), \( L[ v ] \) the loser at the node \( v \) and \( W[ v ] \) the winner below it, the leaf \( k + i \) the player \( i \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{build}( \mathit{key}[ 0 .. k - 1 ] ) \cr 2 & \quad \textbf{for}\ v \gets k - 1\ \textbf{downto}\ 1 \ \textbf{do} \cr 3 & \qquad a \gets \text{the player of the node } 2 v;\ b \gets \text{the player of the node } 2 v + 1 \cr 4 & \qquad \textbf{if}\ \mathrm{beats}( a , b )\ \textbf{then}\ W[ v ] \gets a;\ L[ v ] \gets b\ \textbf{else}\ W[ v ] \gets b;\ L[ v ] \gets a \cr 5 & \quad L[ 0 ] \gets W[ 1 ]\ \text{if } k > 1 \text{, else } 0 \cr 6 & \textbf{function}\ \mathrm{beats}( i , j ) \cr 7 & \quad \textbf{if}\ i \text{ is exhausted}\ \textbf{then} \ \textbf{return false} \cr 8 & \quad \textbf{if}\ j \text{ is exhausted}\ \textbf{then} \ \textbf{return true} \cr 9 & \quad \textbf{return}\ \mathit{key}[ i ] < \mathit{key}[ j ] \ \textbf{or}\ ( \mathit{key}[ i ] = \mathit{key}[ j ] \ \textbf{and}\ i < j ) \end{array}} \]
|
nodiscard |
whether all the players are exhausted
|
nodiscard |
the number of players, \( k \)
| void ADS::LoserTree< T, C >::replace | ( | std::optional< T > | key | ) |
the winner takes the key, or is exhausted if it is empty, and the matches on its path are replayed, \( \lceil \log_2 k \rceil \) comparisons at most
In pseudocode (Knuth 5.4.1, beyond the book), with \( \mathrm{beats}() \) of the constructor:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{replace}( x ) \cr 2 & \quad w \gets L[ 0 ];\ \mathit{key}[ w ] \gets x \cr 3 & \quad v \gets \lfloor ( w + k ) / 2 \rfloor \cr 4 & \quad \textbf{while}\ v \ge 1\ \textbf{do} \cr 5 & \qquad \textbf{if}\ \mathrm{beats}( L[ v ] , w )\ \textbf{then} \ \text{swap } w \text{ and } L[ v ] \cr 6 & \qquad v \gets \lfloor v / 2 \rfloor \cr 7 & \quad L[ 0 ] \gets w \end{array}} \]
|
nodiscard |
the key of the winner
|
nodiscard |
the player with the smallest key