Assignment 5: Comparable Binary Search Trees

We have been discussing binary trees (BTs) and binary search trees (BSTs) in detail, in terms of both functionality and implementation. In this assignment you will further your understanding of BTs and BSTs by writing your own version of the author's TreePackage, called MyTreePackage. Most of MyTreePackage will be identical to the author's TreePackage. However, you will be adding to the functionality of the package by modifying some of the classes / interfaces already there, and by adding a new class, ComparableBinaryTree.

Details

The textbook author has provided the implementation of a BT and BST through the BinaryNode, BinaryTree, and BinarySearchTree classes (plus numerous interfaces). However, there are some methods that could be useful that are not provided in the classes. It could also be useful to have a class that can store Comparable objects but does not require them to be organized as in a BST. In this assignment, you will modify the interfaces and classes as specified below, and you will also create a new class, ComparableBinaryTree, which will be a subclass of BinaryTree and the superclass of BinarySearchTree. You will test your resulting MyTreePackage using a driver program, Assign5.java, which will be provided for you. Your output should exactly match that shown in file Assign5-out.txt.

Interfaces

The following methods are added to the BinaryTreeInterface:

/* If the tree is a full tree, return true, otherwise return false. */
public boolean isFull();

/* Return true if:
 *     1) the difference in height between the left and right subtrees is at most k, and
 *     2) the left and right subtrees are both recursively k-balanced; return false otherwise
 */
publc boolean isBalanced(int k);

/* Save the data in the BT to the file "fileName" using an inorder traversal. Format the file
 * in the following way: It first contains an int representing the number of nodes in the tree,
 * followed by the actual objects from the tree (inorder). Use the writeObject() method to
 * write the objects using ObjectOutputStream.
 */
public void saveInorder(String fileName);

/* Build a balanced BT from the file "fileName". Assume the first line of the file has an
 * integer, N, indicating the number of values to follow. The remaining N lines of the file
 * contain N values. The order of the values in the file should be preserved by the tree (i.e.
 * an inorder traversal should show the data in the order stored in the file). Also if N = (2^K)-1
 * for some K, the tree MUST be a FULL tree. This method MUST build the tree recursively (hint:
 * have it call a recursive method). Use the readObject() method to read the objects using an
 * ObjectInputStream (so the file can contain any type of Serializable objects).
 */
public void buildInorder(String filename);

Create a new interface called ComparableTreeInterface. It must extend TreeInterface and add the methods below. This new interface must be generic, requiring that the generic type (T) be comparable with any object that can look like T (hint: use bounded generics).

/* If the tree is not empty, return the maximum value in the tree; otherwise return null */
public T getMax();

/* If the tree is not empty, return the minimum value in the tree; otherwise return null */
public T getMin();

/* Return true if the tree meets the recursive definition of a BST; else return false */
public boolean isBST();

Classes

You must modify or implement the following classes.

BinaryNode Class

While there is a version of this class available through the lecture notes, the version linked here has been modified slightly for this assignment; it has been made public so it can be accessed outside of the package. Add the isFull and isBalanced recursive methods as described above (even though this class won't be implementing the BinaryTreeInterface). Your code must be recursive, calculating your results by recursively traversing the tree.

BinaryTree Class

Add the implementations of the methods above, so that BinaryTree still correctly implements BinaryTreeInterface. Note that because isFull() and isBalanced() will be implemented in the BinaryNode class, they will be trivial to implement in BinaryTree. In your assignment information sheet, explain why.

While there is a version of this class available through the lecture notes, the version linked here provides an implementation of one of the methods as an example of how to implement the other methods. You may start with either version of the class, but the version linked in this assignment has been modified to allow for easier testing. The method setRootNode() has been changed from protected to public. (Note: This does not affect the real functionality of the class, and somewhat violates the principle of data hiding. However, it makes the test program a lot easier to devise, as you will note in Assign5.java.)

ComparableBinaryTree Class

This new class should be a subclass of BinaryTree. Its data will be Comparable but they will not be in any particular order. The class header will be:

public class ComparableBinaryTree<T extends Comparable<? super T>> extends BinaryTree<T> implements ComparableTreeInterface<T>

Be sure to include the implementations of the methods in ComparableTreeInterface.

BinarySearchTree Class

This class should now be a subclass of ComparableBinaryTree. It will have all of the functionality of the author's original BST, but will also override the methods below (from ComparableBinaryTree) as specified below.

Files

You will need a number of files for this assignment, many of which are provided for you. These are necessary because of the many interfaces and classes used in the overall implementations. These files are all linked below. Set up your program in the following way:

All of the files in StackAndQueuePackage.zip can be used as is. You do not need to modify any of these files.

Many files in the MyTreePackage.zip can be used as is. However, the following will need to be done to get the package to work:

Be sure to include the package statement shown below at the top of your MyTreePackage files:

package MyTreePackage;

Extra Credit

As an extra credit option, add a method to the BinaryNode class to draw it graphically and add a method to the BinaryTree class to draw the tree graphically. This is worth 10 points.

Final Notes & Hints

Submission and Grading:

Complete the Assignment Information Sheet.

Make sure you submit all source code files and your assignment information sheet in a single .zip file to get full credit. Save your main program in Assign5.java.

Submit your final zip file to CourseWeb in the Assignment 5 folder.

The grading rubric can be found here: Rubric (doc).

The assignment is due Friday, December 11 by 11:59 pm. As with all programming assignments, you have unlimited uploads (before the deadline), so you may upload the assignment before the deadline. If you later decide to upload another, you may do so without penalty (as long as it's before the assignment deadline). The last submission uploaded will be the one graded.

If you would like ungraded feedback on a programming assignment, you may send an email to your TA or the instructor and ask for feedback; please send your code as well. If your question is basically "Are there any problems with my program?" or "Can you check my code?" tell us what you've already done to test your program; provide the output from the test runs of your program.

For more advice on submitting your assignment, see the Programming Assignments section of the Tips for Success page.