Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class structure of nodes representing employees. Each employee node has

ID: 3689830 • Letter: C

Question

Create a class structure of nodes representing employees. Each employee node has a name, and then can have zero or more employees working for them. Prompt the user for an employee name. Create a node for that employee. Ask how many employees work for that employee. Repeat steps 1-3 for each employee that works for the employee. Each new employee node should be stored as a child node of the employee node for whom they work. This will be recursive -- i.e. you may end up inputting employees of employees of employees of employees of ... You may not store any additional information beyond what was mentioned above this line. Display a list of all employees that have 3+ employees working for them (including all "descendant" employees). Find all employees that do not have any employees working for them. Display each one's name, and a list of their supervisors in order from direct manager to top employee.

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
struct emp_node{
string name;
int no_children;
emp_node *ptr_nextChild[100];
};
class employeetree {
public:
emp_node *root;
employeetree()
{
root=NULL;
}
~employeetree()
{
clear(root);
}
void clear(emp_node *root){

while(root!=NULL)
{
for(int i=0;i<root->no_children;i++)
clear(root->ptr_nextChild[i]);
delete root;
}
}
struct emp_node* insertt(string name,int d)
{
emp_node *e = new emp_node[d];
e->name = name;
e->no_children = d;
for(int i=0;i<d;i++)
e->ptr_nextChild[i] = NULL;
if(root == NULL) root = e;
else
{
emp_node *ptr= root;
while(ptr!=NULL)
{ string n;int des;
for(int i=0;i<ptr->no_children;i++)
{
cout<<" Enter its descendant name:";
cin>>n;
cout<<" Enter its no. of children:";
cin>>des;
e->ptr_nextChild[i]=insertt(n,des);
}
}
}
return e;

}
void printemployee(emp_node *emp)
{
while(emp!=NULL)
{
if(emp->no_children>2)
{
cout<<" "<<emp->name;
}
for(int i=0;i<emp->no_children;i++)
printemployee(emp->ptr_nextChild[i]);
}
}
bool isLeaf(emp_node *r)
{
if(r== NULL || r->no_children==0) return true;
else {
return false;
}
}
void LeafPathPrint(emp_node *emp)
{
if(emp==NULL) return;
else if(emp->no_children==0) { cout<<endl<<emp->name <<" is leaf node"<<endl; return;}
else
{

for(int i=0;i<emp->no_children;i++)
LeafPathPrint(emp->ptr_nextChild[i]);
cout<<emp->name<<"->";

}
}


};
int main()
{
employeetree *e = new employeetree;
string emp_name;
int n;
cout << "Enter the employee name" << endl;
cin>>emp_name;
cout<<" Enter no. of descendants"<<endl;
cin>>n;
e->root = e->insertt(emp_name,n);
e->printemployee(e->root);
e->LeafPathPrint(e->root);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote