Hi, I need someone well versed in C++ to provide the source code for a program t
ID: 3868333 • Letter: H
Question
Hi, I need someone well versed in C++ to provide the source code for a program to follow. Please list file headings (whatever is a .h and .cpp so I can clearly know how to compile it), comments so that a beginner knows what the code is accomplishing, and sample output to show the program works successfully. There will be additional files already completed that this program will need to integrate with/use. Please ensure that it will use those files. I will include program requirements below followed by the files you can use to ensure it works with them.
Program Requirements
Files You Must Use With It
https://www.awesomescreenshot.com/showImage?img_id=2736994
In this program you are going to use the binary tree class you created in Assignment 1 this week. First create a class called Employeeinfo that holds two private data members. One data member is an integer called emplD which holds the id number of the employee. The second data member is a string called empName which holds the full name of the employee. The program will create an instance of the binary tree class with a data type of Employeelnfo (Binary Tree). The binary tree will be sorted by the Employee ID number found in the Employeeinfo class. The program should then allow the user to search for Employee by the Employee ID. If the employee is found in the tree, its name and ID should be displayed. If not, a message should be displayed indicating that it was not found. Sample data would be • 1021 John Williams • 1057 Bill Witherspoon • 2487 Jennifer Twain • 3769 Sophia Lancaster • 1017 Debbie Reece • 1275 George McMullen • 1899 Ashley Smith • 4218 Josh PlemmonsExplanation / Answer
bst1.h
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct EmployeeInfo{
int id;
string name;
};
struct tnode
{
tnode *left;
tnode *right;
EmployeeInfo info;
} ;
class BST
{
tnode *root;
public:
BST()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(EmployeeInfo item);
void inordertrav();
void inorder(tnode *);
void postordertrav();
void postorder(tnode *);
void preordertrav();
void preorder(tnode *);
int searchtrav(int id);
int searchpreorder(tnode *,int id);
};
bst1.cpp
#include "bst1.h"
void BST::insert(EmployeeInfo item)
{
tnode *p=new tnode;
tnode *parent;
p->info.id=item.id;
p->info.name=item.name;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tnode *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(item.id > ptr->info.id)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item.id<parent->info.id)
parent->left=p;
else
parent->right=p;
}
}
int BST::searchtrav(int id){
return(searchpreorder(root, id));
}
int BST::searchpreorder(tnode *ptr, int id)
{
int found = 0;
if(ptr!=NULL)
{
if (ptr->info.id == id) {
cout<<" "<<ptr->info.id<<" " << ptr->info.name << endl;
found = 1;
}
else {
found = searchpreorder(ptr->left,id);
if (found != 1)
found = searchpreorder(ptr->right,id);
}
}
return found;
}
void BST::inordertrav()
{
inorder(root);
}
void BST::inorder(tnode *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
cout<<" "<<ptr->info.id<<" " << ptr->info.name << endl;
inorder(ptr->right);
}
}
void BST::postordertrav()
{
postorder(root);
}
void BST::postorder(tnode *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<" "<<ptr->info.id<<" " << ptr->info.name << endl;
}
}
void BST::preordertrav()
{
preorder(root);
}
void BST::preorder(tnode *ptr)
{
if(ptr!=NULL)
{
cout<<" "<<ptr->info.id<<" " << ptr->info.name << endl;
preorder(ptr->left);
preorder(ptr->right);
}
}
int main()
{
BST b;
ifstream fin;
EmployeeInfo emp;
int id;
string fname;
string lname;
fin.open("input22.txt"); // put all the employee data in the file input22.txt
if (fin){
while (fin >> id >> fname >> lname){
emp.id = id;
emp.name = fname + " " + lname;
b.insert(emp);
}
cout<<"inorder"<<endl;
b.inordertrav();
cout<<endl<<"postorder"<<endl;
b.postordertrav();
cout<<endl<<"preorder"<<endl;
b.preordertrav();
cout << "Enter employee id to search:";
cin >> id;
if (b.searchtrav(id) != 1){
cout << "Record not found" << endl;
}
}
else {
cout << "Error opening file ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.