Given a sorted array key[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches for keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
we organize a binary search tree
so as to minimize the number of nodes visited in all searches, given that we know
how often each element occurs, For this what we need is an Optimal Binary Search Tree.
Formally, we are given a sequence K = <k1, k2, ... , kn> of n distinct keys in sorted order,
(so that k1 < k2 < ... < kn), and we wish to build a binary search tree from these keys. For each key ki, we have a probability pi that a search will be for ki Some searches may be for values not in K, and so we also have n + 1 “dummy keys” d0, d1, d2, ... , dn representing values not in K.
Because we have probabilities of searches for each key and each dummy key, we can determine the expected cost of a search in a given binary search tree T.
General formula for calculating the minimum cost is:
C[i,j] = min{c[i, k-1] + c[k,j]} + w(i,j)
Comments