ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LoserTree.h

The tree of losers of a tournament among \( k \) players, the structure of the merge of \( k \) sorted sequences (Knuth 5.4.1; the notes, section 5.1.3, use a heap of \( k \) pairs for the same purpose). More...

Files

file  ADS/priority/LoserTree.h
 The tree of losers of a tournament among \( k \) players, the structure of the merge of \( k \) sorted sequences (Knuth 5.4.1; the notes, section 5.1.3, use a heap of \( k \) pairs for the same purpose).

Classes

class  ADS::LoserTree< T, C >
 the tree of losers of \( k \) players, the winner the smallest key by less More...

Functions

template<class T, class C = std::less<>>
std::vector< T > ADS::kWayMerge (const std::vector< std::vector< T > > &R, C less=C())
 the items of the sorted vectors of \( R \), merged through a tree of losers

Detailed description

The tree of losers of a tournament among \( k \) players, the structure of the merge of \( k \) sorted sequences (Knuth 5.4.1; the notes, section 5.1.3, use a heap of \( k \) pairs for the same purpose).

The definitions, the invariants and the costs of the area are in Theory of priority queues.

Function documentation

◆ kWayMerge()

template<class T, class C = std::less<>>
std::vector< T > ADS::kWayMerge ( const std::vector< std::vector< T > > & R,
C less = C() )

the items of the sorted vectors of \( R \), merged through a tree of losers

In pseudocode (Knuth 5.4.1, beyond the book), \( R_0 , \ldots , R_{k-1} \) the vectors:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kWayMerge}( R ) \cr 2 & \quad T \gets \text{the tree of losers of the first items of } R_0 , \ldots , R_{k-1} \text{, none for an empty one} \cr 3 & \quad \mathit{out} \gets \text{an empty vector} \cr 4 & \quad \textbf{while not}\ \mathrm{empty}( T )\ \textbf{do} \cr 5 & \qquad w \gets \mathrm{winner}( T );\ \text{append } \mathrm{top}( T ) \text{ to } \mathit{out} \cr 6 & \qquad \mathrm{replace}( T , \text{the next item of } R_w \text{, none if it is over} ) \cr 7 & \quad \textbf{return}\ \mathit{out} \end{array}} \]

Precondition
each vector of \( R \) sorted
Postcondition
the items of \( R \), sorted (an audit)