Project : Dynamic programming and Optimal BSTs

1. Introduction

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.

2. Set up

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.

3. The given code

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.

4. Your task

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

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.

5. Turn-in

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 9.


Thomas VanDrunen
Last modified: Thu Apr 19 14:14:52 CDT 2018