Computer Science 245
Spring 2009
Homework 2:  Binary Search Trees
Due Wednesday, February 18th, 3:30 p.m.

Binary Search Trees


For your second homework, you will be adding some functionality to the binary search tree class located here: BinarySearchTree.java.   You will need to add 3 methods to this class (plus any helper methods that you need).  The methods that you need to add are:

Comparable ElementAt(int index)

Returns the ith largest elementin the binary search tree.   So, ElementAt(0) would return the smallest element in the tree, ElementAt(1) would return the second smallest element in the tree, and so forth.  This method does not need to be efficient, it only needs to compute the correct value.

int NumLess(Comparable elem)

Returns the number of elements in the tree less than elem.   Be sure to make your code as efficient as possible -- only examine the sections of the tree that you need to in order to compute the desired information.

void Balance()

Reorders the tree so that it is balanced -- that is, so the height is as small as possible.  The easiest way to rebalance the tree is to:
The file BinarySearchTree.java already has "stubs" for ElementAt, NumGreater, and Balance --  you just need to fill them in so that they do the correct thing.  (Note -- you will
probably also need to write some helper functions!). 

Submission

Please place BinarySearchTree.java into subversion under:

https://www.cs.usfca.edu/svn/$<$username$>$/cs245/Homework2/}

Also, turn in a printout of only the methods you added or modified (not the entire file BinarySearchTree.java).  To make your TA's life easier, please put your submission directory at the top of the code printout. Note that you will probably want to  add helper methods ...