Practical 5. Program based on binary search Trees maintain employee database and implement task: 1.Display the names of employee based on salary 2.Display the name of employee having maximum and minimum salary 3.Find the total monthly expenses of the company on employee salary - SCIENCE BUZZ

Practical 5. Program based on binary search Trees maintain employee database and implement task: 1.Display the names of employee based on salary 2.Display the name of employee having maximum and minimum salary 3.Find the total monthly expenses of the company on employee salary

#include <stdlib.h>    
#include <string.h>
typedef struct Employee {
    char name[100];    
    float salary;
    struct Employee* left;
    struct Employee* right;
} Employee;

Employee* createEmployee(char* name, float salary) {
    Employee* newEmployee = (Employee*)malloc(sizeof(Employee));
    strcpy(newEmployee->name, name);
    newEmployee->salary = salary;
    newEmployee->left = newEmployee->right = NULL;
    return newEmployee;
}

Employee* insert(Employee* root, char* name, float salary) {
    if (root == NULL) {
        return createEmployee(name, salary);
    }

    if (salary < root->salary) {
        root->left = insert(root->left, name, salary);
    } else {
        root->right = insert(root->right, name, salary);
    }
    return root;
}

void displayEmployeesBySalary(Employee* root) {
    if (root != NULL) {
        displayEmployeesBySalary(root->left);
        printf("Name: %s, Salary: %.2f\n", root->name, root->salary);
        displayEmployeesBySalary(root->right);
    }
}

Employee* findMax(Employee* root) {
    while (root->right != NULL) {
        root = root->right;
    }
    return root;
}

// Function to find the employee with minimum salary
Employee* findMin(Employee* root) {
    while (root->left != NULL) {
        root = root->left;
    }
    return root;
}

// Function to calculate total monthly expenses on salaries
float calculateTotalExpenses(Employee* root) {
    if (root == NULL) {
        return 0;
    }
    return root->salary + calculateTotalExpenses(root->left) + calculateTotalExpenses(root->right);
}

int main() {
    Employee* root = NULL;
    root = insert(root, "Tanushri", 5000000);
    root = insert(root, "Isha", 3000000000);
    root = insert(root, "Deepali", 90000);
    root = insert(root, "Shreashi", 4500000);
    root = insert(root, "Sneha", 600000);

    printf("Employees sorted by salary:\n");
    displayEmployeesBySalary(root);

    Employee* maxEmployee = findMax(root);
    Employee* minEmployee = findMin(root);

    printf("\nEmployee with maximum salary:\nName: %s, Salary: %.2f\n", maxEmployee->name, maxEmployee->salary);
    printf("Employee with minimum salary:\nName: %s, Salary: %.2f\n", minEmployee->name, minEmployee->salary);

    float totalExpenses = calculateTotalExpenses(root);
    printf("\nTotal monthly expenses on employee salaries: %.2f\n", totalExpenses);

    return 0;
}

Practical 5. Program based on binary search Trees maintain employee database and implement task: 1.Display the names of employee based on salary 2.Display the name of employee having maximum and minimum salary 3.Find the total monthly expenses of the company on employee salary Practical 5. Program based on binary search Trees maintain employee database and implement task: 1.Display the names of employee based on salary 2.Display the name of employee having maximum and minimum salary 3.Find the total monthly expenses of the company on employee salary Reviewed by RISHI and ARYAN on 22:52 Rating: 5

No comments:

May I help you ?

Powered by Blogger.