Question

java A University would like to implement its students registery as a binary search tree, calledStudentBST....

java A University would like to implement its students registery as a binary search tree, calledStudentBST. Write an Student node class, called StudentNode, to hold the following information about an Student: - id (as a int) - gpa (as double) StudentNode should have constructors and methods (getters, setters, and toString()) to manage Write the StudentBST class, which is a binary search tree to hold objects of the class StudentNode. The key in each node is the id. :

import.java.ArrayList; public void addStudent(StudentNode sn){}

// adds a Student at the front of the list public ArrayList preorderTraversal(){}

//preorder traversal by returning an arraylist of visited student nodes public ArrayList inorderTraversal(){}

//inorder traversal by returning an array of visited student nodes public ArrayList postorderTraversal(){}

//postorder traversal by returning an array of visited student nodes public int gpaGreaterThan(double a);

// method that returns the number of StudentNode objects having their gpa greater than a (hint: use the result of one of traversal algorithms)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

StudentBST.java

package bstnew;

import java.util.ArrayList;

/* Class containing left and right child of current node and id value*/

class StudentNode

{

int id;

double gpa;

StudentNode left, right;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public double getGpa() {

return gpa;

}

public void setGpa(double gpa) {

this.gpa = gpa;

}

public StudentNode getLeft() {

return left;

}

public void setLeft(StudentNode left) {

this.left = left;

}

public StudentNode getRight() {

return right;

}

public void setRight(StudentNode right) {

this.right = right;

}

public StudentNode(int id, double gpa) {

super();

this.id = id;

this.gpa = gpa;

}

@Override

public String toString() {

return "StudentNode [id=" + id + ", gpa=" + gpa + "]";

}

}

//Java program to demonstrate delete operation in binary search tree

class StudentBST

{

// Root of BST

StudentNode root;

// Constructor

StudentBST()

{

root = null;

}

public void addStudent(StudentNode sn){

root=insertRec(root,sn);

}

StudentNode insertRec(StudentNode root, StudentNode sn)

{

if (root == null)

{

root = sn;

return root;

}

if (sn.id < root.id)

root.left = insertRec(root.left, sn);

else if (sn.id > root.id)

root.right = insertRec(root.right, sn);

return root;

}

ArrayList<StudentNode> inorderTraversal(){

ArrayList<StudentNode> nodeList = new ArrayList<>();

inorderTraversal(root,nodeList);

return nodeList;

}

void inorderTraversal(StudentNode root,ArrayList<StudentNode> nodeList)

{

if (root != null)

{

inorderTraversal(root.left,nodeList);

nodeList.add(root);

inorderTraversal(root.right,nodeList);

}

}

ArrayList<StudentNode> preorderTraversal(){

ArrayList<StudentNode> nodeList = new ArrayList<>();

preorderTraversal(root,nodeList);

return nodeList;

}

void preorderTraversal(StudentNode root,ArrayList<StudentNode> nodeList)

{

if (root != null)

{

nodeList.add(root);

preorderTraversal(root.left,nodeList);

preorderTraversal(root.right,nodeList);

}

}

ArrayList<StudentNode> postTraversal(){

ArrayList<StudentNode> nodeList = new ArrayList<>();

postTraversal(root,nodeList);

return nodeList;

}

void postTraversal(StudentNode root,ArrayList<StudentNode> nodeList)

{

if (root != null)

{

postTraversal(root.left,nodeList);

postTraversal(root.right,nodeList);

nodeList.add(root);

}

}

int gpaGreaterThan(double a){

int count=0;

ArrayList<StudentNode> list = postTraversal();

for(StudentNode s : list){

if(s.gpa>a){

count++;

}

}

return count;

}

public static void main(String[] args)

{

StudentBST tree=new StudentBST();

StudentNode student1 = new StudentNode(1, 90);

StudentNode student2 = new StudentNode(5, 80);

StudentNode student3 = new StudentNode(2, 100);

StudentNode student4 = new StudentNode(4, 30);

StudentNode student5 = new StudentNode(3, 40);

tree.addStudent(student1);

tree.addStudent(student2);

tree.addStudent(student3);

tree.addStudent(student4);

tree.addStudent(student5);

System.out.println("Record Inorder");

for(StudentNode s : tree.inorderTraversal()){

System.out.println(s);

}

System.out.println();

System.out.println("Record PreOrder");

for(StudentNode s : tree.preorderTraversal()){

System.out.println(s);

}

System.out.println();

System.out.println("Record PostOrder");

for(StudentNode s : tree.postTraversal()){

System.out.println(s);

}

int count = tree.gpaGreaterThan(70);

System.out.println("\nStudent count with gpa greater than 70 is : "+count);

}

}

output:

Console 3 rminated> StudentBST Java Application] C:\Program Files Javare7\bin javaw.exe (De Record Inorder StudentNode id-1, apa-90.01 StudentNode [id-2, gpa-100.0] StudentNode [id-3, gpa-40.0] StudentNode [id-4, gpa 30.0] StudentNode [id-5, gpa-80.0] Record PreOrder StudentNode [id-l, gpa-90.0] StudentNode [id-5, gpa-80.0] StudentNode [id-2, gpa-100.0] StudentNode [id-4, gpa 30.0] StudentNode [id-3, gpa-40.0] Record PostOrder StudentNode [id-3, gpa-40.0] StudentNode [id-4, gpa 30.0] StudentNode [id-2, gpa-100.0] StudentNode [id-5, gpa-80.0] StudentNode [id-l, gpa-90.0] Student count with gpa greater than 70 is : 3

Add a comment
Know the answer?
Add Answer to:
java A University would like to implement its students registery as a binary search tree, calledStudentBST....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT