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)
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:
java A University would like to implement its students registery as a binary search tree, calledStudentBST....
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
Can someone help with these two problems? The following binary tree contains the characters 'A' through 'G' in its nodes. List the nodes in the order that they are visited by: A preorder traversal. An inorder traversal. A postorder traversal. The binary tree in Problem 2 is a Binary Search Tree since for every node, all elements stored in the left subtree of the node are smaller than the element in the node and all elements stored in the right...
LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.
in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...
In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT { private BinaryTreeNode root; /** * Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; } /** * Creates a binary tree from an existing root. */ public LinkedBinaryTree(BinaryTreeNode root) { this.root = root; } /** * Creates a binary tree with the specified element...
1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...
Binary Search tree Implementation of a BST class that include the following operations: - Insertion, Search, Deletion - Traversals: Inorder, Preorder, Postorder using c++
Create a binary search tree from the following: 43, 5, 2, 54, 64, 23, 6, 48, 30 and write its preorder, inorder and postorder traversal. (Needs to be in C#)
a) Create a binary search tree with the 4 2 2 0 1 4 5 9 7 1 5 3 6 number. b) Provide the Preorder, Inorder and Postorder traversal of tree obtained in part (a). Show all the steps.
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...