In this program you will replace the linked list from Program 1 with a binary search tree as the basis for your address book. You will also save your address book in a text file. INFS 519 Homework #2
Spring 2019 Instructor: Hal Greenwald
Due April 2, 11:59 PM
In this program you will replace the linked list from Program 1 with a
binary search tree as the basis for your address book. You will also save
your address book in a text file.
Your program will appear the same as Program 1 to the user except that
it will first ask the user “Do you want to open a file? (y/n).” If the user
answers “y” the program will ask for a file name, open and read the
input file. Each pair of lines from the input file will be made an entry in
the address book: a name line and an address line. If the user answers
“n” the program will not open a file. In either case the program will
continue as did Program 1.
A new operation “save” will be added to the menu. When the user
selects “save” the program will ask the user for the name of a file in
which to write the address book, open the file and write the entire
address book to the file. This operation will be very similar to the
“displayAll()” operation.
Internals
Again, you will write (among other classes) a class Table which will
store entries comprised of (key/value) pairs of Strings. This class will
have the same public methods as did Program 1 with the addition of
a public void save() method and it will have none of the “mark” classes.
Table will now be implemented as a binary search tree. Each node will
have two String fields (for the name and address). insert, lookUp, delete,
and update will use binary search tree operations (which you will
write). You will use traversals to implement “displayAll()” and “save()”.
For “displayAll()” use an in-order traversal but for “save” use either a
pre- or post-order traversal.
To turn in
As before, you will turn in a well-commented source listing via
Blackboard.
INFS 519: Program Design and Data Structures
Spring 2019 Instructor: Hal Greenwald
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/** This class implements a binary search tree of key/value pairs of strings * */
public class Table {
/** Root node in the tree */
private Node root;
/**
* Inserts a new Node into the table. If the provided key already
* exists, no entry will be created. Otherwise, the new entry is
* added to the end of the list.
* @param key
* @param value
* @return True if the new node was inserted successfully.
*
False if the provided key already exists in the table.
*/
public boolean insert(String key, String value) {
return true;
}
/**
* Inserts a new node into a binary search tree.
* If the new node key is not unique, the new node will not be added.
* @param parent Root node of the tree
* @param newNode Node to add
* @return Root node of the altered tree
*/
private Node insertNode(Node parent, Node newNode) {
return parent;
}
/**
* Looks up the table entry with the provided key.
* @param key
* @return The value of the entry with the provided key. Null if
* no entry with the key can be found.
*/
public String lookUp(String key) {
return node.getValue();
}
/**
* Looks up the node in the binary search tree.
* @param parent Root node of the tree
* @param key Key of the node to search for
* @return The Node with the provided key.
* Null if no entry with the key can be found.
*/
private Node lookUpNode(Node parent, String key) {
}
/**
* Deletes the table entry with the given key.
* @param key
* @return True if the entry was successfully deleted. False if
* no entry with the given key was found.
*/
public boolean delete(String key) {
}
/**
* Deletes the node with the provided key from the given tree
* @param parent The root of the tree containing the node to delete
* @param key The key of the node to delete
* @return The root node of the altered tree.
*/
private Node deleteNode(Node parent, String key) {
}
/**
* Finds the largest node of the provided tree
* @param parent The root of the tree
* @return The largest node in the provided tree
*/
private Node findLargestNode(Node parent) {
}
/**
* Saves the table to a text file
* @param filename Name of the file to contain the table
*/
public void save(String filename) {
}
/**
* Writes a tree to a file using pre-order traversal
* (parent, left, right)
* @param writer Writer to the file
* @param node Root node of the tree to write
* @throws IOException
*/
private void writeNode(BufferedWriter writer, Node node) throws
IOException {
}
/**
* Displays all nodes in the table.
* @return The number of nodes in the table.
*/
public int displayAll() {
}
}
/**
* Displays all nodes in a (sub)tree using in-order traversal
* (left, parent, right)
* @param node The root node of the tree to display
* @return The number of nodes in the tree
*/
private int displayNode(Node node) {
}
INFS 519: Program Design and Data Structures
Spring 2019 Instructor: Hal Greenwald
/**
* This class is a single entry in a binary search tree.
* It stores a key/value pair of strings and pointers to store
* references to the left and right child Nodes in the tree.
*/
public class Node implements Comparable{
/* Node key and value*/
private String key, value;
/* Child Nodes in the tree */
private Node left, right;
/** Creates a new Node.
* @param key
* @param value
*/
public Node(String key, String value) {
}
/**
* @return The Node key
*/
public String getKey() {
}
/**
* @param key The Node key
*/
public void setKey(String key) {
}
/**
* @return The Node value
*/
public String getValue() {
}
/**
* @param value The Node value
*/
public void setValue(String value) {
}
/**
* @return The left child Node
*/
public Node getLeft() {
}
/**
* @param left The left child Node
*/
public void setLeft(Node left) {
}
/**
* @return The right child Node
*/
public Node getRight() {
}
/**
* @param right The right child Node
*/
public void setRight(Node right) {
}
@Override
public String toString() {
return String.format(“%s%n%s%n”, this.key, this.value);
}
@Override
public int compareTo(Node that) {
return this.key.compareTo(that.key);
}
}
HW2 Rubric
Posted on: Tuesday, March 26, 2019 11:56:18 PM EDT
HW2 Rubric:
10 points
One point (a piece) for the correct implementation of each of the following methods (total 6 points).
Two points for correct program output.
Two points for overall program appearance/readability/comments.
Feel free to use the sample data attached for testing: addresses.txt
• public boolean insert(String key, String value)
Inserts a new entry to the table. If an entry already exists with the
given key value make no insertion but return false.
• public String lookUp(String key)
Looks up the entry with the given key and returns the associated value. If
no entry is found null is returned.
public boolean delete(String key)
Deletes the entry with the given key. If no entry is found
returns false.
public boolean update(String key, String newValue)
Replaces the old value associated with the given key with the newValue
string.
public void save()
Ask the user for the name of a file in which to write the address book, open the
file and write the entire address book to the file.
• public int displayAll()
Displays Name/Address for each table entry. Returns total entry count.