(Java) - These are the files in package: BSTImpl.java: package sa.edu.yuc.bst; import java.util.Stack; public class BSTI
Posted: Fri May 20, 2022 12:33 pm
(Java)
- These are the files in package:
BSTImpl.java:
package sa.edu.yuc.bst;
import java.util.Stack;
public class BSTImpl> implements BSTInterface{
private Node root;
private int size;
//Do Not Change the code
public class Node {
T value;
Node leftChild;
Node rightChild;
public Node(T value) {
this.value = value;
}
}
//Do Not Change the code
public boolean isEmpty() {
return this.size == 0;
}
//Do Not Change the code
@Override
public void insert(T value) {
Node newNode = new Node(value);
if (isEmpty()) {
this.root = newNode;
this.size++;
} else {
Node currentNode = root;
while(currentNode != null) {
if (value.compareTo(currentNode.value) < 0) {
if (currentNode.leftChild == null) {
currentNode.leftChild = newNode;
this.size++;
}
currentNode = currentNode.leftChild;
} else if(value.compareTo(currentNode.value) > 0)
{
if (currentNode.rightChild == null) {
currentNode.rightChild = newNode;
this.size++;
}
currentNode = currentNode.rightChild;
}else {
break;
}
}
}
}
@Override
public T findMaxPercentage(){
// TODO ... Implement this method to return the Maximum
percentage of the student.
return null;
}
//Do No Change the code
@Override
public void printAll() {
if(isEmpty())
System.out.println("Tree is Empty");
else {
Stack> s = new Stack<>();
Node current = this.root;
while (!s.empty() || current != null){
if (current != null){
s.push(current);
current = current.leftChild;
}else {
current = s.pop();
System.out.print(current.value + "\n");
current = current.rightChild;
}
}
}
}
}
BSTInterface.java:
package sa.edu.yuc.bst;
public interface BSTInterface {
//Do Not Change the code
public abstract void insert(T item);
public abstract T findMaxPercentage();
public abstract void printAll();
}
BSTTest.java:
package sa.edu.yuc.bst;
public class BSTTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Do Not Change the code
BSTInterface bst = new BSTImpl();
bst.insert(new Student(200, "abc", 56.89));
bst.insert(new Student(89, "xyz", 69.89));
bst.insert(new Student(100, "def", 65.78));
bst.insert(new Student(210, "jkl", 89.89));
bst.insert(new Student(150, "mno", 98.76));
bst.insert(new Student(30, "pqr", 76.78));
bst.insert(new Student(90, "yts", 78.90));
bst.insert(new Student(190, "cab", 50.25));
bst.insert(new Student(175, "bac", 67.89));
System.out.println("\n Printing the Tree");
bst.printAll();
System.out.println("\n Maximum student percentage details
are\n " + bst.findMaxPercentage());
}
}
Student.java:
package sa.edu.yuc.bst;
public class Student implements Comparable{
private int id;
private String name;
private double percentage;
//Do Not Change the code
public Student(int id, String name, double percentage)
{
super();
this.id = id;
this.name = name;
this.percentage = percentage;
}
@Override
public int compareTo(Student s) {
// TODO Auto-generated method stub
if(this.id > s.id)
return 1;
else if(this.id < s.id)
return -1;
else
return 0;
}
//Do Not change the code
@Override
public String toString() {
return " id=" + id + ", name=" + name + ", percentage=" +
percentage;
}
}
1. For this question, you will use the package.edusa.bst" a. When you execute the BSTTest class, the following the output is generated, this binary search is constructed on Student id. Sample output is given based on student id. Printing the Tree id=30, name=pqi, percentage=76.78 id=89, name=xyz percentage=69.89 id=90, name=yts, percentage=78.9 id=100, name=def, percentage=65.78 id=150, name=mno, percentage=98.76 id=175, name=bac, percentage=67.89 id=190, name=cab, percentage=50.25 id=200, name=abe, percentage=56.89 id=210, name=jkl, percentage=89.89 What changes you have to make so that the binary search tree is constructed on student percentage Printing the Tree id=190, name=cab, percentage=50.25 id=200, name=abe, percentage=56.89 id=100, name=def, percentage=65.78 id=175, name=bac, percentage=67.89 id=89, name=xyz, percentage=69.89 id=30, name=Pqr, percentage=76.78 id=90, name=yts, percentage=78.9 id=210, name=jkl, percentage=89.89 id=150, name=mno, percentage=98.76 Page 2 of 4 aximum Data Structures _CS204_ _Tuesday, 17 May, 2022 b. Write the implementation of findMaxPercentage() method which returns the maxim percentage of student in the BSTImpl. Sample output is given based on student id. Printing the Tree id=190, name=cab, percentage=50.25 id=200, name=abc, percentage=56.89 id=100, name=def, percentage=65.78 id=175, name=bac, percentage=67.89 id=89, name=xyz, percentage=69.89 id=30, name=pqs, percentage=76.78 id=90, name=yts, percentage=78.9 id=210, name=jkl, percentage=89.89 id=150, namemno, percentage=98.76 Maximum student percentage details are id=150, name=mno, percentage=98.76
- These are the files in package:
BSTImpl.java:
package sa.edu.yuc.bst;
import java.util.Stack;
public class BSTImpl> implements BSTInterface{
private Node root;
private int size;
//Do Not Change the code
public class Node {
T value;
Node leftChild;
Node rightChild;
public Node(T value) {
this.value = value;
}
}
//Do Not Change the code
public boolean isEmpty() {
return this.size == 0;
}
//Do Not Change the code
@Override
public void insert(T value) {
Node newNode = new Node(value);
if (isEmpty()) {
this.root = newNode;
this.size++;
} else {
Node currentNode = root;
while(currentNode != null) {
if (value.compareTo(currentNode.value) < 0) {
if (currentNode.leftChild == null) {
currentNode.leftChild = newNode;
this.size++;
}
currentNode = currentNode.leftChild;
} else if(value.compareTo(currentNode.value) > 0)
{
if (currentNode.rightChild == null) {
currentNode.rightChild = newNode;
this.size++;
}
currentNode = currentNode.rightChild;
}else {
break;
}
}
}
}
@Override
public T findMaxPercentage(){
// TODO ... Implement this method to return the Maximum
percentage of the student.
return null;
}
//Do No Change the code
@Override
public void printAll() {
if(isEmpty())
System.out.println("Tree is Empty");
else {
Stack> s = new Stack<>();
Node current = this.root;
while (!s.empty() || current != null){
if (current != null){
s.push(current);
current = current.leftChild;
}else {
current = s.pop();
System.out.print(current.value + "\n");
current = current.rightChild;
}
}
}
}
}
BSTInterface.java:
package sa.edu.yuc.bst;
public interface BSTInterface {
//Do Not Change the code
public abstract void insert(T item);
public abstract T findMaxPercentage();
public abstract void printAll();
}
BSTTest.java:
package sa.edu.yuc.bst;
public class BSTTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Do Not Change the code
BSTInterface bst = new BSTImpl();
bst.insert(new Student(200, "abc", 56.89));
bst.insert(new Student(89, "xyz", 69.89));
bst.insert(new Student(100, "def", 65.78));
bst.insert(new Student(210, "jkl", 89.89));
bst.insert(new Student(150, "mno", 98.76));
bst.insert(new Student(30, "pqr", 76.78));
bst.insert(new Student(90, "yts", 78.90));
bst.insert(new Student(190, "cab", 50.25));
bst.insert(new Student(175, "bac", 67.89));
System.out.println("\n Printing the Tree");
bst.printAll();
System.out.println("\n Maximum student percentage details
are\n " + bst.findMaxPercentage());
}
}
Student.java:
package sa.edu.yuc.bst;
public class Student implements Comparable{
private int id;
private String name;
private double percentage;
//Do Not Change the code
public Student(int id, String name, double percentage)
{
super();
this.id = id;
this.name = name;
this.percentage = percentage;
}
@Override
public int compareTo(Student s) {
// TODO Auto-generated method stub
if(this.id > s.id)
return 1;
else if(this.id < s.id)
return -1;
else
return 0;
}
//Do Not change the code
@Override
public String toString() {
return " id=" + id + ", name=" + name + ", percentage=" +
percentage;
}
}
1. For this question, you will use the package.edusa.bst" a. When you execute the BSTTest class, the following the output is generated, this binary search is constructed on Student id. Sample output is given based on student id. Printing the Tree id=30, name=pqi, percentage=76.78 id=89, name=xyz percentage=69.89 id=90, name=yts, percentage=78.9 id=100, name=def, percentage=65.78 id=150, name=mno, percentage=98.76 id=175, name=bac, percentage=67.89 id=190, name=cab, percentage=50.25 id=200, name=abe, percentage=56.89 id=210, name=jkl, percentage=89.89 What changes you have to make so that the binary search tree is constructed on student percentage Printing the Tree id=190, name=cab, percentage=50.25 id=200, name=abe, percentage=56.89 id=100, name=def, percentage=65.78 id=175, name=bac, percentage=67.89 id=89, name=xyz, percentage=69.89 id=30, name=Pqr, percentage=76.78 id=90, name=yts, percentage=78.9 id=210, name=jkl, percentage=89.89 id=150, name=mno, percentage=98.76 Page 2 of 4 aximum Data Structures _CS204_ _Tuesday, 17 May, 2022 b. Write the implementation of findMaxPercentage() method which returns the maxim percentage of student in the BSTImpl. Sample output is given based on student id. Printing the Tree id=190, name=cab, percentage=50.25 id=200, name=abc, percentage=56.89 id=100, name=def, percentage=65.78 id=175, name=bac, percentage=67.89 id=89, name=xyz, percentage=69.89 id=30, name=pqs, percentage=76.78 id=90, name=yts, percentage=78.9 id=210, name=jkl, percentage=89.89 id=150, namemno, percentage=98.76 Maximum student percentage details are id=150, name=mno, percentage=98.76