CS 2005, B Term 1999
Data Structures and Programming Techniques
HW4 (due Dec. 3)
This page is located at http://www.cs.wpi.edu/~alvarez/CS2005/B99/HW4/
The goal of this assignment is for you to gain further experience
in binary search trees and recursive function calls. You will also
see examples of file access in C++.
A nearly complete program for this assignment is available in
/cs/cs2005/samples/hw4/filetotree.cxx. The finished program should
read a list of items from a file and construct a balanced binary
search tree containing the items. Once the binary search tree has
been constructed, the user should be allowed to examine its contents
in preorder, inorder, and postorder.
Note that there are specific rules for binary search trees regarding
the relative ordering between a node's contents and the contents of
the nodes in the left and right subtrees of that node (see section
10.5 of the textbook). One way of stating this requirement is that
an inorder display of a binary search tree's contents should produce
a list in increasing (non-decreasing) order. Also, saying that a
binary tree is balanced in this context means that for every
node of the tree, the difference between the size (number of
nodes) of the left subtree of the node and the size of the right
subtree of the node is at most one (in particular, it is not enough for
the tree to be height-balanced in the sense of AVL trees as described in
- Review the material on binary trees in chapter 10 of the textbook.
Pay particular attention to sections 10.4 and 10.5, which contain
descriptions of the improved binary tree toolkit and of the basic
concepts of binary search trees. You may also want to review the
material in chapter 9 on recursion.
See appendix F of Main and Savitch for the basics of file I/O.
- Examine the C++ program filetotree.cxx provided in /cs/cs2005/samples/hw4/
that requests a user-specified filename, reads a list of integers from the
file named by the user, places the integers in a dynamic array, sorts the
array contents in increasing order, constructs
a balanced binary search tree containing the integers,
and finally allows the user to view the tree's contents in
preorder, inorder, and postorder. This program is nearly complete,
except for the function buildtree that constructs the balanced binary
search tree and the auxiliary function middleindex that computes the
position (index) halfway between two given positions.
- Once you've understood the overall structure of filetotree.cxx,
implement the missing functions buildtree and middleindex in
filetotree.cxx. Your implementation of buildtree should be recursive,
and it should use the improved binary tree toolkit described in section
10.4 which is available in /cs/cs2005/samples/hw4/. The template version
of the linked list toolkit is also available in that directory for
historical reasons having to do with the implementation.
The relevant files are bintree2.h, bintree2.template, link2.h, link2.template.
- Compile and link the various files for your project using the
Makefile provided in /cs/cs2005/samples/hw4/Makefile.
Perform multiple sample runs to debug your implementation.
A sample input file named itemfile is available in the directory
Note that your submitted HW4 files must compile and link
correctly under g++ on the CCC Unix machines when the Makefile
in /cs/cs2005/samples/hw4/Makefile is used.
The source code for your implementation in filetotree.cxx
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.
Use turnin to submit the following files (and no other files),
fully addressing the items listed above, before 8 pm on Friday
Dec. 3, 1999.