Optimal binary search tree (Dynamic programming)

Source: Internet
Author: User


1. Description

Given an ordered sequence of k={k1<k2<k3<,......, <kn} and the probability of their being queried p={p1,p2,p3,......, PN}, a binary lookup tree T is constructed, which minimizes the total cost of querying all elements. For a search tree, the search succeeds when the searched element is inside the tree. When not in the tree, indicates that the search failed, with a "virtual leaf node" to indicate the failure of the search, so need to n+1 a virtual leaf node {D0<D1<......<DN}. Where D0 indicates that the search element is less than the K1 failure result, and the DN indicates a failed search element greater than kn. Di (0<i<n) indicates the failure of the search node between Ki and K (i+1). For the probability sequence that should be di is q={q0,q1,......, qn}.


2. Analysis

(1) Optimal substructure

A subtree of an optimal binary tree must contain a continuous range of keywords ki~kj,1 <= i <= j<= N, and also contain continuous virtual leaf node di-1~dj.

If an optimal binary lookup tree T has a subtree containing the keyword ki~kj t ', then T ' is also an optimal search tree, which can be proved by clipping thought.

Structure optimal substructure: In ki~kj, select a r,i <= R <= J, make the KR root, Ki~k (r-1) and K (r+1) ~kj as the optimal binary tree for the children. Note the case of r=i or r=j, which indicates that there are only virtual leaf nodes in the left or right sub-tree.

(2) Recursive formula

Defines E[I,J] as the desired cost of an optimal binary tree containing the keyword ki~kj. When j=i-1 there is no real key in, only the virtual leaf node D (i-1).

The

E[I,I-1] = q (i-1) j = i-1,

When J >= I, it is necessary to select the appropriate KR as the root node, and then the remaining nodes Ki~k (r-1) and K (r+1) ~kj constructs the child. At this point to consider the children of these nodes become a sub-tree of a node, its search cost changes: Their expected cost increases "the sum of all probabilities in the subtree" W.

W[I,J] = (pi + ... + pj) + (qi-1 + ... + qj)

So when j>= I, E[I,J]=PR + (E[i,r-1]+w[i,r-1]) + (e[r+1,j]+w[r+1,j]) = e[i,r-1] +e[r+1,j]+w[i,j];

(3) Calculate the expected cost of the optimal binary tree

e[i,j]=

Q (i-1) j = i-1

MIN{E[I,R-1] +e[r+1,j]+w[i,j]} i <= J, wherein I <= R <= J

W[I,J] =

Q (i-1) j = i-1

W[i,j]=w[i,j-1]+pj+qj I <= J time

3. Algorithms

The algorithm is as follows:

void Optimalbst (int n, float p[], float q[])//The length of P and Q is (n + 1)
{
    float w[n + 2][n + 1], E[n + 2] [n + 1], temp;
    int Root[n + 1][n + 1];
    int I, J, L, R;
    for (i = 1;i <= n + 1; i++)
        w[i][i-1] = e[i][i-1] = q[i-1];
    for (L = 1; l <= N; l++)
        for (i = 1, j = l; I <= n-l + 1; i++, J + +)
        {
            e[i][j] = Int_max;
            W[I][J] = W[i][j-1] + p[j] + q[j];
            for (r = i; r <= J; r++) 
            {
                temp = E[i][r-1] + e[r + 1][j] + w[i][j];
                if (E[i][j] > Temp)
                {
                    e[i][j] = temp;
                    ROOT[I][J] = r;
                }
            } for (r = i;)
        }
    for printf ("The optimal search cost is%.2f\n", E[1][n]);
    Constructoptimalbst (n, root);
}


Root is the root node selected during the construction of the record to construct the optimal solution.

The CONSTRUCTOPTIMALBST function is as follows:

void Constructoptimalbst (int n, int root[][n + 1])
{
    printf ("k%d is the root\n", Root[1][n]);
    Recursiveconstruct (n, Root, Root[1][n], 1, n);
}


Where the Recursiveconstruct function is as follows:

void Recursiveconstruct (int n, int root[][n + 1], int r, int i, int j)
{
    if (j = = i-1)
    {
        if (i = = r) 
  
   printf ("d%d is the left child of k%d\n", R-1, R);
        else 
            printf ("d%d is the right child of k%d\n", R, R);
    } else {
        if (i! = r) {
            printf ("k%d of k%d\n ", Root[i][r-1], R);
            Recursiveconstruct (n, Root, root[i][r-1], I, r-1);
        } else{
            printf ("d%d is the left child of k%d\n", R-1, R);

        if (j! = r) {
            printf ("k%d is the right child of k%d\n", Root[r + 1][j], R);
            Recursiveconstruct (n, Root, Root[r + 1][j], R + 1, j);
        } else
            printf ("d%d is the right child of k%d\n", R, R);
    }

  


The test is as follows:


#include <stdio.h>                                                                                                           
#include <limits.h>

void optimalbst (int n, float p[], float q[]);
void Constructoptimalbst (int n, int root[][n + 1]);
void Recursiveconstruct (int n, int root[][n + 1], int r, int i, int j);
int main (void)
{
    float p[] = {-1, 0.15, 0.10, 0.05, 0.10, 0.20};
    Float q[] = {0.05, 0.10, 0.05, 0.05, 0.05, 0.10};
    int n = 5;
    Optimalbst (n, p, q);
    return 0;
}


Output:



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.