Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

modify GolfApp2 by Writing a client method that returns a count of the number of

ID: 3820088 • Letter: M

Question

modify GolfApp2 by Writing a client method that returns a count of the number of nodes in a binary search tree that contain a value less than or equal to the argument value. The signature of the method is

int countLess(BinarySearchTree<Golfer> tree, Golfer maxValue)

//---------------------------------------------------------------------

// GolfApp2.java by Dale/Joyce/Weems Chapter 8

//

// Allows user to enter golfer name and score information.

// Displays information ordered by score.

//----------------------------------------------------------------------

import java.util.Scanner;

import ch08.trees.*;

import support.*; // Golfer

public class GolfApp2

{

public static void main(String[] args)

{

Scanner conIn = new Scanner(System.in);

String name; // golfer's name

int score; // golfer's score

  

BSTInterface<Golfer> golfers = new BinarySearchTree<Golfer>();

Golfer golfer;

int numGolfers;

  

String skip; // Used to skip rest of input line after reading integer

System.out.print("Golfer name (press Enter to end): ");

name = conIn.nextLine();

while (!name.equals(""))

{

System.out.print("Score: ");

score = conIn.nextInt();

skip = conIn.nextLine();

  

golfer = new Golfer(name, score);

golfers.add(golfer);

  

System.out.print("Golfer name (press Enter to end): ");

name = conIn.nextLine();

}

System.out.println();

System.out.println("The final results are");

numGolfers = golfers.reset(BinarySearchTree.INORDER);

for (int count = 1; count <= numGolfers; count++)

{

System.out.println(golfers.getNext(BinarySearchTree.INORDER));

}

}

}

Explanation / Answer

package chegg.bstree;

import java.util.Scanner;

public class GolfApp2 {

   public static void main(String[] args) {

       Scanner conIn = new Scanner(System.in);

       String name; // golfer's name
       int score; // golfer's score

       BSTInterface golfers = new BinarySearchTree();
       Golfer golfer;
       int numGolfers;

       String skip; // Used to skip rest of input line after reading integer

       System.out.print("Golfer name (press Enter to end): ");
       name = conIn.nextLine();
       while (!name.equals("")) {
           System.out.print("Score: ");
           score = conIn.nextInt();
           skip = conIn.nextLine();

           golfer = new Golfer(name, score);
           golfers.add(golfer);

           System.out.print("Golfer name (press Enter to end): ");
           name = conIn.nextLine();
       }
       System.out.println();
       System.out.println("The final results are");

       golfers.display();

       int golfersCount = golfers.countLess(30);

       System.out.println(" Less then Count ---- " + golfersCount);

       // numGolfers = golfers.reset(BinarySearchTree.INORDER);
       // for (int count = 1; count <= numGolfers; count++) {
       // System.out.println(golfers.getNext(BinarySearchTree.INORDER));
       // }
   }

   int countLess(BinarySearchTree tree, int maxValue) {
       return tree.countLess(maxValue);
   }
}

/*
* Java Program to Implement Binary Search Tree
*/

class BinarySearchTree implements BSTInterface<Golfer> {
   private Golfer root;

   /* Constructor */
   public BinarySearchTree() {
       root = null;
   }

   /* Functions to insert data */
   public void add(Golfer data) {
       if (data != null)
           root = add(root, data.getName(), data.getScore());
   }

   /* Function to insert data recursively */
   private Golfer add(Golfer node, String name, int score) {
       if (node == null)
           node = new Golfer(name, score);
       else {
           if (score < node.getScore())
               node.left = add(node.left, name, score);
           else
               node.right = add(node.right, name, score);
       }
       return node;
   }

   /* Functions to search for an element */
   public boolean contains(Golfer s) {
       return search(root, s.getScore());
   }

   /* Function to search for an element recursively */
   private boolean search(Golfer r, int val) {
       boolean found = false;
       while ((r != null) && !found) {
           int rval = r.getScore();
           if (val < rval - 1) {
               r = r.getLeft();
               found = search(r, val);
           } else if (val > r.getScore()) {
               r = r.getRight();
               found = search(r, val);
           } else if (val == rval) {
               found = true;
               break;
           } else {
               found = false;
               break;
           }
       }
       return found;
   }

   public void display() {
       display(root);
   }

   private void display(Golfer r) {
       if (r != null) {
           display(r.getLeft());
           System.out.print(r.toString() + " ");
           display(r.getRight());
       }
   }

   public int countLess(int maxScore) {
       return countLess(this.root, maxScore);
   }

   public int countLess(Golfer root, int maxScore) {
       Golfer golfer = null;
       Integer count = 0;
       if (maxScore == root.getScore() || maxScore > root.getScore()) {
           golfer = root;
           count = getLeafCount(golfer);
       } else {
           golfer = root;
           count = getLeafCount(golfer);
       }
       return count;
   }

   int getLeafCount(Golfer r) {
       int count = 0;
       while (r != null) {
           count++;
           r = r.getLeft();
       }
       return count;
   }

}

class Golfer {
   Golfer left, right;
   String name;
   int score;

   /* Constructor */
   public Golfer() {
       this.left = null;
       this.right = null;
       this.name = null;
   }

   /* Constructor */
   public Golfer(String n, int score) {
       this.left = null;
       this.right = null;
       this.name = n;
       this.score = score;
   }

   /* Function to set left node */
   public void setLeft(Golfer n) {
       left = n;
   }

   /* Function to set right node */
   public void setRight(Golfer n) {
       right = n;
   }

   /* Function to get left node */
   public Golfer getLeft() {
       return left;
   }

   /* Function to get right node */
   public Golfer getRight() {
       return right;
   }

   /* Function to set data to node */
   public void setName(String d) {
       name = d;
   }

   /* Function to get data from node */
   public String getName() {
       return name;
   }

   public int getScore() {
       return score;
   }

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

   @Override
   public String toString() {
       return "[" + name + "," + score + "]";
   }

}

interface BSTInterface<E> {
   public void add(E golfer);

   public boolean contains(E golfer);

   public void display();

   public int countLess(int score);
}

Output

-----------

Golfer name (press Enter to end): A
Score: 23
Golfer name (press Enter to end): B
Score: 34
Golfer name (press Enter to end): C
Score: 12
Golfer name (press Enter to end): D
Score: 9
Golfer name (press Enter to end): E
Score: 6
Golfer name (press Enter to end): F
Score: 2
Golfer name (press Enter to end): G
Score: 54
Golfer name (press Enter to end): H
Score: 76
Golfer name (press Enter to end):

The final results are
[F,2] [E,6] [D,9] [C,12] [A,23] [B,34] [G,54] [H,76] Less then Count ---- 5

Description
-----------------

Please run the main method avaiable in GolfApp2.java class .

Let me know if you face any difficulties to make understanding on above code