Assignment 12

CSIS 10B
Assignment 12 Binary Search Trees
Due: next Wed
In class Demos (0 points)
We develop a simple (non-generic) Binary Search Tree of String class:
1)
2)
3)
4)
5)
basic linking of TreeNodes
recursive methods: printing in order and in graphic form
iterative methods: add, contains, get
inorder, preorder and postorder traveral methods
returning an iterator of tree data
Assignment (10 points) Do either Basic or Advanced
Basic -- do all these exercises in lab12basic folder inside mod12 download
1) Complete the three traversal methods inOrder, preOrder and postOrder
2) We can make a method that prints out tree nodes in "level" order (starting at root and
descending one level at a time, reading left to right) using a queue of TreeNodes to store the
data we encounter, as this following algorithm from Wikepedia outlines:
levelOrder()
q = empty queue
q.enqueue(root)
while not q.empty do
node := q.dequeue()
visit(node)-- (print the data in the node)
if node.left ≠ null
q.enqueue(node.left)
if node.right ≠ null
q.enqueue(node.right)
Create a new method for the BST class called levelOrder that implements the above algorithm in
order to print out the elements of a tree in level order. Create a queue of TreeNodes using the
java.util.ConcurrentLinkedQueue class (remember: offer and poll instead of enqueue and
dequeue)
3) Make a clone method for your BST. This method should make and return a complete copy of the
original tree where every String data in every TreeNode has been duplicated (this is a "deep
copy"). The String class already has a clone method. The usual way to do this is with a recursive
pair of methods that implement a pre-order traversal. The clone method for BST can simply
create a new empty BST, then invoke the cloneAux method passing newBST and root as
parameters.
The cloneAux method will then visit the data in its subTree parameter and add a clone of that
data into the newBST, and invoke itself again on the left and right subTrees in preorder fashion.
Back in clone, after the recursive cloneAux method completes, the newBST is returned.
Test your method using the code provided.
4) Make a method to compute the height of a BST. This method counts the number of nodes on
the longest path from the root to any leaf node in the tree. This statistic is significant because
the amount of time it can take to find an item in the tree is related to the height of the tree.
int height()
// pre: none
// post: returns the height of the tree
You can compute the height of a tree using a postorder traversal, and the following recursive
definition of height:
heightAux(subTree) = 0
if subTree is null
1 + the larger of heightAux( subTree.left) and heightAux(subTree.right)
5) (1 point Extra Credit) Close BlueJ or Eclipse and make a backup of your folder in case the next
step messes up your code. Then in your original folder reopen BlueJ and convert your String
BST (and TreeNode) class to a fully generic class ( using <E extends Comparable<E>> ) for the
data type instead of String. You may wish to refer to the OrderedVector class in mod11 as a
guide. Test your new generic BST on a BST<String> and make sure you get the same results
from previous runs before the generic capability was added.
Then, In the student class provided (copy student from mod12 folder and put in lab12basic, redo
the compareTo method so it comares students by ID. Now write a small app that loads the
Students in StudentA.txt into a BST<Student> dbase and prints the whole database. Add some
code to then, a) prompt the user to enter an ID, b) create a Student query object and set its ID
field to the ID entered, c) retrieve the student record with the selected ID from your dbase.
Print out the complete student information.
Advanced:
Try your hand on the Gardener's Hex-a-Pawn tree based strategy game ( a simplified form of chess)
described in chapter 12 lab on page 313. You may begin with the files provided in lab12advanced.