CS341 Project 2, Lazy Binary Search Trees solution

$29.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (7 votes)

Your assignment is to implement a lazy BST. You may start with the BST class from the textbook or design your own. Each option has advantages and disadvantages. A primary objective of this programming assignment is to have you use recursion. So, one component of grading will evaluate how elegantly you employ recursion to implement this data structure. (Yes, you are being graded on aesthetics!)

In order to implement a lazy BST efficiently, your data structure must be able to determine the size and height of a subtree in constant time. You must have data members for the height and size of a subtree in the class representing the root of a subtree of a lazy BST. The height and size data members must be updated whenever the height or size of that subtree changes. The update must not affect the asymptotic running time of insert, delete and search. These must still run in time proportional to the height of the tree.

Furthermore, your lazy BST implementation must be generic. In particular, it must work with any class that implements theComparable interface. The declaration of your LazyBST class must be:

package lazybst; public class LazyBST < T extends Comparable<? super T { … }

This declaration is similar to the one used in the textbook. You must not change the package name because doing so will make your program incompatible with the test programs used for grading and will result in deductions.

Here are the methods you must implement in your LazyBST class. (You will need to implement others for your own coding needs.)

An insert() method that adds an item to the LazyBST that has the following signature:

public void insert (T x) ;

The insert() method must run in time proportional to the height of the lazy BST. Your LazyBST must not allow duplicates. If the insert() method is invoked with a key value that already appears in the lazy BST, your insert()method should do nothing.
A remove method that finds and removes an item with the given key value. The remove method should return a reference to the item removed. If no such item is in the lazy BST, your remove method should return null. (Do not throw an exception.) The remove method must have the following signature:

public T remove (T x) ;

For full credit, your remove() method must run in time proportional to the height of the tree.

Note that the textbook’s implementation of the remove method is quite inefficient because in the case where the node to be removed has two children, it first finds the value of the smallest item in the right subtree and then, in a separate step, removes that smallest item from the right subtree. This is inefficient because it uses two traversals of the BST. Your implementation must find and remove the smallest item in the right subtree without traversing the BST a second time.
A find() method that returns the item that has the given key. The signature of the find() method should be:

public T find (T x) ;

If no such item exists, the find() method should return null. For full credit, your find() method must run in time proportional to the height of the tree.
A method named span() that returns the number of items in the lazy BST with keys within a specified range. I.e., span(x,y) is the number of items with key ≥ x and ≤ y. The signature of the span() method should be:

public int span (T x, T y) ;

For full credit, your span() method must run in time proportional to the height of the tree. Try to make good use of recursion in the computation of span(x,y).
A method named rebalance() that rebalances a subtree of the lazy BST as described above. The running time of rebalance() must be proportional to the subtree being rebalanced. Note that a proper implementation would require you the keep track of the size and height of the subtree. Read the description above.
A dump() method that prints out each item in the LazyBST in increasing order. Next to each item, the dump() method must also print out the item’s height and the size of the subtree rooted at the item.

public void dump () ;

Finally, your lazy BST class must be called LazyBST and must be in a package called lazybst ( NOT proj2 or anything else). Your LazyBST class must be accessible to a main program in a different package. You can check that your code compiles correctly with this sample main program: Proj2Test.java. This test program must be placed in a separate directory nameddriver (since it belongs to the driver package). The output of Proj2Test.java should look something like this: Proj2Test.txt.Your code must compile with Proj2Test.java without alteration.

Note: “without alteration” means you cannot change even one character of the file — so DO NOT change the package name and DO NOT change the import directives. If your program does not compile with Proj2Test.java on GL using ANT, you must change your program to make it compile. This is the whole point of providing a sample test program.