 
Summary: CS 5050 Program 1 (30 points)
Optimal Binary Search Trees
Introduction
A binary search tree is a tree where the key values are stored in the internal nodes, the external nodes (leaves)
are null nodes, and the keys are ordered lexicographically, i.e. for each internal node all the keys in the left
subtree are less than the keys in the node, and all the keys in the right subtree are greater. You should have
created binary search trees in the prerequisite course.
When we know the frequency of searching each one of the keys, it is quite easy to compute the expected cost of
accessing each node in the tree. An optimal binary search tree is a BST which has minimal expected cost of
locating each node. In this version of the problem, we are not concerned with the frequency of searching for a
missing node. (If we needed that, we would also be concerned with the height of the null pointers.)
Example:
Node ID 0 1 2 3 4 5
Key A B C D E F
Frequency 4 1 1 2 8 16
The expected cost of 83 is computed by multiplying each frequency by its level (starting with the root at 1). In
general, let bestCost[i][j] be the expectedCost of the best tree consisting of nodes i through j. A different tree
will have a different expected cost:
It's clear that this tree is not optimal.  It is easy to see that if F is closer to the root, given its high frequency,
the tree will have a lower expected cost.
