Chapter 3 "optimal binary search tree" Based on Dynamic Planning"

Source: Internet
Author: User

This book starts from the case of Text Translation. It is assumed that English is translated into French, each English word is a keyword, and its corresponding French is satellite data. How to design this tree for Binary Search Tree storage. Even if it is a red/black tree, the time complexity of searching is O (lgn), that is, the depth of the tree. But becauseArticleA word may appear at different frequencies, so some frequently-used words such as the depth may be very deep, while the depth of uncommon aha may be very shallow. Intuitively, we should make this case closer to the root of the tree (in fact, even if the probability is highest, it is not necessarily the root of the tree ). We should minimize the expected number of searches for the entire tree, that is, build an optimal binary search tree. The left and right Subtrees of an optimal binary search tree are certainly the best. Array P is K1 ~ The probability of K5. K1 to K5 is a lexicographically ascending order. When an English word cannot be found, it will eventually go to the leaf. The probability of each leaf appearing is stored in the array Q. I have never figured out how the probability author of this leaf made it. Who knows to tell me.

ProgramOne note is the comparison of floating point numbers, which cannot be compared directly, unless the difference is very large. If the two floating point numbers are equal, it is also possible to use a larger number for determination.

CodeAs follows:

# Include <iostream> using namespace STD; void opticalbst (double * P, double * q, int N, double (* E) [6], INT (* root) [6]) {double W [n + 1] [6]; for (INT I = 1; I <= n + 1; I ++) {e [I] [I-1] = Q [I-1]; W [I] [I-1] = Q [I-1];} For (INT L = 1; L <= N; l ++) {for (INT I = 1; I <= N-L + 1; I ++) {Int J = L-1; E [I] [J] = 1 <30; // cout <E [I] [J]; System ("pause "); W [I] [J] = W [I] [J-1] + P [J] + Q [J]; // subtree I... total probability of J for (INT r = I; r <= J; r ++) {double T = E [I] [r-1] + E [R + 1] [J] + W [I] [J]; If (E [I] [J]-T> 0.0000000001) // E [I] [J]> T cannot be used directly, because if E [I] [J] and T are both floating point numbers, even if they are equal, the subtraction may not be equal to 0 {e [I] [J] = T; root [I] [J] = r ;}}}}} void constructopticalbst (INT (* root) [6], int I, Int J) {if (I = 1 & J = 5) {cout <"K" <root [I] [J] <"Yes root" <Endl; // system ("pause ");} if (I <root [I] [J]) {cout <"K" <root [I] [root [I] [J]-1] <"Yes K" <root [I] [J] <<"left child" <Endl; constructopticalbst (root, I, root [I] [J]-1 );} else {cout <"D" <root [I] [J]-1 <"yes" <root [I] [J] <" Left child "<Endl;} If (root [I] [J] <j) {cout <"K" <root [root [I] [J] + 1] [J] <"Yes K" <root [I] [J] <<"right child" <Endl; constructopticalbst (root, root [I] [J] + 1, J );} else {cout <"D" <root [I] [J] <"Yes K" <root [I] [J] <"right child" <<Endl ;}} int main () {int n = 5; Double P [6] = {0, 0.15, 0.1, 0.05, 0.1}; // The first one is not required. 1 ~ 5 represents K1 ~ K5, and K1 ~ K5 is arranged in ascending order. The order of K is very important for the output of the entire number. Double Q [6] = {0.05, 0.1, 0.05, 0.05, 0.05, 0.1}; Double E [7] [6]; int root [6] [6]; opticalbst (p, q, N, E, root); constructopticalbst (root, 1, 5); System ("pause"); Return 0 ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.