Question

In: Computer Science

Please do this in visual studio C++. Overview In this programming challenge, you will create an...

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.

Solutions

Expert Solution

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


Related Solutions

Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is...
Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is a sub class is derived from the Account class and implements the ITransaction interface. There are two class variables i.e. variables that are shared but all the objects of this class. A short description of the class members is given below: CheckingAccount Class Fields $- COST_PER_TRANSACTION = 0.05 : double $- INTEREST_RATE = 0.005 : double - hasOverdraft: bool Methods + «Constructor» CheckingAccount(balance =...
Create a C++ project in visual studio. You can use the C++ project that I uploaded...
Create a C++ project in visual studio. You can use the C++ project that I uploaded to complete this project. 1. Write a function that will accept two integer matrices A and B by reference parameters, and two integers i and j as a value parameter. The function will return an integer m, which is the (i,j)-th coefficient of matrix denoted by A*B (multiplication of A and B). For example, if M = A*B, the function will return m, which...
In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication,...
In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should request a numerical input from the user, followed by the operation to be performed, and the second number to complete the equation. The result should be displayed to the user after each equation is performed. For example, if the user performs 3+3, the program should display 6 as the result. The program should continue running so the user can...
In visual Studio C++ Create a program that uses a for loop to input the high...
In visual Studio C++ Create a program that uses a for loop to input the high temperature, and low temperature for each day of the week. The high and low will be placed into two elements of the array. For each loop the high and low will be placed into the next set of elements of the array. After the temps for all seven days have been entered into the array, a for loop will be used to pull out...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this part of the assignment, you are required to create a C# Console Application project. The project name should be A3<FirstName><LastName>P2. For example, a student with first name John and Last name Smith would name the project A1JohnSmithP2. Write a C# (console) program to calculate the number of shipping labels that can be printed on a sheet of paper. This program will use a menu...
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3...
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3 text boxes: 2 of them to enter numbers, the 3rd one displays the results Create 4 buttons to add, subtract, multiply, and divide Prevent the user from entering text in the number fields Display a message indicating “cannot divide by” when the user click “/” and there is a zero the in the second box Create two additional buttons: - One to store data...
Create a new website using C# & ASP.Net in Visual Studio: 1. Create a web page...
Create a new website using C# & ASP.Net in Visual Studio: 1. Create a web page to access a database and display the data from a table by providing an SQL statement. Create the page with these requirements:     create label, textbox and button     the textbox will capture the SQL statement     the button will process the statement and display the results 2. Add necessary data access pages to your project. 3. Display the data in Gridview
Create a C# .NET Core Console project in Visual Studio. (This is the same kind of...
Create a C# .NET Core Console project in Visual Studio. (This is the same kind of project we have been doing all semester.) Do all of the following in the Program class. You do not need to add any other classes to this project. 2. If it exists, remove the Console.WriteLine(“Hello World!”); line that Visual Studio created in the Program class. 3. At the very top of the Program.cs page you should see using System; On the empty line below...
Using Visual Studio in C#; create a grading application for a class of ten students. The...
Using Visual Studio in C#; create a grading application for a class of ten students. The application should request the names of the students in the class. Students take three exams worth 100 points each in the class. The application should receive the grades for each student and calculate the student’s average exam grade. According to the average, the application should display the student’s name and the letter grade for the class using the grading scheme below. Grading Scheme: •...
create a C++ program using Visual Studio that could be used by a college to track...
create a C++ program using Visual Studio that could be used by a college to track its courses. In this program, create a CollegeCourse class includes fields representing department, course number, credit hours, and tuition. Create a child (sub class) class named LabCourse, that inherits all fields from the the CollegeCourse class, includes one more field that holds a lab fee charged in addition to the tuition. Create appropriate functions for these classes, and write a main() function that instantiates...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT