Question

Using Java, Load the data provided below as text file using binary tree algorithm. Create two...

Using Java,

Load the data provided below as text file using binary tree algorithm.

Create two methods, remove, and search, of a binary search tree.

The search method shall allow a search by salary. It should display the matched salary and the associated name. Otherwise return not found message.

The delete method shall delete the object that matched the search criteria, search by name.

Name Salary
Betty 44000
Bob 48000
Dilbert 98000
Joseph 22300
Nathan 90000
Sally 91000
Sam 87000
Susan 8900
Veronica 150000
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note - To search by both name and salary, 2 different Binary trees will be created to correspond for the required search key.

All further explanation is in the code comments. Hope this helps!

Code:

import java.util.*;
import java.io.*;

// class to store individual employees
class Employee
{
public String name;
public int salary;
  
// constructors
Employee(String name, int salary)
{
this.name = name;
this.salary = salary;
}
Employee(Employee employee)
{
this.name = employee.name;
this.salary = employee.salary;
}
}

// class to store BST for employees
class Tree
{
public Employee val;
public Tree right, left;
// constructor
Tree(Employee val)
{
this.val = new Employee(val);
this.right = null;
this.left = null;
}
}

// Main class
public class Main
{
// add data to tree where data is stored by name
public static Tree addData1(Tree root, Employee employee)
{
// end of tre
if(root == null)
{
root = new Tree(employee);
}
// add to right of tree if greater
else if(root.val.name.compareTo(employee.name)<0)
{
root.right = addData1(root.right, employee);
}
else // else left of tree
{
root.left = addData1(root.left, employee);
}
return root;
}
  
// add data to tree where data is stored by salary
public static Tree addData2(Tree root, Employee employee)
{
// end of tree
if(root == null)
{
root = new Tree(employee);
}
// add to right of tree if greater salary
else if(root.val.salary < employee.salary)
{
root.right = addData2(root.right, employee);
}
else // else left of tree
{
root.left = addData2(root.left, employee);
}
return root;
}
  
// get the minimum value of tree -> leftmost leaf
public static Employee minValue(Tree root)
{
Employee minv = root.val;
while (root.left != null)
{
minv = root.left.val;
root = root.left;
}
return minv;
}
  
// overloaded function
// search by salary
public static void search(Tree root, int salary)
{
// reached end of tree, but salary not found
if(root == null)
{
System.out.println("Salary: " + salary + " not found");
return;
}
if(root.val.salary == salary)
{
System.out.println("Name: " + root.val.name + ", Salary: " + root.val.salary);
}
else if(root.val.salary < salary)
{
search(root.right, salary);
}
else
{
search(root.left, salary);
}
}
  
// overloaded function
// search by name
public static Employee search(Tree root, String name)
{
// reached end of tree, but name not found
if(root == null)
{
System.out.println("Name: " + name + " not found");
return null;
}
if(root.val.name.equals(name))
{
System.out.println("Removing node - " + root.val.name + ", " + root.val.salary);
// return the nodeToBeDeleted
return root.val;
}
else if(root.val.name.compareTo(name) < 0)
{
return search(root.right, name);
}
else
{
return search(root.left, name);
}
}
  
// remove data from tree where search is by name
public static Tree remove(Tree root, String name)
{
if(root == null)
return root;
  
if(root.val.name.equals(name))
{
if (root.left == null)
root = root.right;
else if (root.right == null)
root = root.left;
else
{
// node with two children: Get the inorder successor (smallest
// in the right subtree)
root.val = minValue(root.right);
  
// Delete the inorder successor
root.right = remove(root.right, root.val.name);
}
}
else if(root.val.name.compareTo(name) < 0)
{
root.right = remove(root.right, name);
}
else
{
root.left = remove(root.left, name);
}
return root;
}
  
// remove data from tree where search is by salary
public static Tree remove(Tree root, int salary)
{
if(root == null)
return root;
  
if(root.val.salary == salary)
{
if (root.left == null)
root = root.right;
else if (root.right == null)
root = root.left;
else
{
// node with two children: Get the inorder successor (smallest
// in the right subtree)
root.val = minValue(root.right);
  
// Delete the inorder successor
root.right = remove(root.right, root.val.salary);
}
}
else if(root.val.salary < salary)
{
root.right = remove(root.right, salary);
}
else
{
root.left = remove(root.left, salary);
}
return root;
}
  
   public static void main(String[] args) {
  
   // try to open file
   try {
  
   // input stream
       Scanner sc = new Scanner(new File("input.txt"));
       String name;
       int salary;
       Tree rootbyName = null, rootbySalary = null;
       Employee tmp;
      
       // read data from file
       while (sc.hasNext()) {
       name = sc.next();
       salary = sc.nextInt();
       // add to the 2 trees
       rootbyName = addData1(rootbyName, new Employee(name, salary));
       rootbySalary = addData2(rootbySalary, new Employee(name, salary));
       }
       // close stream
       sc.close();
      
       // sample run ------------------------------------------------------
       // search for salary = 87000
       search(rootbySalary, 87000);
       // delete the Employee with name Sam
       tmp = search(rootbyName, "Sam");
       if(tmp!=null)
       {
       // Delete from both trees
       rootbyName = remove(rootbyName, tmp.name);
       rootbySalary = remove(rootbySalary, tmp.salary);
       }
       // again search for salary = 87000
       search(rootbySalary, 87000);
      
   } catch (IOException e) {
   System.out.println("Unable to open file");
   }
   }
}

Sample run:

Name: Sam, Salary: 87000 Removing node - Sam, 87000 Salary: 87000 not found

Code screenshots:

i import java.util.*; 2 import java.io.*; 4 5 6 - // class to store individual employees class Employee { public String name;

if(root == null) root = new Tree (employee); // add to right of tree if greater else if(root.val.name.compareTo(employee.name

root = root.left; return minv; // overloaded function // search by salary public static void search(Tree root, int salary) //

return root.val; 130 131 132 133 134 else if(root.val.name.compareTo(name) < 0) return search(root.right, name); else return

173 174 175 176 // remove data from tree where search is by salary public static Tree remove(Tree root, int salary) 177 178 1

216 217 218 219 int salary; Tree rootbyName = null, rootbySalary = null; Employee tmp; 220 221 NNNNNNN // read data from file

Add a comment
Know the answer?
Add Answer to:
Using Java, Load the data provided below as text file using binary tree algorithm. Create two...
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