CS301 ASSIGNMENT NO. 2 SPRING 2023 || 100% RIGHT SOLUTION || DATA STRUCTURES || BY VuTech
Visit Website For More Solutions
www.vutechofficial.blogspot.com
KINDLY, DON’T COPY PASTE
Problem Statement:
You are working for a company that manages a database of employee records. Your task is to implement a binary search tree to store employee records efficiently and in an organized manner. Each employee record consists of a unique employee ID, name, and salary. The binary search tree should allow efficient search and insertion of employee records based on their employee ID.
The program will prompt the user to enter the ID and name as input. The Employee ID should be the last three digits of your VUID and the salary should be the last five digits of the VUID. The program will extract the Employee ID and salary from the VUID entered by the user. For example, if a student has a VUID of "BC123456789", the user should enter "123456789" as the ID, and the program will extract "789" as the Employee ID and "56789" as the salary.
That first record should be hardcoded and that should be your VU ID. Once the program is running, you will be able to search for employee records based on their Employee ID. Additionally, you can insert more records into the binary search tree as your application continues to run.
You are required to create a program using C++ programming language to implement binary search tree (BST).
Program:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Node {
int empID;
string name;
double salary;
Node* left;
Node* right;
public:
Node(int id, string n, double s) : empID(id), name(n), salary(s),left(NULL) ,right(NULL) {}
int getEmpID() const {
return empID;
}
string getName() const {
return name;
}
double getSalary() const {
return salary;
}
Node* getLeft() const {
return left;
}
Node* getRight() const {
return right;
}
void setLeft(Node* root) {
left = root;
}
void setRight(Node* root) {
right = root;
}
};
class BST {
Node* root;
Node* insertHelper(Node* root, int empID, string name, double salary) {
if (root == NULL) {
return new Node(empID, name, salary);
}
if (empID < root->getEmpID()) {
root->setLeft(insertHelper(root->getLeft(), empID, name, salary));
} else {
root->setRight(insertHelper(root->getRight(), empID, name, salary));
}
return root;
}
Node* searchHelper(Node* root, int empId) {
if (root == NULL) {
return NULL;
}
if(empId == root->getEmpID()){
return root;
}
else if (empId < root->getEmpID()) {
return searchHelper(root->getLeft(), empId);
} else {
return searchHelper(root->getRight(), empId);
}
}
public:
BST(): root(NULL) {}
Node* getRoot() const {
return root;
}
void insert(int id, string name) {
int empID = id % 1000;
double salary = id % 100000;
root = insertHelper(root, empID, name, salary);
}
Node* search(int id) {
return searchHelper(root, id);
}
void printInOrder(Node* node) {
if (node == NULL) {
return;
}
printInOrder(node->getLeft());
cout << "ID: " <<setw(3) << setfill('0') << node->getEmpID() << ", "
<< "Name: " << node->getName() << ", "
<< "Salary: " <<setw(5) << setfill('0') << node->getSalary() << endl;
printInOrder(node->getRight());
}
};
int main() {
BST bstTree;
bstTree.insert(200405877 , " Fahad");
string employeeName;
int Vu_ID;
int enterNo = 0;
do {
cout << "Enter 1 to check the existing record\n"
<< "Enter 2 to insert a new employee record\n"
<< "Enter 3 to search for an employee record\n"
<< "Enter 4 to exit\n\n";
cin >> enterNo;
switch (enterNo) {
case 1:
cout << "Existing Employee Records:\n";
bstTree.printInOrder(bstTree.getRoot());
cout << endl;
break;
case 2:
cout << "Enter Employee ID: ";
cin >> Vu_ID;
cout << "Enter Employee Name: ";
cin.ignore();
getline(cin, employeeName);
bstTree.insert(Vu_ID, employeeName);
cout << endl;
break;
case 3:
int ID;
cout << "Enter Employee ID: ";
cin >> ID;
Node* searchData = bstTree.search(ID);
if (searchData != NULL) {
cout << "Employee found: --> ID: " << setw(3) << setfill('0') << searchData-> getEmpID()
<< "\nName: " << searchData->getName()
<< "\nSalary: " <<setw(5) << setfill('0') << searchData ->getSalary() << endl;
} else {
cout << "Invalid ID\n";
}
cout << endl;
break;
}
} while (enterNo != 4);
return 0;
}
KINDLY, DON’T COPY PASTE
SUBSCRIBE, SHARE, LIKE AND COMMENTS FOR MORE UPDATES
SEND WHATSAPP OR E-MAIL FOR ANY QUERY
0325-6644800
kamranhameedvu@gmail.com
Visit Website For More Solutions
www.vutechofficial.blogspot.com