Introduction to algorithms --------------- optimal binary search tree
Given an ordered sequence consisting of n different keywords K = {k1
The probability that each keyword and virtual key are searched can determine the expected price of one search in a given binary search tree T. Set the actual cost of a search to the number of nodes to be checked. That is, add 1 to the depth of the nodes found in the T search. Therefore, the expected price for a search within T is:
It should be noted that an optimal binary search tree is not necessarily the smallest tree in the overall height, nor always the keyword of the maximum probability is put at the root.
Dynamic Planning and Solving Process
1) Structure of the optimal binary search tree
If an optimal binary search tree T contains the key word ki ,......, Kj subtree T ", then this subtree T 'for the key word ki ,...... Kj and virtual key di-1 ,......, The sub-Problem of dj must also be optimal.
2) a recursive Solution
Define e [I, j] To search for a key word ki ,......, The expected cost of the optimal binary search tree of kj is classified as follows:
When j = I-1, it means there is only a virtual key di-1, so e [I, I-1] = qi-1
When j is greater than or equal to I ,......, In kj, select one with kr, and then use the key word ki ,......, Kr-1 to construct an optimal binary tree as the left subtree, using the keyword kr + 1 ,......, Kj to construct an optimal binary search tree as the right subtree. Define a key word ki ,......, The subtree of kj defines the sum of probabilities:
Therefore, if kr is a key ,......, The root of the optimal subtree of kj is:
Therefore, e [I, j] is rewritten:
The final recursion formula is as follows:
# Include
# Include
// # Include
// # Include
Using namespace std; pair
>, Vector
> Optimal_binary_search_tree (double * p, double * q, int n) {vector
> E (n + 2, vector
(N + 1, 0); vector
> Root (n + 1, vector
(N + 1, 0); vector
> W (n + 2, vector
(N + 1, 0); for (int I = 1; I <= n + 1; ++ I) {e [I] [I-1] = q [I-1]; w [I] [I-1] = q [I-1];} for (int len = 1; len <= n; ++ len) {for (int I = 1; I <= n-len + 1; ++ I) {int j = I + len-1; e [I] [j] = 100.0; w [I] [j] = w [I] [j-1] + p [j] + q [j]; for (int r = I; r <= j; ++ r) {double t = e [I] [r-1] + e [r + 1] [j] + w [I] [j]; if (t <e [I] [j]) {e [I] [j] = t; root [I] [j] = r ;}}}} return make_pair (e, root);} void Construct_Optimal_BST (vector
> Root, int I, int j, bool flag) {if (flag = 0) {cout <"k" <root [I] [j] <"Yes root" <endl; flag = 1 ;} int r = root [I] [j]; // if the left subtree is a leaf if (r-1
= J) {cout <"d" <j <"is the right child of" <"K" <r <endl ;} // if the right subtree is not a leaf else {cout <"k" <root [r + 1] [j] <"is the right child of" <"K "<r <endl; construct_Optimal_BST (root, r + 1, j, 1) ;}} void construct_optimal_bst2 (vector
> Root, int I, int j) {int n = j-I + 1; if (I = 1 & j = n) cout <"k" <root [1] [n] <"Yes root" <endl; if (I
J) cout <"d" <j <"Yes k" <j <"right child" <endl;} int main () {double p [6] = {0, 0.15, 0.1, 0.05, 0.1, 0.2}; double q [6] = {0.05, 0.1, 0.05, 0.05, 0.05, 0.1}; vector
> E (5 + 2, vector
(5 + 1, 0); vector
> Root (5 + 1, vector
(5 + 1, 0); int n = sizeof (p)/sizeof (double); cout <n <endl; e = optimal_binary_search_tree (p, q, 5 ). first; root = optimal_binary_search_tree (p, q, 5 ). second; cout <"The cost of the optimal binary tree is:" <
(Cout, ""); cout <endl;} cout <"-----------------------------------------" <endl; for (int I = 1; I <= 5 + 1; + + I) {for (int j = I-1; j <= 5; ++ j) {cout <e [I] [j] <"";} cout <endl ;}cout <endl; cout <"each sub-tree root is shown in the following table:" <endl; for (int I = 1; I <= 5; ++ I) {copy (root [I]. begin (), root [I]. end (), ostream_iterator
(Cout, ""); cout <endl ;}cout <endl; cout <"--------------------------------------" <endl; for (int I = 1; I <= 5; ++ I) {for (int j = I; j <= 5; ++ j) the optimal binary search tree constructed by {cout <root [I] [j] <";}cout <endl ;}cout <" is as follows: "<endl; Construct_Optimal_BST (root, 1, 5, 0); cout <" ------------------------------------ "<endl; construct_optimal_bst2 (root, 1, 5 );}