0% found this document useful (0 votes)
20 views8 pages

CS301P Assignment 2 Solution Spring 2024

The document provides a solution for CS301P Assignment 2 for Spring 2024, detailing a C++ program for an Employee Management System using a Binary Search Tree (BST). It includes instructions on how to name the program, the importance of displaying the student's VUID, and a warning against copy-pasting solutions. Additionally, it offers a contact for assistance and emphasizes the need for originality in submissions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

CS301P Assignment 2 Solution Spring 2024

The document provides a solution for CS301P Assignment 2 for Spring 2024, detailing a C++ program for an Employee Management System using a Binary Search Tree (BST). It includes instructions on how to name the program, the importance of displaying the student's VUID, and a warning against copy-pasting solutions. Additionally, it offers a contact for assistance and emphasizes the need for originality in submissions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS301P ASSIGNMENT 2 SOLUTION SPRING 2024

Due Date: 24-June-2024


Total Marks: 20

DO NOT COPY PASTE THE SAME

Note:
You must follow the following program structure for naming and to build the program.
Also, while displaying output STUDENT OWN VUID should also be displayed
otherwise, you will get ZERO marks.

FOR CORRECT SOLUTION WITH YOUR OWN STUDENTS ID CONTACT


WHATSAPP +923162965677

CODE Solution:

#include <iostream>
using namespace std;

class Employee {
public:
int employeeID;
string name;

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
Employee(int id, string empName) : employeeID(id), name(empName) {}
};

class Node {
public:
Employee* employee;
Node* left;
Node* right;

Node(Employee* emp) : employee(emp), left(NULL), right(NULL) {}


};

class BST {
public:
Node* root;

BST() : root(NULL) {}

void insertEmployee(int id, string name) {


Employee* newEmployee = new Employee(id, name);
root = insertEmployeeHelper(root, newEmployee);
cout << "Employee with ID " << id << " has been added." << endl;
}

Node* insertEmployeeHelper(Node* node, Employee* emp) {

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
if (node == NULL) {
return new Node(emp);
}

if (emp->employeeID < node->employee->employeeID) {


node->left = insertEmployeeHelper(node->left, emp);
} else if (emp->employeeID > node->employee->employeeID) {
node->right = insertEmployeeHelper(node->right, emp);
} else {
cout << "Employee ID already exists." << endl;
}

return node;
}

void removeEmployee(int id) {


if (searchEmployee(id) == NULL) {
cout << "Employee ID " << id << " not found." << endl;
} else {
root = removeEmployeeNode(root, id);
cout<<""<<endl;
cout << "Employee with ID " << id << " has been removed." << endl;
cout<<""<<endl;
}
}

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
Node* removeEmployeeNode(Node* node, int id) {
if (node == NULL) {
return node;
}

if (id < node->employee->employeeID) {


node->left = removeEmployeeNode(node->left, id);
} else if (id > node->employee->employeeID) {
node->right = removeEmployeeNode(node->right, id);
} else {
if (node->left == NULL) {
Node* temp = node->right;
delete node;
return temp;
} else if (node->right == NULL) {
Node* temp = node->left;
delete node;
return temp;
}

Node* temp = minValueNode(node->right);


node->employee = temp->employee;
node->right = removeEmployeeNode(node->right, temp->employee->employeeID);
}

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
return node;
}

Node* minValueNode(Node* node) {


Node* current = node;
while (current && current->left != NULL) {
current = current->left;
}
return current;
}

void printBST() {
cout<<""<<endl;
cout << "Display Employee Records:" << endl;
printBSTInOrder(root);
}

void printBSTInOrder(Node* node) {


if (node == NULL) return;

printBSTInOrder(node->left);
cout << "Employee ID: " << node->employee->employeeID << ", Name: " << node-
>employee->name << endl;
printBSTInOrder(node->right);
}

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
Node* searchEmployee(int id) {
return searchEmployeeHelper(root, id);
}

Node* searchEmployeeHelper(Node* node, int id) {


if (node == NULL || node->employee->employeeID == id) {
return node;
}

if (id > node->employee->employeeID) {


return searchEmployeeHelper(node->right, id);
}

return searchEmployeeHelper(node->left, id);


}
};

int main() {
cout << "Employee Management System by BC123456789" << endl;
cout << "----------------------------------------" << endl;

BST bst;

// Insert employees

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
bst.insertEmployee(1, "Bilal");
bst.insertEmployee(2, "Kaleem");
bst.insertEmployee(3, "Akmal");
cout<<""<<endl;

// Print all employees


bst.printBST();

// Remove an employee
bst.removeEmployee(2);

// Print all employees after removal


bst.printBST();

return 0;
}

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
REGARD - SARIM
WHATSAPP +923162965677

PLEASE NOTE:
Don't copy-paste the same answer.
Make sure you can make some changes to your solution file before
submitting copy paste solution will be marked zero.
If you found any mistake then correct yourself and inform me.
Before submitting an assignment must check your assignment requirement
file.
If you need some help or question about file and solutions feel free to ask.

FOR FREE ASSIGNMENTS SOLUTIONS VISIT

VUStudentspk.com

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1

You might also like