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

Programing with java BINARY SEARCH TREE DATA STRUCTURE OBJECTIVES -Students know

ID: 3714668 • Letter: P

Question

Programing with java

BINARY SEARCH TREE DATA STRUCTURE OBJECTIVES

-Students know how to implement the Binary Search Tree structure: How to insert a node to a tree, How to fetch, delete or update a node on the Binary Search tree. -Also, students know how to display the information of all nodes on the tree

REQUIREMENT Create an application that allows users can work on the information of students with the following tasks:

1. Insert

Allow users to enter the information of a candidate or a candidate with experience from the keyboard -Create the object then insert to the data structure

2. Fetch

-Allow users to type a candidate id then search in the date structure for looking for the candidate or the candidate with experience. If it is found, print out the information on the screen otherwise display the message: “The candidate cannot be found”

3. Encapsulation

-Allow users to type a candidate id from the keyboard. Search in the date structure. If the candidate (or candidate with experience) is found, ask for new phone number from the keyboard and change the phone of the candidate (or candidate with experience). -Fetch the candidate (or candidate with experience) with the same provided id. Then compare the phone of the candidate (or candidate with experience) just fetched with the new phone. If both the phone numbers are the same then display the message “Binary Search Tree structure is not encapsulated” otherwise display the message “Binary Search Tree structure is encapsulated” =[

4. Update

Display the message to ask users to enter the candidate id. Using the candidate id to read the node out the data structure as a candidate (or a candidate with experience). Asking for the new degree from the keyboard and change the degree of the candidate (or the candidate with experience) with the new degree to have a new node. Update the data structure with the new node with the same candidate id. Display the message to see if update successfully or not

5. Delete

Display the message to ask users to enter the candidate id. Remove the node with the entered id Display the message to see if delete successfully or not

6. Show all

Display all the candidate (or candidate with experience) are currently stored in the data structure

Explanation / Answer

import java.util.Scanner;
class BinarySearchTree {

/* Class containing left and right child of current node and key value*/
class Node {
int key;
String name;
float exp;
Node left, right;

public Node(int item,String name1,float exp1) {
key = item;
name=name1;
exp=exp1;
left = right = null;
}
}

// Root of BST
Node root;

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

// This method mainly calls insertRec()
void insert(int key,String name1,float exp1) {
root = insertRec(root, key, name1, exp1);
}

/* A recursive function to insert a new key in BST */
Node insertRec(Node root, int key,String name1,float exp1) {

/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(key,name1,exp1);
return root;
}

/* Otherwise, recur down the tree */
if (key < root.key)
root.left = insertRec(root.left,key, name1, exp1);
else if (key > root.key)
root.right = insertRec(root.right, key, name1, exp1);

/* return the (unchanged) node pointer */
return root;
}

// This method mainly calls InorderRec()
void inorder() {
inorderRec(root);
}

// A utility function to do inorder traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key+" "+root.name+" "+root.exp);
inorderRec(root.right);
}
}

// Driver Program to test above functions
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
String name;
int key;
float exp;
Scanner input=new Scanner(System.in);
while(true)
{
System.out.print("Enater -1 to exit:");
key=input.nextInt();
if(key==-1)
break;
System.out.print("Enater id in int:");
key=input.nextInt();
System.out.print("Enater id in name:");
name=input.next();
System.out.print("Enater the exp:");
exp=input.nextFloat();
tree.insert(key, name, exp);

}
  
// print inorder traversal of the BST
tree.inorder();
}
}