In: Computer Science
Please do this in visual studio C++. Overview In this programming challenge, you will create an Employee Tree application. Instructions Begin this assignment by writing your own version of a class template that will create a binary tree that can hold values of any data type. Design an EmployeeInfo class that holds the following employee information: Employee ID Number (int) Employee Name (string) Next, use the template you designed to implement a binary tree whose nodes hold an instance of the EmployeeInfo class. The nodes should be sorted on the Employee ID number. Test the binary tree by inserting nodes with the following information: Employee ID Number Name 1021 John Williams 1057 Bill Witherspoon 2487 Jennifer Twain 3769 Sophia Lancaster 1017 Debbie Reece 1275 George McMullen 1899 Ashley Smith 4218 Josh Plemmons Your program should allow the user to enter an ID number, then search the tree for the number. If the number is found, it should display the employee’s name. If the node is not found, it should display a message indicating so.
Given below is the code for the question. Please do rate the answer if it helped. Thank you.
BinaryTree.h
-----
#ifndef BINARY_TREE
#define BINARY_TREE
#include <iostream>
using namespace std;
template <typename T>
struct Node
{
T data;
Node* left;
Node* right;
Node(T val){
data = val;
left = NULL;
right = NULL;
}
};
template <typename T>
class BinaryTree{
private:
Node<T>* root;
void printInorder(Node<T>* n);
public:
BinaryTree();
void insert(T data);
bool search(T key, T &value);
void printInorder();
};
template <typename T>
BinaryTree<T>::BinaryTree(){
root = NULL;
}
template <typename T>
void BinaryTree<T>::insert(T data){
Node<T>* n = new Node<T>(data);
if(root == NULL)
root = n;
else{
Node<T>* parent = root;
while(parent != NULL){
if(data < parent->data){
if(parent->left == NULL){
parent->left = n;
return;
}
else
parent = parent->left;
}
else if(data > parent->data){
if(parent->right == NULL){
parent->right = n;
return;
}
else
parent = parent->right;
}
else //duplicate
return;
}
}
}
template <typename T>
bool BinaryTree<T>::search(T key, T &value)
{
if(root == NULL)
return false;
Node<T>* curr = root;
while(curr != NULL){
if(curr->data == key){ //found
value = curr->data;
return true;
}
else if(key < curr->data)
curr = curr->left;
else
curr = curr->right;
}
return false;//not found
}
template <typename T>
void BinaryTree<T>::printInorder(){
printInorder(root);
}
template <typename T>
void BinaryTree<T>::printInorder(Node<T>* n){
if(n == NULL) return;
printInorder(n->left);
cout << n->data << endl;
printInorder(n->right);
}
#endif
------
EmployeeInfo.h
-------
#ifndef EMPLOYEE_INFO_H
#define EMPLOYEE_INFO_H
#include <iostream>
using namespace std;
class EmployeeInfo
{
private:
int id;
string name;
public:
EmployeeInfo(){
id = 0;
name = "";
}
EmployeeInfo(int id1, string name1){
id = id1;
name = name1;
}
int getId() const{
return id;
}
void setId(int id1){
id = id1;
}
string getName() const{
return name;
}
void setName(string name1){
name = name1;
}
//define the operators so they can be used in Binary Tree for
comparisons and printing
bool operator ==(const EmployeeInfo &other){
if(id == other.id)
return true;
else
return false;
}
bool operator <(const EmployeeInfo &other){
if(id < other.id)
return true;
else
return false;
}
bool operator >(const EmployeeInfo &other){
if(id > other.id)
return true;
else
return false;
}
friend ostream& operator << (ostream& out, const
EmployeeInfo &e){
out << e.id << " " << e.name ;
return out;
}
};
#endif
-------
main.cpp
--------
#include <iostream>
#include "BinaryTree.h"
#include "EmployeeInfo.h"
using namespace std;
int main(){
BinaryTree<EmployeeInfo> tree;
int choice = 0;
EmployeeInfo e ,result;
int id;
string name;
while(choice != 4){
cout << "1. Insert employee" << endl;
cout << "2. Search employee" << endl;
cout << "3. Print employees" << endl;
cout << "4. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice){
case 1:
cout << "Enter id: ";
cin >> id;
cin.ignore();
cout << "Enter name: ";
getline(cin,name);
e.setId(id);
e.setName(name);
tree.insert(e);
cout << "Employee inserted" << endl;
break;
break;
case 2:
cout << "Enter id of employee to search: ";
cin >> id;
e.setId(id);
e.setName("");
if(tree.search(e, result)){
cout << "Employee found. " << endl;
cout << result << endl;
}
else
cout << "Employee not found." << endl;
break;
case 3:
cout << "The employees in ascending order of Id are "
<< endl;
tree.printInorder();
break;
case 4:
break;
default:
cout << "Invalid choice!" << endl;
}
cout << endl;
}
return 0;
}
output
-----
1. Insert employee
2. Search employee
3. Print employees
4. Quit
Enter your choice: 1
Enter id: 1021
Enter name: John Williams
Employee inserted
1. Insert employee
2. Search employee
3. Print employees
4. Quit
Enter your choice: 1
Enter id: 1057
Enter name: Bill Witherspoon
Employee inserted
1. Insert employee
2. Search employee
3. Print employees
4. Quit
Enter your choice: 1
Enter id: 1017
Enter name: Debbie Reece
Employee inserted
1. Insert employee
2. Search employee
3. Print employees
4. Quit
Enter your choice: 3
The employees in ascending order of Id are
1017 Debbie Reece
1021 John Williams
1057 Bill Witherspoon
1. Insert employee
2. Search employee
3. Print employees
4. Quit
Enter your choice: 2
Enter id of employee to search: 2487
Employee not found.
1. Insert employee
2. Search employee
3. Print employees
4. Quit
Enter your choice: 2
Enter id of employee to search: 1057
Employee found.
1057 Bill Witherspoon
1. Insert employee
2. Search employee
3. Print employees
4. Quit
Enter your choice: 4