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
.
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.
The following methods are added to the BinaryTreeInterface
:
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).
You must modify or implement the following classes.
BinaryNode
ClassWhile 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
ClassAdd 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
ClassThis 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:
Be sure to include the implementations of the methods in ComparableTreeInterface
.
BinarySearchTree
ClassThis 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.
public T getMax()
public T getMin()
ComparableBinaryTree
versions of these methods to tailor them to a BST (i.e. make them more efficient)public boolean isBST()
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:
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.
isFull()
and isBalanced()
in BinaryTree
.saveInorder
method for you. See the BinaryTree.java file for this implementation.
buildInorder
method is tricky. Think about it carefully before implementing it. As a hint, note that the recursive method you call should include a parameter that indicates the number of nodes to be stored in the current subtree. If you cannot get this method working, comment it out of the Assign5.java program and indicate this in your Assignment Information Sheet.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.