c++ Write your own version of a class template that will create a binary tree th
ID: 3829072 • Letter: C
Question
c++
Write your own version of a class template that will create a binary tree that can hold values of any data type.
Then, design an EmployeeInfo class that holds the following employee information:
Employee ID Number:an integer
Employee Name:a 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 NumberName
1021John Williams
1057Bill Witherspoon
2487Jennifer Twain
3769Sophia Lancaster
1017Debbie Reece
1275George McMullen
1899Ashley Smith
4218Josh 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.
Please run the program to make sure that it gives this provided output............................
The following is a small example of a subset of a run of this program:
Here is the workforce:
ID Number: 1017 Name: Debbie Reece
ID Number: 1021 Name: John Williams
ID Number: 1057 Name: Bill Witherspoon
ID Number: 1275 Name: George McMullen
ID Number: 1899 Name: Ashley Smith
ID Number: 2487 Name: Jennifer Twain
ID Number: 3769 Name: Sophia Lancaster
ID Number: 4218 Name: Josh Plemmons
Enter an employee number: 3769
Employee was found:
ID Number: 3769 Name: Sophia Lancaster
Explanation / Answer
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST
{
int data;
struct BST *lchild,*rchild;
}node;
char name;
int id;
void insert(node *,node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *,int,node **);
void main()
{
int choice;
char ans='N';
int key;
node *new_node,*root,*tmp,*parent;
node *get_node();
root=NULL;
printf(" Employees list ");
do
{
printf(" 1.Create");
printf(" 2.Search");
printf(" 4.Exit");
printf(" Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
do
{
new_node=get_node();
printf(" Information about employe with number: ");
scanf("%d",&new_node->data);
printf(" Name: ");
scanf("%s",&name);
printf(" Id. Number: ");
scanf("%d",&id);
if(root==NULL) /* Tree is not Created */
root=new_node;
else
insert(root,new_node);
printf(" Do you wish to enter a new employe?(y/n)");
ans=getch();
}while(ans=='y');
break;
case 2:
printf(" Insert the employe number you're looking for: ");
scanf("%s",&key);
tmp = search(root,key,&parent);
printf(" Parent of node %d is %d",
tmp->data,parent->data);
break;
case 3:
if(root==NULL)
printf("Tree Is Not Created");
else
{
printf(" The Inorder display: ");
inorder(root);
printf(" The Preorder display: ");
preorder(root);
printf(" The Postorder display: ");
postorder(root);
}
break;
}
}while(choice!=4);
}
/*
Get new Node
*/
node *get_node()
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root,node *new_node)
{
if(new_node->data < root->data)
{
if(root->lchild==NULL)
root->lchild = new_node;
else
insert(root->lchild,new_node);
}
if(new_node->data > root->data)
{
if(root->rchild==NULL)
root->rchild=new_node;
else
insert(root->rchild,new_node);
}
}
/*
This function is for searching the node from
binary Search Tree
*/
node *search(node *root,int key,node **parent)
{
node *temp;
temp=root;
while(temp!=NULL)
{
if(temp->data==key)
{
printf(" Employe with number %d is present",temp->data);
return temp;
}
*parent=temp;
if(temp->data>key)
temp=temp->lchild;
else
temp=temp->rchild;
}
return NULL;
}
/*
This function displays the tree in inorder fashion
*/
void inorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->lchild);
printf("%d",temp->data);
inorder(temp->rchild);
}
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp)
{
if(temp!=NULL)
{
printf("%d",temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp)
{
if(temp!=NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d",temp->data);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.