CS 2005, B Term 1999
Data Structures and Programming Techniques
HW5 (due Dec. 10)

This page is located at http://www.cs.wpi.edu/~alvarez/CS2005/B99/HW5/


This assignment continues the study of binary search trees, including AVL tree rotations and an analysis of binary tree and binary search algorithms. Part of the assignment involves C++ programming, and the rest involves analysis problems in English (Greek letters and weird characters are optional). Nearly complete source files for the programming portion of the assignment are available in the directory /cs/cs2005/samples/hw5/, together with a sample executable file named treerotate and a sample input file named itemfile.


Programming portion

  1. Review your class notes on AVL trees, recursion, and induction.
  2. Examine the header file bintree3.h available in the directory /cs/cs2005/samples/hw5/. This file contains the specification for an extended binary tree toolkit containing two new functions tree_rotate_left and tree_rotate_right in addition to the functions provided by the toolkit bintree2.h used in HW4.
  3. Implement the functions tree_rotate_left and tree_rotate_right in the file bintree3.template.
  4. Examine the C++ program newfiletotree.cxx provided in /cs/cs2005/samples/hw5/. This program is much like that used in HW4, except that two new menu options are made available to perform left and right rotations on the binary tree constructed from the input file.
  5. Compile and link the various files for your project using the Makefile provided in /cs/cs2005/samples/hw5/Makefile. The resulting executable file will be named treerotate. Perform multiple sample runs to debug your implementation. Compare your program's behavior with that of the sample executable file provided in /cs/cs2005/samples/hw5/treerotate. Note that your submitted HW5 files must compile and link correctly under g++ on the CCC Unix machines when the Makefile in /cs/cs2005/samples/hw5/Makefile is used.
  6. The source code for your implementation in bintree3.template should be carefully commented. Provide additional documentation for your code in a file named README, following the WPI CS documentation standard. You do not need to include the actual source code in the documentation file, just the names of the appropriate source files. Include screen views of several sample runs of your program.

Analysis portion (place your solutions in the README file)

  1. Think hard about why the result of performing a left rotation on a binary search tree does not affect the inorder listing of the tree's contents. Construct a detailed explanation of this fact that is general enough to apply to all binary search trees, not just a few examples. Play devil's advocate and try to find holes in your reasoning. Once you're completely convinced that you have a solid argument (this may require sleeping on it), write it down in as clear a manner as possible. You may want to include some rough diagrams.

  2. Prove by induction that the tree constructed by the buildtree function given in the solution to HW4 is balanced. Here's an outline for the proof that you should fill in:
    1. Overall ordering of the input data arrays for the proof: group together all arrays that lead to trees of a certain height h. The number n of nodes will satisfy 2h <= n < 2h+1.
    2. The statement that is to be proven by induction: if the length n=highindex-lowindex+1 of the portion of the input array between positions lowindex and highindex satisfies 2h <= n < 2h+1, then the tree pointed to by the corresponding return value of buildtree is balanced.
    3. Base step: h=0. This means that the number of relevant positions n of the input array satisfies 1 <= n < 2, which doesn't leave many options for n. Argue that the resulting tree will be balanced in this case.
    4. Recursive step: h >= 1. Assume that the target statement shown above in italics is known to be true for all values of h less than or equal to some value h0. The objective is to show that the target statement then holds whenever the length n=highindex-lowindex+1 satisfies the condition corresponding to h=h0 + 1, namely 2h0+1 <= n < 2h0+2. So, assume that the input satisfies this condition and show that the resulting output tree is balanced.

  3. Extra credit: Analyze the average-case running time for binary search, following the ideas presented in class on Friday Dec. 3. You may assume that all array positions are equally likely to be accessed, and that all searches terminate successfully (i.e. that the target item is always present in the search array). Your answer should give the average running time as a function of n, the size of the array, in as simple a form as possible.


Use turnin to submit the following files (and no other files), fully addressing the items listed above, before 5 pm on Friday Dec. 10, 1999.
  1. newfiletotree.cxx
  2. link2.h
  3. link2.template
  4. bintree3.h
  5. bintree3.template
  6. Makefile