CS 210
Intermediate Programming with Data Structures
Programming Assignment 6
Spring, 2006
Due Monday, May 8 at
Purpose
This assignment aims to help you:
Read Weiss Weiss Sections 19.1 -- 19.3.
Description
In this assignment you will work with the non-JDK ordered list interface, specified in OrderedList.java. An ordered list is a list whose elements can be ordered (implement Comparable) and that maintains the elements in a specified order (smallest to largest, or vice-versa). Because it cannot support List.set(), we should not say an OrderedList IS-A List. But we can say that OrderedList IS-A Collection, and extend it by get() and indexOf() from List. A BinarySearchTreeWithRank is an excellent way to implement an ordered list, since it supports an ordered collection with numerical position of each element, with efficient insertion.
1. Binary Search Trees. Design and implement class BSTOrderedList implements OrderedList starting from Weiss's BinarySearchTreeWithRank class and the JDK's AbstractCollection class. The basic setup using stubs is in BSTOrderedList.java. Stubs are implementations that don't do anything but do compile, thus checking the method headers. You can test with the supplied OrderedListTester and OrderedListDriver.
2. Sorting. A general strategy for sorting is to insert the n objects into a container that maintains order, such as OrderedList or TreeSet, and then iterate through them. Write TestSort that generates n Integers at random from 1 to 1000 and places them in two identical arrays. Then it times their sort by JDK sort (using one array) and also by inserting them (using the other copy of the array) into an OrderedList implemented by BSTOrderedList, and then iterating through them in order, placing them back in a third array. As a further test of your OrderedList implementation, check that both sort results are identical by looping through the first and third arrays. JDK sort should beat BST insert-sort but not by more than a factor of 10 I’m guessing. Report both times in ms and also the ratio of times (time-BST/time-JDK-sort). Usage: java SortTester n.
Design
For 1., you are starting from Weiss's BinarySearchTreeWithRank class, which extends BinarySearchTree and uses a nested BinaryNodeWithSize class, which extends BinaryNode. These classes are all available at Weiss’s website, except the needed ItemNotFoundException.java, but this is easily created. Remove the package spec at the top of each file.
Note that each node does not need to know (have a field for) its own index. The indexes are figured out dynamically from the size information in the nodes of the tree. This is a good thing, because if the index was stored in each node, it would have to be updated on insert in many nodes.
Remember that the OrderedList interface allows for multiple entries that compareTo() one another as equal. But the BST code disallows duplicate keys. To simplify, let us assume that all objects whose keys compare as equal are completely identical. Then all you need to do is hold a count of duplicates at each node along with the one copy of the object.. The size of a node then counts all the duplicates at or below it in the tree. Watch out for assumptions of 1 key at a node and change them to the duplicates-count.
You should find that add(), get(), contains() and iterator() are pretty straightforward. Note that iterator() can use the rank capability to keep track of where it is. remove() is tricky, as Weiss notes, but it's implemented for you except for duplicates count. We talked about this in class, focusing on what to do for the different types of nodes (leaf, parent with one child, parent with two children). Now you simply decrement the duplicates-count if it’s over 1, else do the old work.
The indexOf() method that returns an index given a key is also a little tricky. At the root the size tells us the range of all indices, [0, n-1], and in particular the lowest possible value, 0. The key tells us which child to follow, and that child’s size, say s, narrows the possible range to a smaller range, [0, s-1] (left child) or [n-s, n-1] (right child.) This range is further restricted by the next child down in the tree, and so on. You can track just the lower end of the range and get the right value.
For 2., you can use JDK class Date for millisecond-precision timings, since the construction of a default Date object captures the current time to this precision, and getTime() returns that value in a long. See TestTime.java.
memo.txt
In the file memo.txt, answer the following questions, in one
to three pages
(60-180 lines) of text. Use
complete sentences, as you would for an English class
Turn In
Use the turnin system to submit your files. Before the due date, submit the following files to the pa06 folder of your turnin system account:
memo.txt