Question

You are requested to save all employees information of a company hospital in a Binary Search Tree (BST). Write a C program th

note that the solution has to be in ONLY c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution if you have any doubts then you can write in the comment section.

Please give feedback.

Solution:

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>

//define struct node  
struct Node 
{ 
    int EmployeeID;
    char EmployeeName[30];
    int salary;
    struct Node *left, *right; 
}*root = NULL; 
   
// A function to create a new BST node 
struct Node *newNode(int id,char name[],int salary) 
{ 
    struct Node *temp =  (struct Node *)malloc(sizeof(struct Node)); 
    //assign values to Node
    temp->EmployeeID = id;
    strcpy(temp->EmployeeName,name);
    temp->salary=salary;
    temp->left = temp->right = NULL; 
    return temp; 
} 
 
/* A function to insert a new node with given key in BST */
struct Node* insert(struct Node* node, int id,char name[],int salary) 
{ 
    /* If the tree is empty, return a new node */
    if (node == NULL) return newNode(id,name,salary); 
  
    /* Otherwise, recur down the tree according to id*/
    if (id < node->EmployeeID) 
        node->left  = insert(node->left, id,name,salary); 
    else if (id > node->EmployeeID) 
        node->right = insert(node->right, id,name,salary);    
  
    /* return the (unchanged) node pointer */
    return node; 
}   
//function to do preorder traversal of BST 
void preorder(struct Node *root) 
{ 
    if (root != NULL) 
    {
        //print data
        printf("EmployeeID: %d  EmployeeName: %s  Salary: %d\n", 
        root->EmployeeID,root->EmployeeName,root->salary);
        //recur for left and right of tree
        preorder(root->left); 
        preorder(root->right); 
    } 
} 

//function to find totalSalary
float totalSalary(struct Node *root)
{
    float total=0;
    if (root != NULL) 
    {  total+=(root->salary)+totalSalary(root->left)+totalSalary(root->right);
    }
    return total;
}
//function to multiply Id by 10 recursively
void multiplyId(struct Node *root) 
{ 
    if (root != NULL) 
    { 
         
        root->EmployeeID*=10;
        multiplyId(root->left); 
        multiplyId(root->right); 
    } 
} 
   

   
// Driver Program to test above functions 
int main() 
{
    int n,i;
    //take number of Employees from user
    printf("Enter Number Of Employees You want to Enter: ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        int id,salary;
        char name[30];
        //read data from user
        printf("ID: ");
        scanf("%d",&id);
        printf("Name: ");
        scanf("%s",name);
        printf("Salary: ");
        scanf("%d",&salary);
        if(i==0)
        {
            //insert 1st element and update root
            root = insert(root, id,name,salary);
        }
        else
        {
            //insert element
            insert(root, id,name,salary);
        }
    }
    printf("Employees\n\n");
    // print preoder traversal of the BST 
    preorder(root); 
    //call multiplyId function to multiply Id by 10
    multiplyId(root);
    printf("\n\nEmployees After Updation\n\n");
    // print preoder traversal of the BST 
    preorder(root);
    //call totalSalary to find totalSalary and divide it by n to find average
    float averageSalary=(totalSalary(root)/n);
    //print Average Salary
    printf("Average Salary: %f",averageSalary);
    
    return 0; 
} 

Output:

input Enter Number of Employees You want to Enter: 7 ID: 50 Name: Rakesh Salary: 50000 ID: 30 Name: Anuj Salary: 30000 ID: 20input Name: Sanjay Salary: 60000 ID: 80 Name: Vipin Salary: 80000 Employees Employee ID: 50 EmployeeName: Rakesh Salary: 5000

Thnak You!

Add a comment
Know the answer?
Add Answer to:
note that the solution has to be in ONLY c You are requested to save all...
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
  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • please use java application to solve methods: 5,6,10,12,13 (no need for the rest) Problem1: Create a...

    please use java application to solve methods: 5,6,10,12,13 (no need for the rest) Problem1: Create a new Java Application that has the following methods: 6 A method that generate a binary search tree (BST) where the number of nodes and the range of the values are 1. from a file. 2. A recursive method to print the BST in preorder traversal 3. A recursive method to print the BST in post-order traversal 4. A recursive method to print the BST...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Create a new Java Application that has the following methods: A method that generate a binary...

    Create a new Java Application that has the following methods: A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file. A recursive method to print the BST in preorder traversal A recursive method to print the BST in post-order traversal A recursive method to print the BST in in-order traversal A recursive method to count the number of all nodes in BST A method to find...

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

    Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...

  • TestCodeForAssigment.py def main(): # testing recursive find, pre- and post-order, and __len__ """ creating the following...

    TestCodeForAssigment.py def main(): # testing recursive find, pre- and post-order, and __len__ """ creating the following BST: 25 15 29 12 20 27 50 17 """ t1 = BST() t1.insert_rec(25) t1.insert_rec(15) t1.insert_rec(12) t1.insert_rec(20) t1.insert_rec(17) t1.insert_rec(29) t1.insert_rec(27) t1.insert_rec(50) print("Let's see if we can find 27 using the non-recursive find: ", t1.find(27)) print("Let's see if we can find 27 using the recursive find: ", t1.findRecursive(27)) print("The length of the BST tree is ",t1.__len__()) print("The preorder traversal of the tree:", list(t1.preOrder())) print("The postorder...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • C++ Data Structures and Algorithms Binary Trees: Implementation Answer the following question(s) concerning implementing recursive functions...

    C++ Data Structures and Algorithms Binary Trees: Implementation Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes arranged as binary trees. For these questions, use the following struct definition for the nodes of the tree (we will not templatize the node here, so you do not have to write template functions for these questions, just assume trees of <int>values): struct BinaryTreeNode { int item; BinaryTreeNode* left; BinaryTreeNode* right; }; Write a recursive function...

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