Introduction to algorithms 15.5 optimal binary search tree implementation (Java language)

Source: Internet
Author: User

These two days are watchingAlgorithmThe optimal binary search tree in the introduction. for how to implement the dynamic planning of the optimal binary search tree, see Chapter 15th of Introduction to algorithms.

I have pasted the optimal binary search tree implemented in Java for your reference.

Package homework. part5.work1; import Java. util. optional; public class optimalbst {private double [] P; private double [] q; private int N; private double [] [] E; private double [] [] W; private int [] [] root; public static void main (string [] ARGs) {// todo auto-generated method stuboptimalbst sample = new optimalbst (); sample. getinputinfo (); sample. optimalbst (); sample. constructoptimalbst (sample. root, 1, sample. n);} public void getinputinfo () {system. out. println ("Number of input keywords"); keyword in = New Keyword (system. in); this. N = in. nextint (); P = new double [n + 1]; q = new double [n + 1]; system. out. println ("input in sequence" + this. N + "probability P of the keyword"); For (INT I = 0; I <n; I ++) {P [I + 1] = in. nextdouble ();} system. out. println ("input in turn" + (this. n + 1) + "probability of non-Keyword Q"); For (INT I = 0; I <= N; I ++) {q [I] = in. nextdouble () ;}} public void optimalbst () {e = new double [n + 2] [n + 1]; W = new double [n + 2] [n + 1]; root = new int [n + 1] [n + 1]; for (INT I = 1; I <n + 2; 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 = I + L-1; E [I] [J] = double. max_value; 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 ;}}}}} public void constructoptimalbst (INT [] [] root, int I, Int J) {int r = root [I] [J]; if (I = 1 & J = This. n) {system. out. println ("K" + R + "is root");} If (R-1 <I) {system. out. println ("D" + (r-1) + "is K" + R + "Left subtree");} else {system. out. println ("K" + root [I] [r-1] + "is K" + R + "Left subtree"); constructoptimalbst (root, I, R-1 );} if (j <R + 1) {system. out. println ("D" + J + "is the right subtree of K" + R + ");} else {system. out. println ("K" + root [R + 1] [J] + "is K" + R + "right subtree"); constructoptimalbst (root, R + 1, j );}}}

Here is an example of using books to test the aboveProgramThe test results are as follows:

 
Number of input keywords 5 input probability of five keywords in turn listen 15 0.10 0.05 0.10 0.20 Input 6 probability of non-keywords in turn q0.05 0.10 0.05 0.05 0.05 0.10k2 is the left subtree where K1 is K2 D0 is the left subtree of K1, d1 is K1, right subtree K5 is K2, right subtree K4 is K5, left subtree K3 is K4, left subtree D2 is K3, left subtree D3 is K3 the right subtree D4 is K4. The right subtree D5 is K5's right subtree.

From the above running results, the optimal binary search tree in the book is 15-7 (B ).

Next we will use the above program to do the exercises 15.5-2 after class. The running results are as follows:

 
Enter the number of keywords 7 enter the probability of 7 keywords in turn 04 0.06 0.08 0.02 0.10 0.12 0.14 enter the probability of 8 non-keywords in sequence q0.06 0.06 0.06 0.06 0.05 0.05 0.05k5 is the root K2 is K5 the left subtree K1 is K2, the left subtree D0 is K1, the left subtree d1 is K1, the right subtree K3 is K2, the right subtree D2 is K3, the left subtree K4 is K3, And the right subtree subtree D3 is K4 left subtree D4 is K4 right subtree K7 is K5 right subtree K6 is K7 left subtree D5 is K6 left subtree D6 is K6 right subtree tree D7 is the right subtree of K7.

Expected search cost: 3.12
The structure of the optimal binary search tree is as follows:


 

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.