Assignment 1

advertisement
Assignment 2
Due 10-27-2002 by midnight
Weight: 5% of course grade
This assignment is going to give you more experience
using and writing algorithms associated with Binary
Search Trees, in addition to providing you with a
comparison of some of the data structures covered in
previous courses.
Part 1
Create a class named BinarySearchTreeImpl in the
cs312.impl package that extends the BinarySearchTree
class as defined below:
class BinaryTreeNode {
BinaryTreeNode left;
BinaryTreeNode right;
BinaryTreeNode parent;
Integer value;
}
abstract class BinarySearchTree {
BinaryTreeNode root;
public abstract void add(Integer i);
public abstract void remove(Integer i);
public abstract boolean exists(Integer i);
public abstract String inorder();
public abstract String preorder();
public abstract String postorder();
}
The method should be implemented to perform the
necessary functionality, keeping in mind that the Binary
Search Tree properties must be maintained AT ALL
Professor Jeffrey Miller
CS312 Assignment 2
1/4
TIMES. So, when an element is added to the tree, make
sure that it is added in the proper location. Of course,
there are many different positions in a binary search
tree to add an element, so that is completely up to you.
Create a test class to make sure that your
implementation of the BinarySearchTree is working
properly. I will have my own test cases, so make sure
that you test thoroughly.
Part 2
A binary search tree maintains the property that, at any
time, if we call inorder() on the tree, we should get a
sorted representation of our tree. This part of the
assignment is going to require you to explore whether
or not a binary search tree is the best data structure for
this property.
Create a class named DataStructureTest in the
cs312.impl package. This class is going to perform an
analysis on a Hashtable, a Vector (stored in sorted and
unsorted order), a Stack (stored in sorted and unsorted
order), a Queue (stored in sorted and unsorted order), a
Linked List (stored in sorted and unsorted order), and a
Binary Search Tree.
This class is going to determine the average time that it
takes to add an element to each one of the above data
structures and print out a sorted representation of the
data in the structure. To do this, use the
Professor Jeffrey Miller
CS312 Assignment 2
2/4
System.currentTimeMillis() method, which will return
to you the current time in milliseconds since January 1,
1970. This can be used to compare how long it takes to
add an element and print out the sorted representation
of each of the structures. You will have to figure out
how to find the average of all of the adds, since that is
the number that should be outputted (not the time it
takes to add only one element).
The numbers to store in each structure will be read
from the command line, though you will not know
before runtime how many numbers to read from the
command line.
The output should be a table in the following format:
Data Structure
-------------Hashtable
Vector (sorted)
Vector (unsorted)
Stack (sorted)
Stack (unsorted)
Queue (sorted)
Queue (unsorted)
LinkedList (sorted)
LinkedList (unsorted)
Binary Search Tree
Add
--xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
Print Sorted
-----------xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
Professor Jeffrey Miller
CS312 Assignment 2
3/4
Submission
To submit this assignment, FTP the files into your Unix
account, then type the following command from the
directory that contains the BinarySearchTreeImpl.java
and DataStructureTest.java files:
perl /home/jmiller6/cs312/turnin.pl 2 BinarySearchTreeImpl.java
DataStructureTest.java
The “2” denotes that this is assignment 2. The files will
automatically be placed in a directory named
cs312/impl.
Professor Jeffrey Miller
CS312 Assignment 2
4/4
Download