10-Optimal Binary Search Tree

# 10-Optimal Binary Search Tree - Optimal Binary Search Tree...

• Notes
• 12

This preview shows pages 1–3. Sign up to view the full content.

Optimal Binary Search Tree We want to build a binary search tree which will contain keys K 1 to K n . They will be searched for over time with probabilities β 1 to β n respectively. Also, keys not in the tree whose values lie between Ki to K i+1 are searched for with probability α i for i from 1 to n-1, keys < K 1 with probability α 0 and keys > K n with probability α n . How can we build the tree so it has minimum weighted path length when α’s and β’s are taken as the weights of the nodes? Such a tree is called an optimal binary search tree. Discuss search through all trees - then how to construct the optimal tree and its corresponding recursive solution - and its drawback in terms of execution time due to a large number of repeated calls to solve the same problems. Let obst(i,j) denote the optimal solution for the problem involving keys K i to K j , w (i, j ) its optimal weighted path length and sw (i, j ) the sum of its weights. Then: obst(i,j) is obtained by : 1. For k = i, . . . , j find obst(i,k-1) and obst(k+1,j). 2. Find a k that minimizes [ w (i, k -1) + w ( k +1, j )] over k = i, . . . , j and let rij denote this value of k. 3. The solution has root rij, obst(i, rij -1) as its left subtree, and obst( rij +1,j) as its right subtree and w (i, j ) = sw (i, j ) + w (i, rij-1) + w (rij+1, j ). Solving our n key problem requires invoking obst(1,n). Obst(1,n) is given by the tree with root r1n, obst(1, r1n -1) as its left subtree, and obst( r1n +1,n) as its right subtree. The weighted path length of obst(1,n) is w (1, n ) where w (1, n ) = sw (1, n ) + w (1, r1n -1) + w ( r1n +1, n ).

This preview has intentionally blurred sections. Sign up to view the full version.

The nonrecursive efficient algorithm is: 1. For i from 0 to n Set w(i+1, i) to 0 and sw(i+1, i) to α i . For j from i+1 to n Set sw(i+1, j) to sw(i+1, j-1) + β j + α j . 2. For j from 1 to n Set w(j, j) to sw(j, j) and r(j, j) to j. 3. For s from 2 to n For j from s to n Set i to j-s+1. Set w(i, j) to sw(i, j) + minimum [w(i, k-1) + w(k+1, j)] i < k < j change to r(i, j-1)< k< r(i+1, j) later Set r(i, j) to the minimizing k. Example: β 1 β 2 β 3 β 4 β 5 β 6 10 3 9 2 0 10 α 0 α 1 α 2 α 3 α 4 α 5 α 6 5 6 4 4 3 8 0 In step 3, i is set to j-s+1 which means that j-i +1= s but this is the # keys involved in the optimal tree being determined. The value of s determines the diagonal of w and r that gets filled in, and this diagonal will then hold the information about all the optimal subtrees of size s with contiguous keys - this diagonal has n-s+1 entries. Since s starts at 2, in our example, the 5 entries 1,2 2,3 3,4 4,5 5,6 are filled first and when s finishes at n, there is only one entry, 1,6. This algorithm requires O ( n 3 ) execution time and O ( n 2 ) storage. Knuth, who developed this solution, noticed an important fact that reduces the execution time to O ( n 2 ). He was able to show that the minimization need not be over all k from 1 to n . Instead, the minimization may be taken only over all k between r(i,j - 1) and r(i + 1, j ), where r(i,j ) denotes the root of obst(i,j) . See Knuth [1973 Vol 3]. Thus the “change” above.
This is the end of the preview. Sign up to access the rest of the document.
• Fall '16
• James Korsh
• Binary Search, Greedy Tree Construction

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern