CS 112
Homework 9
Notice different deadline: Monday April 18, by midnight.
Trees
Design, implement and test a Java class called BinaryTree which implements
an abstract data type for a binary tree, as started in class on Thursday and continued on Tuesday and next Thursday.
Extend it to a new class called BinSearchTree for binary search trees. Details below.
Notice the detailed timeline I am enforcing, to make sure that you get started in time, and get a part of the homework done before
each of the following two classes. In each class, a slightly more advanced technique will be explained, and you will have difficulty
understanding it if you have not worked on the previous part of the homework.
Final submission (changing permissions on the java files) is due on Tuesday April 19, before class.
Description
The basic node data structure was discussed in class, and consists in a data field and two links to the left and
right subtrees.
The tree class should allow insertion and deletion of leaves and subtrees, and navigation in the tree.
The class should
contain the following functions:
- Constructor: creates an empty tree
- Insert functions:
- InsertRoot: inserts a new node to be the root (with no children)
- InsertLeft: inserts a new leaf node as the left child of the
current node
- InsertRight: inserts a new leaf node as the right child of the
current node
- Navigation functions:
- MoveToRoot: moves current to root
- MoveLeft: moves current to left
- MoveRight: moves current to right
- Testing functions:
- EmptyTree: tests if the tree (starting at root) is empty
- EmptyLeft: tests if the left subtree of the current node is empty
- EmptyRight: tests if the right subtree of the current node is empty
- Delete functions
- DeleteRoot: deletes the whole tree, starting from root,
and deallocates the space
- DeleteLeft: deletes the left subtree of the current node
- DeleteRight: deletes the right subtree of the current node
- Print and Drawfunctions (far from trivial!!)
- Print: prints the whole tree, starting from root
- PrintCurrent: prints the subtree rooted at the current node
- Traverse functions (to be explained in class on Tuesday)
Extension
(to be done next week, on Tuesday and Thursday)
The Binary Search Tree class should also contain a function to
create a binary search tree starting from an array, to search for
a value in the tree and to sort.
You should come up with the best
solution you can for this implementation.
Then you should discuss your solution with me in
next Tuesday's lab to check that it is sound, before embarking
(next week) on
the implementation.
The final program should be a self contained applet, with an applet graphical interface.
Timeline
- Part 1, due by Tuesday April 12, before class: the basic binary tree class structure, with a menu-based testdriver (application)
for testing it. Even if not fully debugged, the class should be designed by now, so that you become aware of the difficulties
in designing the print method.
To be discussed in class on Tuesday: tree traversal. Recursive implementation of tree traversal algorithms.
- Part 2, due by Th April 14, before class. Binary tree fully functional, including (recursively implemented) tree traversal methods
and print method. Applet GUI designed. Basic binary tree drawing method designed, and aplied to a wired-in example.
- Part 3, due on Monday April 18, by midnight: Binary Search Tree. The class extends a binary tree with methods to insert an element
in the binary-search-tree fashion and to create a binary tree starting from an array. The testdriver will sort an array by
creating a binary tree from it, and then traversing the tree in the appropriate order. This part is based on material discussed in class
on Th April 14.
- Part 4, due on Monday April 18, by midnight: a binary search tree applet, visualizing a binary search tree after each insertion. The insertion
is controlled by a button, which for simplification inserts a set of wired-in elements (but you may add any additional features
if you want).
- Extra credit: a binary search tree applet which combines the GUI for sorting from an early homework, replacing the selection sort
algorithm with a sorting algorithm based on the binary search tree.
Grading guide
- Part 1 done in time: 25% of the grade.
- Part 2 done in time: 25% of the grade.
- Parts 3 and 4 done in time: 50% of the grade.
Ileana Streinu