Problem Set 9: Binary Search Trees
Assigned: Wednesday November 16, 2016
Due: Friday December 9, 2016, midnight
Points: 10 Points up to 16 Points
This is an individual problem set. Choose any one of the following
three options.
Option 1: (10 Points up to 14 Points) Immutable BSTs
Sedgewick and Wayne's
Binary
Search Tree ADT implements mutable BSTs. SW BSTs have a
header node with a root field that may be null or may
point to a value of type Node. Insertions and deletions take
place by mutating fields in the tree structure.
...
public void put(Key key, Value val);
public void deleteMin();
public void deleteMax();
public void delete(Key key); // Hibbard Deletion
...
By altering the interface somewhat, we can specify immutable or
persistent BSTs in which alterations to a BST construct a new
BST. The following BST interface can be used as a
specification for immutable BSTs.
public interface BST<Key extends Comparable<Key>, Value> {
public boolean isEmpty();
public int size();
public boolean contains(Key key);
public Value get(Key key);
public BST<Key, Value> put(Key key, Value val);
public BST<Key, Value> deleteMin();
public BST<Key, Value> delete(Key key); // Hibbard Deletion
public Key min();
public String toString();
public Key floor(Key key);
public int height();
}
Immutable BSTs can be implemented along the lines that we used in
implementing the Immutable Map API in class. Remember that our
implementation had two classes, a Binding class implementing
nodes that paired a key with a value and an Empty class
representing an empty map.
public class Empty<Key, Value> implements Map<Key, Value> { ... }
public class Binding<Key, Value> implements Map<Key, Value> { ... }
Notes
- Like the SW implementation of mutable BSTs, your implementation should
throw a NoSuchElementException if a client attempts to delete
a key/value pair that doesn't exist in the BST.
- The toString function should return a simple string representation
of the tree. Consider using -- for empty trees and
(left, key, val, right) for non-empty trees.
- As usual, your classes should include main functions that
serve as a unit test for the class.
Extra Credit
- (1 Point) Implement the public Key select(int k); operation.
See SW for a description.
- (1 Point) Implement the public int rank(Key key); operation.
See SW for a description.
- (2 Point) Implement the public Iterable<Key> keys();
operation. See SW for a description.
Option 2: (16 Points) Implement the Immutable Red/Black tree ADT.
public interface RedBlack<Key extends Comparable<Key>, Value> {
public boolean isEmpty();
public int size();
public boolean contains(Key key);
public Value get(Key key);
public RedBlack<Key, Value> put(Key key, Value val);
public String toString();
}
Option 3: (10 Points) The Great-Tree List Recursion Problem
This is a tough, interesting problem from the Web Exercises of the
Sedgewick/Wayne website (Section 3.2). A fuller discussion of the
problem can be found
here. Note that the "fuller discussion" includes
a solution(!) so you'll have to be self-disciplined to avoid looking
at it. A binary search tree and a circular doubly linked list are
conceptually built from the same type of nodes - a data field and two
references to other nodes. Given a binary search tree, rearrange the
references so that it becomes a circular doubly-linked list (in sorted
order). Nick Parlante describes this as one of the neatest recursive
pointer problems ever devised. Hint: create a circularly linked list A
from the left subtree, a circularly linked list B from the right
subtree, and make the root a one node circularly linked list. Then
merge the three lists.
|