Binary Search Tree

The goal of this lab period is to build a Binary Search Tree.


Tasks

Testing

As always, as you are coding up this project create testing files for each class you create exercising the public methods implemented in this class.

MapSet

This week, we will be building a Binary Search Tree that maps a set of keys to specific values. For example, perhaps we might want to keep track of a list of student IDs and map the IDs to the student they represent. In this case, the keys would be the IDs and the Students themselves would be the values. Because our keys are inherently just Strings, we could use lexicographic ordering to sort them, and hence store them in a binary search tree for relatively fast lookup.

In particular for this project, we will be implementing the MapSet interface. Take a minute to peruse the file: you will notice an inner class called KeyValuePair: think of this as the specific pairing of the key to its asociated value. These KeyValuePairs will be the objects that are actually getting stored into the BST, which will be sorted by the keys within the pair.

KeyValuePairs are generic-based, so they can be used with any underyling data type. For example, in the above idea of mapping String IDs to Student objects, I might make a KeyValuePair by writing something like KeyValuePair<String,Student>.

BSTMap

This class will be a Binary Search Tree that implements the MapSet methods. In order to do so, we will need to have that our Keys be comparable so that we can sort them accordingly. However, we still want to use generics in creating our BSTMap; to do this in Java, we can make sure that whatever data type is passed in for the keys extends Comparator via the following:

public class BSTMap<K extends Comparable<K>, V> implements MapSet<K, V>

After declaring the class, we'll want an inner Node class, just as in our previous LinkedLists. However, this time we'll want to keep track of the following fields:

The BSTMap itself only needs to keep track of 2 fields:

The methods you need to create are simply the methods inherited from the MapSet interface.

When you are done, you can proceed to the project.