Question: In class we implemented a set using a binary search tree. Implement a map the same way by adding the following methods to TreeMap: V

In class we implemented a set using a binary search tree. Implement a map the same way by adding the following methods to TreeMap:

V add (K key, V value); boolean containsKey (K key); V get (K key);

Then compile and run TreeMaps.java. Your output should look like this:

Here's the group with each person's age {Ben=21, Fred=21, George=25, Harold=16, James=28, Jan=25, Jeff=24, Ken=27,

Paul=29, Sam=22, Tom=30} Is George in the group? yes Is Keith in the group? no How old is Harold? 16 How old is Lily? null

//Driver

public class TreeMaps

{

public static void main (String [] args)

{

TreeMap ages = new TreeMap< >();

String [] names = {"Ken", "Fred", "Sam", "Ben", "Harold", "Tom",

"James", "Paul", "Jeff", "George", "Jan"};

int [] numbers = {27, 21, 22, 21, 16, 30, 28, 29, 24, 25, 25};

for (int i = 0; i < names.length; i++)

{

ages.add(names[i], numbers[i]);

}

// add

System.out.println(" Here's the group with each person's age " +ages);

String result = ages.containsKey("George") ? "yes" : "no";

// contains

System.out.println("Is George in the group? " + result);

result = ages.containsKey("Keith") ? "yes" : "no";

System.out.println("Is Keith in the group? " + result);

// get

System.out.println("How old is Harold? " + ages.get("Harold"));

System.out.println("How old is Lily? " + ages.get("Lily"));

}

}

//TreeMap Class

public class TreeMap, V> implements Map

{

private class Node

{

private K key;

private V value;

private Node left;

private Node right;

public Node (K key, V value)

{

this.key = key;

this.value = value;

left = null;

right = null;

}

public String toString()

{

return "" + key + "=" + value;

}

}

private Node root;

private int size;

public TreeMap()

{

root = null;

size = 0;

}

public V add (K key, V value)

{

// to be implemented

return null;

}

public boolean containsKey (K key)

{

// to be implemented

return false;

}

public V get(K key)

{

// to be implemented

return null;

}

public V remove (K key)

{

return null;

}

public int size()

{

return size;

}

public String toString()

{

if (root == null)

{

return "{}";

}

String s = "{" + inorder(root, new StringBuilder());

return s.substring(0, s.length()-2) + "}";

}

private String inorder(Node n, StringBuilder s)

{

if (n.left != null)

inorder (n.left, s);

s.append(n + ", ");

if (n.right != null)

inorder(n.right, s);

return s.toString();

}

}

//Map Interface

public interface Map

{

V add (K key, V value);

V remove (K key);

V get (K key);

boolean containsKey (K key);

int size();

String toString();

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!