CS 2005, B Term 1999
Techniques of Programming
Lab 4 (Dec. 1)
Instructions
In this lab session you will implement the middleindex function
required in HW4, and you will make progress toward implementing
the recursive buildtree function for HW4.
- 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/lab4/. That directory contains copies
of the files required for HW4,
in particular a copy of the file named filetotree.cxx.
- 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.
Problems
-
Copy the files in /cs/cs2005/samples/lab4/ to one of your own
personal directories. Be careful to choose a directory other
than the one you're using for HW4, as otherwise you may overwrite
your HW4 files.
-
Examine the incomplete implementation file filetotree.cxx.
Most of the functions needed for HW4 have already been
implemented there, except for middleindex and buildtree.
Make sure you understand the overall outline of filetotree.cxx.
See HW4 for additional comments.
-
Fill in the implementation of the middleindex function in
filetotree.cxx. This can be done in a single line of C++ code.
You may use the fact that the midpoint between numbers x and y
is (x+y)/2. If x and y are integers, the result of this division
will be rounded down to an integer. In order to achieve
the desired rounding up effect, you may find it helpful
to use the modular remainder operator a % b which gives the
integer remainder in the integer division a/b; for example,
5%2 equals 1. Notice that rounding up is required precisely
when the sum of the lowindex and highindex arguments to middleindex
is odd, and this condition may be expressed in terms of the
modular remainder %2, so you just need to add the appropriate
modular remainder expression to the midpoint expression given above.
-
Fill in the outline for buildtree provided in filetotree.cxx
to obtain a complete implementation.
You will need to use the binary tree toolkit that is available
in the files bintree2.h and bintree2.template.
The buildtree function must recursively construct a balanced
binary search tree from the items stored in its data[] argument.
The basic binary tree toolkit function needed for this is
create_node. This function accepts three arguments: the entry
to be stored in the new node, and pointers to the trees that
will be the new node's left subtree and right subtree respectively.
Study the code for create_node in the binary tree toolkit files
for further details.
For each case in the buildtree outline, assign appropriate
values to the pointers left_ptr and right_ptr. You may find it
easiest to fill in the last case first.
Keep in mind that the final instruction in buildtree should
return a pointer to the root of the newly constructed tree,
which should have data[midindex] as the root entry, and
left_ptr and right_ptr as pointers to the appropriate
subtrees.
-
Type make at the Unix prompt in the directory to which
you copied the files provided in /cs/cs2005/samples/lab4/.
This compiles and links the appropriate files, and produces
an executable file named filetotree.
Run the executable file filetotree and use it to debug your
implementation. Modify your implementation as needed.
Note that the integers needed to fill the balanced binary search
tree constructed by filetotree must be placed in a file,
with blanks between consecutive integers; there should be
no spaces after the last integer.