Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Problem statement:

..The key values are stored in the Binary Search Tree's internal nodes (BST). The exterior nodes
are made up of null nodes. The keys are lexicographically ordered, which means that all keys in
the left sub-tree are smaller than the node's keys, while all keys in the right sub-tree are bigger.

Knowing the frequency of querying each of the keys makes it straightforward to determine the
expected cost of visiting each node in the tree. Because it has the lowest expected cost of
identifying each node, a BST is an excellent binary search tree.

The search time of a BST element is O(n), whereas the search time of a Balanced-BST element
is O(n) (log n). The most frequently used data is placed in the root and closer to the root element
in the Optimal Cost Binary Search Tree, whereas the least frequently used data is placed near
leaves and in leaves..

Pseudo-code:

Optimal-Binary-Search-Tree(p, q, n)
“e[1…n + 1, 0…n],
w[1…n + 1, 0…n],
root[1…n + 1, 0…n]
for i = 1 to n + 1 do
e[i, i - 1] := qi - 1
w[i, i - 1] := qi - 1
for l = 1 to n do
for i = 1 to n – l + 1 do
j = i + l – 1 e[i, j] := ∞
w[i, i] := w[i, i -1] + pj + qj
for r = i to j do
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
return e and root”
“Mathematical proof:
j
C(T) ¿ ∑ pl(depthT (l)+1)
l=i

k −1 j
= ∑ pl(depthT (l)+1+1) + pk + ∑ pl( depthT (l)+1)
l=i l=k+ 1

j
= c(Tl) + C(TR) + ∑ pl
l=i

The algorithm requires O (n3) time, since three nested for loops are used. Each of these loops takes on at
most n values.”

pi
0.35

0.3

0.25

0.2 pi

0.15

0.1

0.05

0
0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5

Summary:

A binary search tree algorithm known as the Optimal Binary Search Tree Algorithm is discussed. In the
beginning, we create a BST using the n keys given in the form of k1-kn (keys 1, 2, 3,..., kn). Suppose
accessing a key Ki is pi times more likely than not. To allow for searches for values that aren't in the Key
set K, some fake keys are included (d0, d1, d2,... dn). We suppose that the probability of access is qi for
each dummy key di.

The concept behind the above method is straightforward: we just test each node as root one at a time (r
varies from I to j in second term). The optimum cost is calculated in a recursive manner from I to r-1 and
from r+1 to j when the rth node is made the root.
Assign sums to frequencies from 1 to 5 (see first term in the above formula)

You might also like