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.)
- Sign in with the TA. You should both print your name
and sign the sheet.
- Listen to the TA's mini-lecture.
- 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.
- 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.
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.
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
Recall that the steps involved in performing a left rotation
on the subtree with root oldroot are the following:
- newroot = right(oldroot)
- hold = left(newroot)
- left(newroot) = oldroot
- right(oldroot) = hold;
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.
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.
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
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!