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

The optimal binary search tree. More...

Files

file  ADS/optimization/OptimalSearchTree.h
 The optimal binary search tree: the keys \( 0 , \ldots , n - 1 \) are searched with the weights \( p_i \) (frequencies or probabilities), and the gaps between them, the unsuccessful searches, with the weights \( q_0 , \ldots , q_n \); the tree of least weighted path length \( \sum_i p_i ( d_i + 1 ) + \sum_j q_j d'_j \), \( d_i \) the depth of the key \( i \) and \( d'_j \) that of the external node \( j \), the root at depth 0.

Classes

struct  ADS::OptimalSearchTree< T >
 an optimal binary search tree on the keys \( 0 , \ldots , n - 1 \): its cost, its root and the children of each key, \( -1 \) for none (and for the root of the empty tree) More...

Functions

template<class T>
OptimalSearchTree< T > ADS::optimalSearchTree (const std::vector< T > &p, const std::vector< T > &q)
 the optimal binary search tree of the keys of weights \( p \) and of the gaps of weights \( q \)
template<class T>
OptimalSearchTree< T > ADS::optimalSearchTree (const std::vector< T > &p)
 the tree of optimalSearchTree() above, with the gaps of weight 0

Detailed description

The optimal binary search tree.

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

Function documentation

◆ optimalSearchTree() [1/2]

template<class T>
OptimalSearchTree< T > ADS::optimalSearchTree ( const std::vector< T > & p)

the tree of optimalSearchTree() above, with the gaps of weight 0

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{optimalSearchTree}( p ) \cr 2 & \quad \textbf{return}\ \mathrm{optimalSearchTree}( p , ( 0 , \ldots , 0 ) ) \end{array}} \]

Precondition
\( p \) not negative

◆ optimalSearchTree() [2/2]

template<class T>
OptimalSearchTree< T > ADS::optimalSearchTree ( const std::vector< T > & p,
const std::vector< T > & q )

the optimal binary search tree of the keys of weights \( p \) and of the gaps of weights \( q \)

In pseudocode (exercise B4 of 11/01/2012, Chiavi frequenti, of the Esercizi Programmazione dinamica of the book, with the gaps and the bound of Knuth on the roots beyond the book), by increasing length \( h \) of the range of keys:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{optimalSearchTree}( p , q ) \cr 2 & \quad C[ i ][ i ] \gets 0 \text{ for each } i \cr 3 & \quad \textbf{for}\ h \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ n - h\ \textbf{do} \cr 5 & \qquad\quad j \gets i + h \cr 6 & \qquad\quad \textbf{if}\ h = 1\ \textbf{then}\ \mathit{lo} , \mathit{hi} \gets i\ \textbf{else}\ \mathit{lo} \gets R[ i ][ j - 1 ];\ \mathit{hi} \gets R[ i + 1 ][ j ] \cr 7 & \qquad\quad R[ i ][ j ] \gets \text{the largest } r \in [ \mathit{lo} , \mathit{hi} ] \text{ of least } C[ i ][ r ] + C[ r + 1 ][ j ] \cr 8 & \qquad\quad C[ i ][ j ] \gets W( i , j ) + C[ i ][ R[ i ][ j ] ] + C[ R[ i ][ j ] + 1 ][ j ] \cr 9 & \quad \textbf{return}\ C[ 0 ][ n ] \text{ and the tree built from } R[ 0 ][ n ] \text{ down} \end{array}} \]

Precondition
\( p \) and \( q \) not negative, \( |q| = |p| + 1 \)
Postcondition
a tree whose symmetric visit is \( 0 , \ldots , n - 1 \), of the cost found (audit)