assignment 11

PHOTO EMBED

Sat Jun 18 2022 16:45:06 GMT+0000 (Coordinated Universal Time)

Saved by @KanishqJ8

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
 
 
// The following is a employee in the organisation, it has the pointer to two more employees a subordinate_1 and a subordinate_2
struct Employee {
    int emp_id; // emp_ids will be unique
    struct Employee* subordinate_1;
    struct Employee* subordinate_2;
};
 
// The following function creates a employee and returns its pointer
struct Employee* create_employee(int x) {
    struct Employee* ptr = (struct Employee*) malloc(sizeof(struct Employee));
    ptr->emp_id = x;
    ptr->subordinate_1 = NULL;
    ptr->subordinate_2 = NULL;
    return ptr;
}
 
// The following code creates a organisation from scanning the input file
struct Employee* create_company() {
    int x;
    scanf("%d", &x);
 
    if(x == -1) return NULL; // -1 is used when there is a NULL pointer ie when no value is present
    struct Employee* par = create_employee(x);
 
    par->subordinate_1 = create_company();
    par->subordinate_2 = create_company();
    
    return par;
}
 
// The following function 
void print_company_helper(struct Employee* ceo) {
    // take input
    if(!ceo) {
        printf("%d ", -1);
        return;
    }
    printf("%d ", ceo->emp_id);
    print_company_helper(ceo->subordinate_1);
    print_company_helper(ceo->subordinate_2);
    return;
}
 
void print_company(struct Employee* ceo) {
    print_company_helper(ceo);
    printf("\n");
    return;
} 
 
// --------------------------------------------------- YOU CAN EDIT BELOW THIS LINE -----------------------------------
 
struct Employee* CEO = NULL;
// CEO is a global pointer that points to the CEO of the company
 
 
/*  In this function you have to print all the employees at a given level, Note that ceo is at level 0
In any of the functions which involve printing you need not print -1 for NULL pointers */
 
void EmployeesAtSameLevel(int level) {
    // The level may be any integer
    return;
}
 
// You have to print the employees as you search the organization look for the examples in the pdf and the input.txt and output.txt for details
// Note: You do not have to print -1 for NULL pointers
 
void print_my_helper(struct Employee* ceo){
    if(ceo){
        printf("%d ", ceo->emp_id);
    }else{
        return;
    }
    print_my_helper(ceo->subordinate_1);
    print_my_helper(ceo->subordinate_2);
    return;
}
 
void get_employees() {
    struct Employee* ptr=CEO;
    print_my_helper(ptr);
    return;
}
 
/* In the following function you have to print the immediate team of a employee - it includes their boss and their subordinates
Note: You do not have to print -1 for NULL pointers */
 

 
void ImmediateTeam(int emp_id) {
    return;
}
 
// The following function returns the level of a employee with the given emp_id
int Level(int emp_id) {
    // Note that ceo has level 0
    return 0;
}
 
// The following function gives the distance between employees with emp_id1 and emp_id2
 
int max(int x, int y){
    if(x > y){
        return x;
    }else{
        return y;
    }
}
 
int ans;
 
int findDistance(struct Employee* CEO, int n1, int n2)
{
    if (!CEO) return 0;
    int left = findDistance(CEO->subordinate_1, n1, n2);
    int right = findDistance(CEO->subordinate_2, n1, n2);
      //if any node(n1 or n2) is found
    if (CEO->emp_id == n1 || CEO->emp_id == n2)
    {   
          //check if their is any descendant(n1 or n2)
          //if descendant exist then distance between descendant
          //and current root will be our answer.
        if (left || right)
        {
            ans = max(left, right);
            return 0;
        }
        else
            return 1;
    }
      //if current root is LCA of n1 and n2.
    else if (left && right)
    {   
        ans = left + right;
        return 0;
    }
      //if their is a descendant(n1 or n2).
    else if (left || right){
          //increment its distance
        return max(left, right) + 1;
    }
    //if neither n1 nor n2 exist as descendant.
    return 0;
}
 
int Distance(int emp_id1, int emp_id2) {
    // If emp_id1 and emp_id2 are equal the distance is 0, both emp_id's will exist in the organisation
    ans = 0;
    findDistance(CEO,emp_id1,emp_id2);
    return ans;
}
 
 
/* The following function takes an emp_id this will belong to a employee in the organisation and your task is to return the emp_id of its boss
Note: If the boss does not exit return -1 */

int head(struct Employee* CEO, int val)
{
    int left = CEO->subordinate_1->emp_id;
    int right = CEO -> subordinate_2->emp_id;
    
    if ( CEO == NULL || CEO->emp_id==val){
        return -1;
    }
    // If current node is the required node
    else if ((left != NULL && left==val) || (right !=NULL && right==val)) {
        return CEO->emp_id;
    }
 
    else if(head(CEO->subordinate_1, val)!=NULL){
        return head(CEO->subordinate_1, val);
    }
    else if(head(CEO->subordinate_2, val)!=NULL){ 
        return head(CEO->subordinate_2, val);
    }
}

int Boss(int emp_id) {
    return head(CEO,emp_id); 
}
 
/* The following function returns the diameter of a Organisation - 
a diameter is the maximum distance between any two emp_ids in the organisation. You can use the distance function implemented above */
 
int Diameter() {
    return 0;
}
 
/* The following function takes an emp_id of a employee and returns the number of employees directly connected to it.
NULL pointers are not included */
 
int TeamSize(int emp_id) {
    return 0;
}
 
// --------------------------------------------------- YOU CAN EDIT ABOVE THIS LINE -----------------------------------
 
/* The following driver code creates a organisation for you and based on the input file
it will call all the functions that are necessary, your job is to edit the functions
above the line. Their descriptions are in the pdf and in the comments in the code. */
 
int main(int argc, char const *argv[])
{
    CEO = create_company();
    print_company(CEO);
 
    int T; 
    scanf("%d", &T);
 
    char operation_type[20];
 
    while(T--) {
        scanf("%s", operation_type);
 
        if(strcmp(operation_type, "level") == 0) {
            int x;
            scanf("%d", &x);
            int d = Level(x);
            printf("%d\n", d);
        } 
 
        if(strcmp(operation_type, "distance") == 0) {
            int x, y;
            scanf("%d %d", &x, &y);
            int d = Distance(x, y);
            printf("%d\n", d);
        }
 
        if(strcmp(operation_type, "employees_at_same_level") == 0) {
            int x;
            scanf("%d", &x);
            EmployeesAtSameLevel(x);
            printf("\n");
        }
 
        if(strcmp(operation_type, "get_employees") == 0) {
            get_employees();
            printf("\n");
        }
 
        if(strcmp(operation_type, "boss") == 0) {
            int x;
            scanf("%d", &x);
            int d = Boss(x);
            printf("%d\n", d);
        }
 
        if(strcmp(operation_type, "diameter") == 0) {
            int d = Diameter();
            printf("%d\n", d);
        }
 
        if(strcmp(operation_type, "immediate_team") == 0) {
            int x;
            scanf("%d", &x);
            ImmediateTeam(x);
            printf("\n");
        }
 
        if(strcmp(operation_type, "team_size") == 0) {
            int x;
            scanf("%d", &x);
            int d = TeamSize(x);
            printf("%d\n", d);
        }
    }
 
    return 0;
}
content_copyCOPY