## Introduction to Binary Search Trees

Goals: This laboratory provides some experience using a binary search tree structure.

Orientation: Program `~walker/c/trees/bst.c` contains a program shell for operations within a binary search tree. While the shell contains stubs for many operations, it implements three specific functions:

• `addNameIter` inserts a name into the tree,
• `findRec` searches a tree for a specified name, and
• `print` displays the names in the tree.

Approaches to Tree Processing: 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.

Details for This Lab: 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`.

1. 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.

2. 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!)

3. Expand the stub for function `countNodes` that counts the number of nodes within the tree.

4. 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`.

5. 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.

Work to turn in:

This document is available on the World Wide Web as

```     http://www.cs.grinnell.edu/~walker/courses/195.fa01/lab.bst-intro.html
```

 created September 27, 2001 last revised October 11, 2001 For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.