Hey there, welcome to the post about CS301 Assignment 2 Solution! If you’re on the hunt for the perfect solution to your CS301 Assignment 2, you’ve come to the right spot. This post has got you covered! We’ve prepared a comprehensive and well-crafted solution that’ll help you succeed in your academics. Whether you searched for “cs301 assignment 2 solution” or “cs301 assignment 2 solution 2023,” rest assured, we’ve got all the answers right here. So, let’s dive in together and uncover the secrets to conquering this assignment with ease!
Related Posts:
- ENG101 GDB Solution
- MGT211 Assignment No 1 Solution
- CS302p assignment 1 solution
- CS201p Assignment 1 Solution
- VU Handouts
Solution
#include <iostream>
#include <string>
using namespace std;
// TopicsLearn.com
// Employee record structure
struct Employee {
string name;
int id;
int salary;
Employee* left;
Employee* right;
Employee(string name, int id, int salary) {
this->name = name;
this->id = id;
this->salary = salary;
this->left = NULL;
this->right = NULL;
}
};
// BST class
class BST {
private:
Employee* root;
public:
BST() {
root = NULL;
}
// Insert a new employee record into the BST
void insert(Employee* employee) {
if (root == NULL) {
root = employee;
} else {
Employee* current = root;
while (true) {
if (employee->id < current->id) {
if (current->left == NULL) {
current->left = employee;
break;
} else {
current = current->left;
}
} else {
if (current->right == NULL) {
current->right = employee;
break;
} else {
current = current->right;
}
}
}
}
cout << "Employee record inserted successfully!" << endl;
}
// Search for an employee record based on the ID
Employee* search(int id) {
Employee* current = root;
while (current != NULL) {
if (id == current->id) {
return current;
} else if (id < current->id) {
current = current->left;
} else {
current = current->right;
}
}
return NULL; // Employee not found
}
// In-order traversal of the BST (for testing purposes)
void inorderTraversal(Employee* node) {
if (node != NULL) {
inorderTraversal(node->left);
cout << "ID:bc220212960 " << node->id << ", Name:Komal Zahid " << node->name << ", Salary:56789 " << node->salary << endl;
inorderTraversal(node->right);
}
}
};
int main() {
// Create the BST object
BST bst;
// Hard code the first employee record (replace with your VU ID)
Employee* firstEmployee = new Employee("Komakl Zahid", 220212960, 56789);
bst.insert(firstEmployee);
// Prompt the user to enter ID and name
string name;
int id;
cout << "Enter Employee ID:bc220212960 ";
cin >> id;
cout << "Enter Employee Name:Komal Zahid ";
cin.ignore(); // Ignore newline character
getline(cin, name);
// Extract Employee ID and salary from the given ID
int extractedID = id % 1000; // Assuming ID is always 3 digits
int extractedSalary = id / 1000; // Assuming salary is always 5 digits
// Create a new employee record
Employee* newEmployee = new Employee(name, extractedID, extractedSalary);
// Insert the new employee record into the BST
bst.insert(newEmployee);
// Search for an employee record based on ID
int searchID;
cout << "Enter the ID to search: ";
cin >> searchID;
Employee* foundEmployee = bst.search(searchID);
if (foundEmployee != NULL) {
cout << "Employee found! Name: " << foundEmployee->name << ", Salary: " << foundEmployee->salary << endl;
}
}