| CSC 201 | Grinnell College | Spring, 2005 |
| Data Representation, Memory Management, and Formal Methods | ||
This laboratory provides some experience using a binary search tree structure.
Program ~walker/c/trees/bst.c contains a program shell
for operations within a binary search tree. Although the shell contains
stubs for many operations, it implements four specific functions:
addNameIter inserts a name into the tree,
findRec searches a tree for a specified name,
height computes the height of a tree, and
print displays the names in the tree.
These operations illustrate three basic algorithmic approaches for tree processing.
addNameIter uses iteration to work downward node-by-node
through a tree. A variable treePtr starts at the root
of the tree and progresses left or right down the tree from one level to
the next, until it comes to a place where a new node may be added.
findRec uses recursion to work downward through a tree.
Following usual patterns of recursion, base cases handle the simplest cases
(in this case, a null tree or a tree where an item is found), and the
recursive step allows movement downward from one level to the next.
print performs a full, in-order traversal of a tree, using a
double recursion. Processing begins with a left subtree, then moves
to a current node, and then concludes with a right subtree. The base
case of the recursion is an empty tree, when no processing is needed.
In this lab and the next, you are asked to apply these approaches to fill
in the details for other operations that currently are shown as stubs in
program bst.c.
Copy program ~walker/c/trees/bst.c to your account,
review its structure, study the algorithms used for the completed
functions addNameIter, findRec, and print, and
run the program a few times to determine how it works.
Expand the stub for function printLeaves that prints only
the leaves within the tree. In this process, you should change only
function printLeaves. While you may add additional functions
as needed, you should not change any other existing functions. (I.e.,
if something is not broken, do not fix it!)
Expand the stub for function countNodes that counts the
number of nodes within the tree.
Expand the stub for function findIter that uses an iterative
algorithm to determine if a designated item is located in the tree. You
should pattern your iterative algorithm on the approach used in function
addNameIter.
Develop a collection of test data to test the new functions thoroughly. What types of cases should be considered? What supporting evidence is necessary to support a claim that the resulting program is correct?
Use these test data to test your programs thoroughly.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/201.sp05/lab-bst-intro.shtml
|
created September 27, 2001 last revised April 18, 2005 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |