I want to compare between nodes in BST to find the highest salary and im using recursion function in implementing my BST.

Below is the question:

A company needs to keep all records of its employees. Employee record should
contain employee id(4 digit integer numbers), name, department, salary and
an employee phone number. Using bst, write a program to do the following tasks:

1. Insert data. (Arrange by employee id).
2. Display all data.
3. Display data of all employees from the same department.
4. Display the employee with the highest salary.
5. Count and display how many employee have salary > 10000
6. Display the total salary of all employees.
7. Display the average salary.

*no data duplication

So this is my code for finding the highest salary. It doesn't give the correct output.

``````  public void highestSalary()
{
System.out.println("\n<EMPLOYEE WITH HIGHEST SALARY>\n");
Employee highestEmployee = new Employee();
Employee highestEmp = highestSalaryHelper(root, 0.0, highestEmployee);
System.out.println(highestEmp.toString());

}

public Employee highestSalaryHelper(TreeNode node, double hS, Employee hE)
{

if(node.left!=null)
{
if(node.data.getSalary()>hS)
{
hS = node.data.getSalary();
hE = node.data;
}

else
{
highestSalaryHelper(node.left, node.left.data.getSalary(), hE);
}
}

if(node.right!=null)
{
if(node.data.getSalary()>hS)
{
hS = node.data.getSalary();
hE = node.data;
}

else
{
highestSalaryHelper(node.right, node.right.data.getSalary(), hE);
}
}

return node.data;

}

}
``````
2
Contributors
4
Replies
21
Views
5 Years
Discussion Span
Last Post by bguild

no one able to solve this? or didnt get what im asking about?

Why do you modify `hS` and `hE` even when those variables will never be used again?

Why do you call `highestSalaryHelper` and not use its return value? Isn't its return value supposed to be an employee with the the highest salary? I thought you wanted that employee.

Why does `highestSalaryHelper` have only one `return` statement that always returns the employee of the node that `highestSalaryHelper` was given? What if that's not the employee with the highest salary?

Edited by bguild

Im using hS and hE to assign highest salary and the employee with the highest salary so that it can compare salary from every node in the bst and return the employee.

Why im calling highestSalary though didn't use the return value? I did use its value to display the employee data.

I didn't actually really know how to traverse through all nodes in the bst and sorry if my code looks so rubbish, im still a beginner.

In your `highestSalaryHelper` you want three `Employee`s. One of them is `node.data`. One of them is the highest salary employee from `node.left` and the the last one is the highest salary employee from `node.right`. Once you have those three, I hope you can figure out the highest salary employee of the entire tree.

I recommend that you simplify your problem by making `highestSalaryHelper` take only one argument, a `TreeNode node`, and promise yourself that `highestSalaryHelper` will always return the `Employee` with the highest salary among the employees in `node` and its children. Then you can get your three `Employee`s easily as `node.data`, `highestSalaryHelper(node.left)`, and `hightestSalaryHelper(node.right)`. Of course you'll have to check for `null`s.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.