Computer Science 210 Lab 8: Balancing Binary Search Trees for Performance
Due: May 1, 1998

Objectives and Overview: Binary search trees can provide an efficient vehicle for searching, provided that they are well-balanced. However, as you know, the guarantee of balance in a binary search tree is not automatic; worst-case search of an n-element binary search tree is O(n) in complexity.

The so-called "AA tree" can be used to ensure that a binary search tree remains balanced as new nodes are inserted. This lab provides an opportunity for you to exercise the AA-tree. You should be familiar with the relevant sections of Chapter 16 in your text before doing this lab.

Part 1 - Exercise the New, Improved Cross-Reference Generator

On the CS210 (Tucker) server is a driver program called XRef2.cpp, a revised version of the cross-reference generator that uses AA trees. Drag this to the desktop, along with a fresh copy of the programs given in the following display to make a project called Lab8.µ.

Now run this program so that it will build an AA tree from the file tictactoe.cpp.

  1. What is the height of this tree, as reported in the output? Is this correct? Explain.
  2. Using what you know about the Skew, Split, and the recursive Insert function in the AATree.h file, show how the first five nodes in the AA tree will be inserted and the tree rebalanced after each insertion.
  3. Using the rest of the Depth information given in the output of your run, draw a picture of the final AA Tree for this input.

Part 2 - Modify This Program (Team Project)

The erroneous height given in the output comes from a faulty Depth function. That is, the Depth of a node in the tree is not simply the difference between that node's level and the root's level. This is only true if the node is not a right-horizontal child of its ancestor (that is, is not a "red" node, in the vernacular of a red-black tree). If the node is a right-horizontal child, then its depth is one greater than that of its parent, even though it has the same level as its parent.

  1. Make this correction to the Depth function inside the AATree class, and rerun the program with the tictactoe.cpp file as input to make sure that your correction gives the proper depth.
  2. Rerun this program now with the input file literacy.notes. What is the actual (correct) depth of that tree? How close is that depth to the optimal depth?
  3. Working as a team, figure out the actual entries in the tree and draw (with the aid of a program?) a picture that shows the entire structure of the AA tree for the literacy.notes input file. The important part of this exercise is that everyone on the team should contribute to the drawing in a material way. Identify who on your team did what, and turn in a single picture.

Lab 8 Deliverables:

Submit your team's modified C++ file AATreeteamname.h from Part 2 of this lab by dragging it to the Drop Box folder. Also hand in a hard copy listing of this file, along with your answers to the remaining questions in Part 1 and your team's answers to the questions in Part 2.

Part 2 of this lab must be done as a team, according to the team assignments given below. This team selection will be continued into the final project, where the need for a division of work will be more compelling.

Team A: BJ Bernard, James Choe, Joe Doyle, Mai-Lan Dunlop
Team B: Brian Glusman, Alexios H. Mihal, Victor Johnson, Prema Katari
Team C: Damon Lauder, Tim Lawson, Randy Loffelmacher, Kris Mohlman
Team D: Homa Mojtabai, Ernest Mugo, Chris Murphy, Nick O'Grady, Dan Pollard
Team E: Marco Quina, Rachel Sanders, Dara Sklar, Malik Tiwana, Brian Williams