|
ADS
Algorithms and Data Structures
|
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. More...
#include "ADS/Contract.h"#include <algorithm>#include <cstddef>#include <vector>#include "ADS/optimization/impl/OptimalSearchTree.tpp"Go to the source code of this file.
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... | |
Namespaces | |
| namespace | ADS |
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 | |
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.
With \( W( i , j ) = \sum_{i \le l < j} p_l + \sum_{i \le l \le j} q_l \) the cost of the keys \( i , \ldots , j - 1 \) is \( C[ i ][ i ] = 0 \) and
\[ C[ i ][ j ] = W( i , j ) + \min_{i \le r < j} C[ i ][ r ] + C[ r + 1 ][ j ] , \]
\( O( n^3 ) \) time as written. Knuth proved that a best root \( R[ i ][ j ] \) can be looked for between \( R[ i ][ j - 1 ] \) and \( R[ i + 1 ][ j ] \), which makes the total time \( O( n^2 ) \) by a telescoping sum, with \( O( n^2 ) \) space; Yao derived it from the quadrangle inequality of \( W \), the largest best root being monotone. optimalSearchTree() does so.
In the literature: Hu and Tucker, and Garsia and Wachs, build in \( O( n \log n ) \) the optimal alphabetic tree, where only the \( q_j \) count (the keys in the leaves); a tree within a constant of the optimum comes in \( O( n ) \) from the weight-balancing rule of Mehlhorn; the splay trees are within a constant of the optimal static tree without knowing the weights [see SplayTree.h].
The contracts [see Contract.h]: the weights are not negative and \( |q| = |p| + 1 \) when \( q \) is given; the tree has \( 0 , \ldots , n - 1 \) as its symmetric visit and the cost found (checked at level 3). The functions are defined in OptimalSearchTree.tpp.