Question: You are to create a Program that simulates a Database with Golfer information that is stored in a Binary Search Tree. Summary of program: Your
You are to create a Program that simulates a Database with Golfer information that is stored in a Binary Search Tree.
Summary of program:
Your program will start with reading an input file filled with golfer information. Each line of this file will contain the needed information to create a golfer object. This file will be given to you. As each line of the file is read, you will create a Golfer object and place it in the Binary Search Tree. Then a menu will be displayed giving you choices to display golfer information, update, remove, or add new golfer. When quit is chosen the contents of your binary search tree will be traversed and output to update the same datafile. (You will be writing over the original data) You may want to create a file of a different name for testing purposes, so that you do not corrupt the original file during debugging.
Golfer class:
The Golfer class will contain the following private data variables:
-lastname
-numberOfRounds
-averageScore (double)
-handicap (positive integer value between 0- 20)
Must also contain the following methods:
-constructor (maybe two)
-accessors for each of the four variables
-mutators for each of the four variables
-method to add a new score (updates numberOfRounds and averageScore)
-compareTo method to help sort (class will implement Comparable)
-toString that will convert to one string all of the stored info
You may wish to add other methods to the Golfer class for your own use. I am keeping it simple for this project, but someone could add information on calculating handicap values, or keep track of other statistics such as number of putts or what is par for the course.
To create the Binary Search Tree you will be using the BTNode
In the TreeBag class you only need to implement methods that you will be using for this project. You are not required to implement the rest of the methods in the class. You may leave them as program stubs and ignore. Make sure all references to Golfer objects are generic ones. I have written Golfer Object in as the parameters so that you better understand what each method is doing, but in the actual Treebag class they should be listed as E. Among the methods that you will need to create are:
public void add (Golfer Object)
You will be adding golfers in the tree to locations based on the golfers lastname, but there is method in the Golfer class that you have created already called compareTo. Therefore you should use the compareTo method to compare this new golfer to golfers already within the tree.
public GolferObject retrieve (Golfer Object).
The retrieve method will search the Binary Search tree for the element, the method will return the object that was found. If the object is not found the method returns null. You will need to create a Golfer object with only the lastname, in order to do the search. I created an extra constructor for this task. When compareTo gives a zero, you know that you have found the correct Node.
public boolean remove (Golfer Object)
The remove method returns boolean therefore you need first find if the object is in the tree, if not return false, if yes, then delete the node from the tree and return true. Make careful sure that after Golfer removal you still have a valid binary search tree. (Handle all cases)
public void display()
Display the golfers in alphabetical order based on the lastname (think of the appropriate traversal through the tree to get this)
You may want to create other methods that you will be using in the TreeBag class.
You are to make use of the following class that we have discussed without making any changes. These classes are stored in the project folder.
BTNode
You will create these classes to finish the project:
Golfer (explained above)
TreeBag (template given)
GolferScoresTree- main driver of program.
The main driver was described above and contains the main menu which must contain the following choices:
Menu choices:
Display listing to screen of all golfers stats(ordered by lastname)
Display the golfers in current tree format(Use displayAsTree )
Find and display one individual golfers stats
Update an individual golfers stats(by adding an additional score)
Remove a golfer from the Database
Add a new golfer to the Database
Quit and update datafile
The Datafile used is data stored in text format (not binary, so that we can read it) with one golfer on each line and spaces between fields. It is organized like this:
lastname numberOfRounds handicap average
The sample data file given is called golferinfo.txt and will be used as a starting point to test your program. A copy of this file is given in with these program directions.
Tips for good grades:
Make sure you use comments where needed and use variable names that make sense, some of your grade will depend on program style as well as the use of your program.
Update the comments in the class file, to include your names and any new information
You will lose points for things like not indenting, or naming variables in non-descriptive ways. Do no leave in debugging code, or commented out code.
I use jGrasp and the java version that is in the lab computers. So make sure that your programs work with this.
Test your own projects thoroughly before you hand them in.
Late projects will not be accepted so plan ahead.
The four classes you are using for this project should be in separate files. Name them GolferScoresTree.java, Golfer.java , TreeBag.java and BTNode.java. If you do not name these files correctly, you will lose points. Use Javadoc to create the documentation for your TreeBag class.
golfer.inf.txt
Stevens 7 8 78.45 Walker 5 14 84.88 Smith 2 9 81.50 Jones 8 4 76.90 Addison 6 18 89.55 Norman 12 3 76.40 Woods 10 6 80.75 Palmer 7 7 77.77 Nickels 1 13 84.00 Rubble 5 17 91.25 Flintstone 5 15 89.85 Franks 8 10 79.35
// File: BTNode.java // Complete documentation is available from the BTNode link in: // http://www.cs.colorado.edu/~main/docs/ /****************************************************************************** * ABTNode<provides a node for a binary tree. Each node * contains a piece of data (which is a reference to an E object) and references * to a left and right child. The references to children may be null to indicate * that there is no child. The reference stored in a node can also be null. * * Limitations: * Beyond
Int.MAX_VALUE elements,treeSize, is * wrong. * *Java Source Code for this class: * * http://www.cs.colorado.edu/~main/edu/colorado/nodes/BTNode.java * * @author Michael Main * (main@colorado.edu) * * @version * Jul 22, 2005 ******************************************************************************/ public class BTNode { // Invariant of the BTNode class: // 1. Each node has one reference to an E Object, stored in the instance // variable data. // 2. The instance variables left and right are references to the node's // left and right children. private E data; private BTNodeleft, right; /** * Initialize a BTNode with a specified initial data and links * children. Note that a child link may be the null reference, * which indicates that the new node does not have that child. * @param <CODE>initialData * the initial data of this new node * @param <CODE>initialLeft * a reference to the left child of this new node--this reference may be null * to indicate that there is no node after this new node. * @param <CODE>initialRight * a reference to the right child of this new node--this reference may be null * to indicate that there is no node after this new node. *Postcondition: * This node contains the specified data and links to its children. **/ public BTNode(E initialData, BTNode initialLeft, BTNode initialRight) { data = initialData; left = initialLeft; right = initialRight; } /** * Accessor method to get the data from this node. * @param - none * @return * the data from this node **/ public E getData( ) { return data; } /** * Accessor method to get a reference to the left child of this node. * @param - none * @return * a reference to the left child of this node (or the null reference if there * is no left child) **/ public BTNode getLeft( ) { return left; } /** * Accessor method to get the data from the leftmost node of the tree below * this node. * @param - none * @return * the data from the deepest node that can be reached from this node by * following left links. **/ public E getLeftmostData( ) { if (left == null) return data; else return left.getLeftmostData( ); } /** * Accessor method to get a reference to the right child of this node. * @param - none * @return * a reference to the right child of this node (or the null reference if there * is no right child) **/ public BTNode getRight( ) { return right; } /** * Accessor method to get the data from the rightmost node of the tree below * this node. * @param - none * @return * the data from the deepest node that can be reached from this node by * following right links. **/ public E getRightmostData( ) { if (right == null) return data; else return right.getRightmostData( ); } /** * Uses an inorder traversal to print the data from each node at or below * this node of the binary tree. * @param - none * Postcondition: * The data of this node and all its descendants have been writeen by * System.out.println( ) using an inorder traversal. **/ public void inorderPrint( ) { if (left != null) left.inorderPrint( ); System.out.println(data); if (right != null) right.inorderPrint( ); } /** * Accessor method to determine whether a node is a leaf. * @param - none * @return *true (if this node is a leaf) or *false (if this node is not a leaf. **/ public boolean isLeaf( ) { return (left == null) && (right == null); } /** * Uses a preorder traversal to print the data from each node at or below * this node of the binary tree. * @param - none *Postcondition: * The data of this node and all its descendants have been writeen by * System.out.println( ) using a preorder traversal. **/ public void preorderPrint( ) { System.out.println(data); if (left != null) left.preorderPrint( ); if (right != null) right.preorderPrint( ); } /** * Uses a postorder traversal to print the data from each node at or below * this node of the binary tree. * @param - none *Postcondition: * The data of this node and all its descendants have been writeen by * System.out.println( ) using a postorder traversal. **/ public void postorderPrint( ) { if (left != null) left.postorderPrint( ); if (right != null) right.postorderPrint( ); System.out.println(data); } /** * Uses an inorder traversal to print the data from each node at or below * this node of the binary tree, with indentations to indicate the depth * of each node. * @param <CODE>depth * the depth of this node (with 0 for root, 1 for the root's * children, and so on)( *Precondition: * depth is the depth of this node. *Postcondition: * The data of this node and all its descendants have been writeen by * System.out.println( ) using an inorder traversal. * The indentation of each line of data is four times its depth in the * tree. A dash "--" is printed at any place where a child has no * sibling. **/ public void print(int depth) { int i; // Print the indentation and the data from the current node: for (i = 1; i <= depth; i++) System.out.print(" "); System.out.println(data); // Print the left subtree (or a dash if there is a right child and no left child) if (left != null) left.print(depth+1); else if (right != null) { for (i = 1; i <= depth+1; i++) System.out.print(" "); System.out.println("--"); } // Print the right subtree (or a dash if there is a left child and no left child) if (right != null) right.print(depth+1); else if (left != null) { for (i = 1; i <= depth+1; i++) System.out.print(" "); System.out.println("--"); } } /** * Remove the leftmost most node of the tree with this node as its root. * @param - none *Postcondition: * The tree starting at this node has had its leftmost node removed (i.e., * the deepest node that can be reached by following left links). The * return value is a reference to the root of the new (smaller) tree. * This return value could be null if the original tree had only one * node (since that one node has now been removed). **/ public BTNode removeLeftmost( ) { if (left == null) return right; else { left = left.removeLeftmost( ); return this; } } /** * Remove the rightmost most node of the tree with this node as its root. * @param - none * Postcondition: * The tree starting at this node has had its rightmost node removed (i.e., * the deepest node that can be reached by following right links). The * return value is a reference to the root of the new (smaller) tree. * This return value could be null if the original tree had only one * node (since that one node has now been removed). **/ public BTNode removeRightmost( ) { if (right == null) return left; else { right = right.removeRightmost( ); return this; } } /** * Modification method to set the data in this node. * @param <CODE>newData * the new data to place in this node * Postcondition: * The data of this node has been set to newData. **/ public void setData(E newData) { data = newData; } /** * Modification method to set the link to the left child of this node. * @param <CODE>newLeft * a reference to the node that should appear as the left child of this node * (or the null reference if there is no left child for this node) *Postcondition: * The link to the left child of this node has been set to newLeft. * Any other node (that used to be the left child) is no longer connected to * this node. **/ public void setLeft(BTNodenewLeft) { left = newLeft; } /** * Modification method to set the link to the right child of this node. * @param <CODE>newLeft * a reference to the node that should appear as the right child of this node * (or the null reference if there is no right child for this node) * Postcondition: * The link to the right child of this node has been set to newRight. * Any other node (that used to be the right child) is no longer connected to * this node. **/ public void setRight(BTNodenewRight) { right = newRight; } /** * Copy a binary tree. * @param <CODE>source * a reference to the root of a binary tree that will be copied (which may be * an empty tree where source is null) * @return * The method has made a copy of the binary tree starting at *source. The return value is a reference to the root of the copy. * @exception OutOfMemoryError * Indicates that there is insufficient memory for the new tree. **/ public staticBTNode treeCopy(BTNode source) { BTNode leftCopy, rightCopy; if (source == null) return null; else { leftCopy = treeCopy(source.left); rightCopy = treeCopy(source.right); return new BTNode (source.data, leftCopy, rightCopy); } } /** * Count the number of nodes in a binary tree. * @param <CODE>root * a reference to the root of a binary tree (which may be * an empty tree where source is null) * @return * the number of nodes in the binary tree *Note: * A wrong answer occurs for trees larger than * INT.MAX_VALUE. **/ public staticint treeSize(BTNode root) { if (root == null) return 0; else return 1 + treeSize(root.left) + treeSize(root.right); } }
// File: TreeBag.java // The implementation of most methods in this file is left as a student // exercise from Section 9.5 of "Data Structures and Other Objects Using Java" /****************************************************************************** * This class is a homework assignment; * AnTreeBag is a collection of int numbers. * *Limitations: * Beyond
Integer.MAX_VALUE elements,countOccurrences, * andsize are wrong. * *Note: * This file contains only blank implementations ("stubs") * because this is a Programming Project for my students. * * @version * Jan 24, 2016 ******************************************************************************/ public class TreeBag extends Comparable> implements Cloneable { // The Term E extends Comparable is letting the compiler know that any type // used to instantiate E must implement Comparable. i. e. that means that whatever // type E is must have a compareTo method so that elements can be compared against one another // This is required becuase we are doing comparisons in our methods // Invariant of the TreeBag class: // 1. The elements in the bag are stored in a binary search tree. // 2. The instance variable root is a reference to the root of the // binary search tree (or null for an empty tree). private BTNode root; /** * Insert a new element into this bag. * @param <CODE>element * the new element that is being inserted * Postcondition: * A new copy of the element has been added to this bag. * @exception OutOfMemoryError * Indicates insufficient memory a new BTNode. **/ public void add(E element) { // Implemented by student. } /** * Retrieve location of a specified element from this bag. * @param <CODE>target * the element to locate in the bag * @return * the return value is a reference to the found element in the tree * Postcondition: * If target was found in the bag, then method returns * a reference to a comparable element. If the target was not found then * the method returns null. * The bag remains unchanged. **/ public E retrieve(E target) { // Student will replace this return statement with their own code: return target; } /** * Remove one copy of a specified element from this bag. * @param <CODE>target * the element to remove from the bag *Postcondition: * If target was found in the bag, then one copy of *target has been removed and the method returns true. * Otherwise the bag remains unchanged and the method returns false. **/ public boolean remove(E target) { // Student will replace this return statement with their own code: return false; } /** * Displays the entire tree of Node elements in a order specified * by the elements compareTo method * * @param * none *Postcondition: * Outputs all elements in the tree to Screen. * Does not change the structure **/ public void display() { // Student will replace this with their own code: } /** * Displays the entire tree of Node elements using the * built in print method of BTNode * which displays the entire tree in tree format * * @param * none * Postcondition: * Outputs all elements in the tree to Screen. * Does not change the structure **/ public void displayAsTree() { root.print(0); } /** * Generate a copy of this bag. * @param - none * @return * The return value is a copy of this bag. Subsequent changes to the * copy will not affect the original, nor vice versa. Note that the return * value must be type cast to an TreeBag before it can be used. * @exception OutOfMemoryError * Indicates insufficient memory for creating the clone. **/ public TreeBagclone( ) { // Clone an IntTreeBag object. // Student will replace this return statement with their own code: return null; } /** * Accessor method to count the number of occurrences of a particular element * in this bag. * @param <CODE>target * the element that needs to be counted * @return * the number of times that target occurs in this bag **/ public int countOccurrences(E target) { // Student will replace this return statement with their own code: return 0; } /** * Determine the number of elements in this bag. * @param - none * @return * the number of elements in this bag **/ public int size( ) { return BTNode.treeSize(root); } /** * Add the contents of another bag to this bag. * @param <CODE>addend * a bag whose contents will be added to this bag *Precondition: * The parameter, addend, is not null. *Postcondition: * The elements from addend have been added to this bag. * @exception IllegalArgumentException * Indicates thataddend is null. * @exception OutOfMemoryError * Indicates insufficient memory to increase the size of the bag. **/ public void addAll(TreeBagaddend) { // Implemented by student. } /** * Create a new bag that contains all the elements from two other bags. * @param <CODE>b1 * the first of two bags * @param <CODE>b2 * the second of two bags * Precondition: * Neither b1 nor b2 is null. * @return * the union of b1 and b2 * @exception IllegalArgumentException * Indicates that one of the arguments is null. * @exception OutOfMemoryError * Indicates insufficient memory for the new bag. **/ public static TreeBag union(TreeBag b1, TreeBag b2) { // Student will replace this return statement with their own code: return null; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
