CS 2005, B Term 1999
Techniques of Programming
Lab 5 (Dec. 8)


In this lab session you will implement the left tree_rotate_left function required in HW5. You will also have the option of working on an implementation of the quicksort algorithm. If you'd rather work on quicksort, just skip the tree rotation portion of the lab. (However, if you haven't yet finished HW5, it is suggested that you do the tree rotation portion first.)

  1. Sign in with the TA. You should both print your name and sign the sheet.
  2. Listen to the TA's mini-lecture.
  3. Log onto your CCC Unix account and change the directory to /cs/cs2005/samples/lab5/. That directory contains copies of the files required for HW5, in particular a copy of the file named bintree3.template.
  4. Do the problems listed below. Feel free to ask the TA questions about the problems. Actively working on the lab assignment during the lab session is required. Don't worry if you can't finish the full assignment before the lab session is over. You can finish afterwards.


  1. Copy the files in /cs/cs2005/samples/lab5/ to one of your own personal directories. Be careful to choose a directory other than the one you're using for HW5, as otherwise you may overwrite your HW5 files.

  2. Study the specification of the functions tree _rotate_left and tree_rotate_right in the file bintree3.h. These functions correspond to the tree rotations described in class in connection with AVL (height-balanced) trees.

    Recall that the steps involved in performing a left rotation on the subtree with root oldroot are the following:

  3. Fill in the implementation of the tree_rotate_left function in bintee3.template so that the specification is satisfied. Remember that a pointer to the root of the rotated tree must be returned.

  4. Type make at the Unix prompt in the directory to which you copied the files provided in /cs/cs2005/samples/lab5/. This compiles and links the appropriate files, and produces an executable file named treerotate. Run the executable file treerotate and use it to debug your implementation. Modify your implementation as needed.

  5. The Quicksort Challenge

    Examine the file named quick.cxx in the directory /cs/cs2005/samples/lab5/. This file contains a test program for the quicksort function. A partial implementation of quicksort is also provided. However, the crucial partition function needed for quicksort has not been implemented. See the notes in quick.cxx and read the relevant parts of chapter 13 for information about the partition function. In particular, a pseudocode outline of the partition function appears on p. 601 of the book. Work on implementing the partition function and thus on obtaining a complete implementation of quicksort.

    Once you've implemented the partition function, modify the test program in quick.cxx so that it obtains its input from a file instead of the console, and so that it allows larger arrays (say thousands of items instead of just 10). Test quicksort on some sample files to see how its performance scales up to larger input sizes. If you want to be fancier, provide a function that generates samples input files automatically.

    This problem is challenging and you should not be disappointed if you can't completely solve it before the end of the lab session. Good luck!