Question

In: Computer Science

2. Implement a main method that does the following: Use Java (a) Creates a binary search...

2. Implement a main method that does the following: Use Java

(a) Creates a binary search tree to hold sports scores as data for a sport of your own choice

(b) Adds between 5 and 8 scores to the tree using standard tree operations Try insert

(c) Choose one of the tree traversals, preorder or postorder, and use it to print out all elements of the tree

(d) Delete one score from the tree using standard tree operations (suppose the score was found to be illegitimate) (e) Use an iterator to get a list for an inorder traversal for the tree (so that you can process each element from the tree), and print out a message about the sports score, including the sport and other informative information you can come up wit

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.List;

public class BinarySearchTree { // can be made generic based on game type BinarySearchTree<? extends Game>

   public static void main(String[] args) {
       BinarySearchTree bst = new BinarySearchTree();

       bst.insertRecord(new Game("Cricket", 100, "India vs England"));
       bst.insertRecord(new Game("Cricket", 200, "India1 vs England1"));
       bst.insertRecord(new Game("Cricket", 99, "India2 vs England2"));

       bst.printTree();

       System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");

       List<Game> list = new ArrayList<>();

       bst.inorderPopulateList(list);

       for (Game game : list) {
           System.out.println(game);
       }

       System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");

       bst.deleteRecord(new Game("Cricket", 100, "India vs England"));

       bst.printTree();
   }

   private static class TreeNode {
       private Game game;
       private TreeNode left;
       private TreeNode right;

       public TreeNode(Game game) {
           this.game = game;
       }
   }

   TreeNode root;

   public TreeNode insertRecord(Game key) {
       return insertRecordUtil(this.root, key);
   }

   private TreeNode insertRecordUtil(TreeNode root, Game key) {

       if (root == null) {
           root = new TreeNode(key);
           this.root = root;
           return root;
       }

       if (key.getScore() < root.game.getScore())
           root.left = insertRecordUtil(root.left, key);
       else if (key.getScore() > root.game.getScore())
           root.right = insertRecordUtil(root.right, key);

       this.root = root;
       return root;
   }

   public TreeNode deleteRecord(Game key) {
       return deleteRecordUtil(root, key);
   }

   private TreeNode deleteRecordUtil(TreeNode root, Game key) {
       if (root == null) {
           return root;
       }

       if (key.getScore() < root.game.getScore()) {
           root.left = deleteRecordUtil(root.left, key);
       } else if (key.getScore() > root.game.getScore()) {
           root.right = deleteRecordUtil(root.right, key);
       } else {
           if (root.left == null)
               return root.right;
           else if (root.right == null)
               return root.left;

           root.game = minValue(root.right);

           root.right = deleteRecordUtil(root.right, root.game);
       }

       return root;
   }

   private Game minValue(TreeNode root) {
       Game minv = root.game;
       while (root.left != null) {
           minv = root.left.game;
           root = root.left;
       }
       return minv;
   }

   public void printTree() {
       printTree(this.root);
   }

   private void printTree(TreeNode root) {

       if (root == null) {
           return;
       }

       printTree(root.left);
       System.out.println(root.game.toString());
       printTree(root.right);
   }

   // Populates blank list by inorder traversal of bst
   public void inorderPopulateList(List<Game> list) {
       inorderPopulateListUtil(this.root, list);
   }

   private void inorderPopulateListUtil(TreeNode root, List<Game> list) {

       if (root == null) {
           return;
       }

       printTree(root.left);
       printTree(root.right);
       list.add(root.game);
   }

}

class Game { // can be abtract and derived classes like football, cricket etc can be used

   private String type;
   private Integer score;
   private String match; // A vs B

   // Add any attributes you can add

   public Game(String type, Integer score, String match) {
       this.type = type;
       this.match = match;
       this.score = score;
   }

   public String getType() {
       return type;
   }

   public void setType(String type) {
       this.type = type;
   }

   public Integer getScore() {
       return score;
   }

   public void setScore(Integer score) {
       this.score = score;
   }

   public String getMatch() {
       return match;
   }

   public void setMatch(String match) {
       this.match = match;
   }

   @Override
   public String toString() {

       return this.type + " Match : " + this.match + "With score " + this.score;
   }

}


Related Solutions

Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to binary search, except it tries to begin the search nearer to the location of the item. Instead of the using the middle value of the sorted array, interpolation search estimates the location of the target with respect to the first & last values in the array. The implementation is the same as binary search except that you should calculate the mid value...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order traversal, preorder traversal, and find. Please separate the code in the four parts and explain in detail what is happening. Also, if you can please basic C language. If not, then I understand. Thank you for your time. The test cases are 'm', 'd', 'g', 'r', 'p', 'b', and 'x'. Output: Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i Enter...
Java programming Create a application with a method void (after main() ) that creates an array...
Java programming Create a application with a method void (after main() ) that creates an array and asks for the user fill it with float numbers. This array have infinite elements until the user decide that it is enough and input a command to stop. Display this array's elements data in another method void. Thanks for your help!
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
Problem 1: Write a Java program for traversing a Binary Search Tree in following ways: (use...
Problem 1: Write a Java program for traversing a Binary Search Tree in following ways: (use any data structure, library functions) ---------- 20 points i) Depth First Traversal: Inorder, LVR ii) Depth First Traversal: Inorder, RVL iii) Depth First Traversal: Preorder, VLR iv) Depth First Traversal: Preorder, VRL v) Depth First Traversal: Postorder, LRV vi) Depth First Traversal: Postorder, RLV No choice menu required. Sample Input (taken from keyboard, single space separated nodes as found in a complete BFS: top-down,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT