Question
Each entry in your phone directory will have just two entries: a name and an associated phone number. Your directory will be sorted by names.
Binary Search Tree implementation has two data items in each BSTNode: the Key value and the rest of the element E. Since in this example there are only two data items for each element, you can use the name as the Key and the phone number as the element E.
Your main program loop should prompt the user to type the name of one of these commands: add, delete, find, quit. When the command is read, do the following action for each
add: prompt the user for the name, and then the phone number, of a new entry to add. Insert a new node in the tree with this data.
delete: prompt the user for a name, and then attempt to find and remove a node matching that name from the tree. If that name is not in the tree, print a message such as "No match found".
find: prompt the user for a name, and then attempt to find and print the phone number matching that name. If that name is not in the tree, print a message such as "No match found".
quit: end the program.
For 5 percent bonus: When the program begins, it should read in data from a file named phonedata.txt and store this information in the tree. Each line will have a single name followed by a single phone number. The two will be separated by one or more spaces. There will be no other information in the file and you can assume the file is formatted error-free.
Extra: Check and make sure the file exists before attempting to read it; if it does not exist, gracefully continue starting with an empty tree instead of having the program crash or exit.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
import java.lang.Comparable;/** Binary Search Tree implementation for Dictionary ADT */
class BST<Key extends Comparable<? super Key>, E>
implements Dictionary<Key, E> {
private BSTNode<Key,E> root; // Root of the BST
int nodecount; // Number of nodes in the BST
/** Constructor */
BST() { root = null; nodecount = 0; }
/** Reinitialize tree */
public void clear() { root = null; nodecount = 0; }
/** Insert a record into the tree.
@param k Key value of the record.
@param e The record to insert. */
public void insert(Key k, E e) {
root = inserthelp(root, k, e);
nodecount++;
}
/** Remove a record from the tree.
@param k Key value of record to remove.
@return The record removed, null if there is none. */
public E remove(Key k) {
E temp = findhelp(root, k); // First find it
if (temp != null) {
root = removehelp(root, k); // Now remove it
nodecount--;
}
return temp;
}...