|CSC 153||Grinnell College||Spring, 2005|
|Computer Science Fundamentals|
This lab considers a binary search tree as a specific type of tree and provides practice with how such a tree structure might be implemented in Java.
Review the readings for this lab regarding the basic defintions of general trees and binary search trees.
Which of the following trees is(are) a binary search tree(s)?
The implementation of a binary search tree in Java follows a similar approach to our implementation of lists.
Each data element in a tree is considered an object of a TreeNode class. Program ~walker/java/examples/trees/TreeNode.java shows a typical declaration for such a node class.
A BSTree class combines the nodes into an overall structure. As with the school directory example in the reading on generalization, we consider the following basic operations:
Program ~walker/java/examples/trees/BSTree.java implements a binary search tree, including several methods already identified. This program also contains the same testing sequence used for the SchoolDirectory program involving lists.
This lab's readings discuss the approaches and implementations for each of these Java classes.
Copy ~walker/java/examples/trees/TreeNode.java and ~walker/java/examples/trees/BSTree.java to your account. Compile and run the programs to verify they produce the same results as the SchoolDirectory program described in the reading on generalization.
What can you say about the order of the entries printed by the print procedure? Explain why this sequence is obtained.
The discussion of insertion into a binary search tree in the reading for this lab described the insertion of the number 153 into the following tree (which repeats the tree given above).
Suppose a similar insert method was used to build the tree in the above example (with numbers 23, 37, 48, 96, 123, 185, 200, 285, and 309 rather than names and entries).
Explain your answers.
Given the order of insertions in the main method of ~walker/java/examples/trees/BSTree.java, draw a picture of the binary search tree that is produced by that program.
Add an update method to the BSTree class, analogous to the corresponding method for the SchoolDirectory program from the lab on generalization.
Note: As with the similar problem for the SchoolDirectory, the body of this update method can be just two lines long!
Write an iterative version of the recursive lookup method.
Use ideas from
Ideas from print also can be used to count the number of (non null) nodes in a tree. Use this approach to write a countNodes method.
The height of a tree is the maximum number of levels of nodes within the
tree; by convention, the height of a tree with only one node is 0. Thus,
the binary search tree shown between parts 4 and 5 above (with root 123)
has height 3. Add to the BSTree class a method
height which computes the height of a tree.
This document is available on the World Wide Web as
created April 18, 2000
last revised March 24, 2005
|For more information, please contact Henry M. Walker at firstname.lastname@example.org.|