Question: In this assignment, we will implement some Red - Black tree methods to store phone contacts. The Red - Black tree will use a person's
In this assignment, we will implement some RedBlack tree methods to store phone contacts. The RedBlack tree will use a person'sNameas theKeyand theContactas theValue
Here aContactwill have two fields:
aphone numberand
acountry
An implementation is provided for thatContactclass inRedBlackBSTTestjavaso that the program can use it to store the contact information in a RedBlack Tree.Next you will need to complete the methods that throwUnsupportedOperationExceptioninRedBlackBSTjava. You can use the implementations fromthe reference book's implementation.
The input will be from a file that has been already uploaded. As the output, the program will print the number of nodes in the tree and the contact details using an inorder traversal.
Directions: Fill in the contact method in the RedBlackBSTTest.java file and the RedBlackBST method in the RedBlackBST.java file.
RedBlackBSTTest.java
import java.ioFileNotFoundException;
import java.ioIOException;
import java.nio.file.Paths;
import java.util.Scanner;
public class RedBlackBSTTest
class Contact
implement this class
add necessary instance variables
and constructor and accessors
public static void mainString args
RedBlackBST redblackTree new RedBlackBST;
Scanner input null;
try
input new ScannerPathsgetexampletxt;
while inputhasNext
String country input.next;
String phone input.next;
String name input.next;
Contact contact new RedBlackBSTTestnew Contactcountry phone;
redblackTree.putname contact;
System.out.printfSize of the Red Black Tree:dn redblackTree.size;
System.out.printlnRed Black Tree keys inorder:";
for String s : redblackTree.keys
Contact contact redblackTree.gets;
System.out.printfs s sn s
contact.getCountry
contact.getPhone;
catch FileNotFoundException e
eprintStackTrace;
catch IOException e
eprintStackTrace;
input.close;
RedBlackBST.java
import java.util.NoSuchElementException;
public class RedBlackBST, Value
private static final boolean RED true;
private static final boolean BLACK false;
private Node root; root of the BST
BST helper node data type
private class Node
private Key key; key
private Value value; associated data
private Node left, right; links to left and right subtree
private boolean color; color of parent link
private int size; subtree count
public NodeKey key, Value value, boolean color, int size
this.key key;
this.value value;
this.color color;
this.size size;
initialize the empty tree
public RedBlackBST
Node helper methods
is the node red? false if the node is null.
private boolean isRedNode node
throw new UnsupportedOperationExceptionMethod not implemented yet.";
number of node is subtree rooted at node;
if node is null
private int sizeNode node
throw new UnsupportedOperationExceptionMethod not implemented yet.";
returns the number of keyvalue pairs in the tree
public int size
return sizeroot;
is the tree empty?
public boolean isEmpty
throw new UnsupportedOperationExceptionMethod not implemented yet.";
Standard BST search
returns the value associated with the given key.
public Value getKey key
ifkey null
throw new IllegalArgumentExceptionKey cannot be null.";
return getroot key;
Value associated with the given key in subtree rooted
at node; null if no such key
private Value getNode node, Key key
throw new UnsupportedOperationExceptionMethod not implemented yet.";
Does this tree contains the given key?
public boolean containsKey key
return getkey null;
RedBlack tree insertion
Inserts the specified keyvalue pair into the tree.
overwrites the old value with the new value if the tree
already contains the key.
Deletes the specified key from the tree if the value is null.
public void putKey key, Value value
throw new UnsupportedOperationExceptionMethod not implemented yet.";
insert the keyvalue pair in the subtree rooted at node
private Node putNode node, Key key, Value value
throw new UnsupportedOperationExceptionMethod not implemented yet.";
RedBlack tree deletion
Removes the smallest key and associated value from the tree
public void deleteMin
throw new UnsupportedOperationExceptionMethod not implemented yet.";
delete the keyvalue pair with the minimum key rooted at node
private Node deleteMinNode node
throw new UnsupportedOperationExceptionMethod not impleme
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
