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

The tree sort (exercise 6.1 and its solution). More...

Files

file  ADS/sort/TreeSort.h
 The tree sort (exercise 6.1 and its solution): the items are inserted in a search tree [see SearchTree.h] and read back by the symmetric visit, which gives the keys in increasing order; each key keeps the number of its copies, so that the repeated items are not lost.

Functions

template<RankStore Store, std::totally_ordered T = std::ranges::range_value_t< std::remove_cvref_t< Store > >>
void ADS::treeSort (Store &&A, bool balanced=true)
 the tree sort on a Red-Black tree, or on a search tree with no balance if balanced is false
template<std::totally_ordered T>
void ADS::treeSort (std::vector< T > &A, bool balanced=true)
 treeSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { treeSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::treeSort (R &&A, P &&... p)
 treeSort() on the range \( A \) [see SortRange], the other arguments as for a vector, in place: on the span of its items if \( A \) is contiguous, on its view otherwise, since the sort reads the items by their ranks only

Detailed description

The tree sort (exercise 6.1 and its solution).

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

Function documentation

◆ treeSort() [1/3]

template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { treeSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::treeSort ( R && A,
P &&... p )

treeSort() on the range \( A \) [see SortRange], the other arguments as for a vector, in place: on the span of its items if \( A \) is contiguous, on its view otherwise, since the sort reads the items by their ranks only

◆ treeSort() [2/3]

template<std::totally_ordered T>
void ADS::treeSort ( std::vector< T > & A,
bool balanced = true )

treeSort() above on a vector

◆ treeSort() [3/3]

template<RankStore Store, std::totally_ordered T = std::ranges::range_value_t< std::remove_cvref_t< Store > >>
void ADS::treeSort ( Store && A,
bool balanced = true )

the tree sort on a Red-Black tree, or on a search tree with no balance if balanced is false

In pseudocode (the search trees of section 6.1 and exercise 6.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{treeSort}( A ) \cr 2 & \quad S \gets \text{an empty search tree from a key to its copies} \cr 3 & \quad \textbf{foreach}\ x \in A\ \textbf{do} \cr 4 & \qquad c \gets S.\mathrm{lookup}( x );\ S.\mathrm{insert}( x , 1 ) \text{ if } c = \textbf{nil} \text{, else } S.\mathrm{insert}( x , c + 1 ) \cr 5 & \quad i \gets 0 \cr 6 & \quad \textbf{foreach}\ ( k , c ) \in S \text{ in symmetric order} \ \textbf{do} \cr 7 & \qquad \text{write } k \text{ in } A[ i .. i + c - 1 ];\ i \gets i + c \end{array}} \]

Postcondition
\( A \) is sorted, with the same items (audit)