Part I: TreeSet, TreeMap, and HashMap Basics
Note: These exercises are outlined in the PowerPoint notes in the link above.
Exercise 1
Use a TreeSet to construct a binary search tree.
Instantiate an empty binary search tree object named bst
Insert the following nodes: "b", "q", "t", "d", "a".
The first node, "b", will be at the root of the tree.
Display each node in bst
TIP:
Use the toArray() method to create an array all the nodes.
Question: What type of traversal was used in toArray()?
Exercise 2
Part a:
Build the FullName class. Implement it as a Comparable interface.
Comparable is an interface that imposes an ordering on objects
of the class that implements it. This ordering is a natural ordering.
The class's compareTo() method is referred to as its natural
comparison method. This method will compare this object with a
specified object for order and will return one of -1, 0, or 1.
-1: this object was less than the specified object.
0 : this object was equal to the specified object.
1: this object was greater than the specified object.
Part b:
Create a TreeSet object to store a tree of names.
Add four names to the TreeSet.
Part c:
Display the tree of names. What order of traversal was used in
accessing names.
Exercise 3
Part a:
Create a TreeMap to construct a PhoneBook containing residents firstname, lastname, and phone numbers.
Use the FullName class.
What is the key>
What is the value
Part b:
Create a TreeSet object to store a tree of names.
Use a loop to display a JOptionPane that asks for a
full name in the format: “firstName lastName”.
Create a lookup method that searches for and displays the
phone number for the input name.
TIPS:
Use the String method split() to parse the name.
-split() takes the delimiter as its argument and returns an array of Strings.
Use the TreeMap method get() to return the phone number of
the entered person.
-get() will return the value if the key is found and a null if the key cannot be found.
Exercise 4
Use a HashMap to construct the PhoneBook containing residents firstname,
lastname, and phone numbers.
Use the FullName class.
Add an equals() method to the FullName class.
The equals() method implements an equivalence relation between (non null)
object references.
The Object class already provides an implementation of the equals method.
This means you will need to add an Override.
Add a hashCode() method to the FullName class.
TIP: Use the pattern Hash Function: (sum of chars) % 4.
Part II: Practice HashMap
Exercise 1
Write a method mapPlay1 that modifies and returns the given map using the following guidelines:
Assume the keys "a". "b", and "c" may exist
If the key "a" has a value, set the key "b" to have that value,
and set the key "a" to have an empty value
Exercise 2
Write a method mapPlay2 that modifies and returns the given map using the following guidelines:
Assume the keys for this map are names of foods and their values are toppings.
For example "salad" may be the key and "dressing" is the value.
If the key "ice cream" is present, set its value to "cherry".
For all cases, set the key "bread" to have the value "butter".
Exercise 3
Given a map of food keys and topping values, modify and return the map as follows:
If the key "potato" has a value, reset its value so that it is the same as the value assigned to the key "fries".
If the key "salad" has a value, reset its value so that it is the same as the value assigned to the key "spinach".