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

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. More...

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include "ADS/tree/SearchTree.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <functional>
#include <utility>
#include <vector>
#include "ADS/sort/impl/TreeSort.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 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.

On a Red-Black tree the insertions take \( O( n \log n ) \) time in the worst case, and on a binary search tree with no balance \( \Theta( n^2 ) \) on a sorted input, which is why the sort is the example of the cost of the balance; it is also the natural sort of a functional language, which builds the tree by recursion and flattens it.

The contracts [see Contract.h]: the vector ends with the same items sorted, an audit. The function is defined in TreeSort.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 6.1 and exercise 6.1, https://algoritmicamente.it/materiali/libro/capitoli/cap06.pdf
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa