Computer Science 210 Lab 9: Binary Search Trees and Search Performance
Due: December 1, 1997

Objectives and Overview: Binary search trees can provide an efficient vehicle for searching, provided that they are well-balanced. However, the guarantee of balance in a binary search tree is not automatic-in the worst case, a search of an n-element binary search tree is O(n) in complexity. The idea of a splay tree can be used to redefine the add and remove functions in such a way that the tree remains better balanced. This lab suggests an experiment that will allow you to compare the efficiency of search using binary search trees and splay trees. You should be familiar with the relevant sections of Chapter 13 in your text before doing this lab.

Part 1 - Exercise the BTSearchDriver Program

On the CS210 (Tucker) server is a driver program called BTSearchDriver.java that you should drag to the desktop, along with a fresh copy of Assert.java (that's in the Source folder) and Structure.zip. Build a project called Lab9yourname that looks like the one below:

Now run this program that builds a 1000-integer binary search tree and then searches for the integer 1001 in this tree.

  1. Draw a diagram of the topmost 8 levels of this tree (beginning with the root).
  2. Considering the nature of the add method inside the BinarySeachTree class (which the program currently uses), what is the complexity of the search for the integer 1001 in this particular tree.
  3. Is this particular program designed to explore the "worst case" complexity of binary tree search? Explain.
  4. In what different way could the integers from 1 to 1000 have been added to an empty binary search tree that would ensure a more efficient search?
  5. What would be the ideal shape of a binary search tree containing 1000 integers? What would be the optimal search complexity for such a tree?

Part 2 - Modify This Program

Modify this program so that it uses the splay tree as a medium for storing integers, and rerun it for 1000 integers and a search for the integer 1001.

  1. What is the difference in performance between this run and the run you made earlier with the same data?
  2. Draw a diagram of the first four levels of the splay tree that results from this run. You might want to add an inorder traversal to the program to save you the work of figuring out by hand which of the 1000 integers migrate to the top of this tree.
  3. Now run the program for tree sizes of 100, 500, 1000, and 2000, each time searching for the integer 2001. Record the time elapsed in milliseconds for each of these runs.
  4. (Optional) Does this search time seem to come close to the optimal search complexity for binary trees? Explain.

Lab 9 Deliverables:

Submit your Java program from Part 2 of this lab by dragging it to the Drop Box folder. Also hand in a hard copy listing of your program, along with your answers to the questions in Parts 1 and 2.

Feel free to work on the programming part of this lab individually or in teams of two. But please complete the answers to the questions individually.