The goal of this project is to practice using dynamic programming to implement a solution to an optimization problem. In particular you will write an method for building and optimal BST.
The optimal BST problem is to build a binary search tree that is optimal not in terms of balance (which minimizes the worst case search time) but so as to minimize the expected case search time when the probability that a given key will be searched for is known ahead of time. In particular, keys that are more likely to be searched for are closer to the root than rarer keys.
The optimal BST can be built from a table of subtrees populated using dynamic programming.
This project corresponds to Section 5.5 in the book, but that section doesn't (yet) have a project description in it.
Find the project code in ~tvandrun/Public/cs345/optbst
.
As usual, it will have the three packages, adt
,
impl
, and test
,
and one additional package, optbstutil
,
which contains a class with a handful of useful static methods.
Take some time to explore the impl
package.
The class OptimalBSTMap
is given to you complete.
(Your task is to construct an instance of this class, but that is
done externally to the class itself.)
It implements the same Map
interface as the
balanced tree projects (recall that means remove()
is
removed), but the put()
method is unsupported.
This is because we assume we know all the keys (and their values)
ahead of time.
We will build the entire tree when we would instantiate
OptimalBSTMap
.
The class OptimalBSTData
is a class for simple
objects that hold the raw data from which the tree
will be built---keys, values, key probabilities, and miss
probabilities.
The class OptimalBSTMapFactory
is where the interesting
stuff happens.
This class holds a factory method for building
an OptimalBSTMap
from an OptimalBSTData
.
Implement the method
impl.OptimalBSTMapFactory.buildOptimalBST()
.
It is possible that you will find it useful to carve
some of the functionality into helper methods
(though my solution had the whole algorithm in one method).
The general structure is
Internal
nodes),
total weighted depth of subtrees,
and total probabiity of subtrees.
Internal
to
make a root of the best subtree, finding the children for that
internal node from appropriate places in the table.
[0][n-1]
and should have
optimal subtrees as its children
Your return statement will be something like
return new OptimalBSTMap( [root of the optimal tree] );
The package test
contains only one test file.
It tests for correctness and optimality of trees generated
by your factory method.
Copy the file you modified (OptimalBSTMapFactory.java
)
to your turn-in folder
/cslab/class/cs345/(your id)/optbst
.
To keep up with the course, this should be finished by April 15.