Hello guys, any one know how to implement a general tree using c++. Implement a
ID: 3907066 • Letter: H
Question
Hello guys, any one know how to implement a general tree using c++. Implement a class which contain a string name as a data member. ask the user to enter one of the following actions - add employee (the first one to be added will be the head of the company) - add a subordinate of an existing employee(employees are to be added left to right : the left most child is the one with the highest seniority) - each employee can hire as many employees as you want (it is recomended to use a vector of pointers to point to the children of a node) - find employee and return a pointer to its location - fire employee (when firing an employee, you want the leftmost child to take its place and you want that to cascade all the way down until the employee has no subordinates.
Explanation / Answer
Implemented Source Code:-
----------------------------------
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include "binary.h"
#define COUNT 5
using namespace std;
struct node
{
string data;
node *Left_link;
node *Right_link;
};
node* insert_node(node *temp,node *newnode);
struct node *root=NULL;
bool Binarytree::add(string x)
{
node *temp=root;
node *newnode;
newnode=new node;
newnode->Left_link=NULL;
newnode->Right_link=NULL;
newnode->data=x;
root=insert_node(temp,newnode);
}
node* insert_node(node *temp,node *newnode)
{
if(temp==NULL)
{
temp=newnode;
}
else if(temp->data < newnode->data)
{
insert_node(temp->Right_link,newnode);
if(temp->Right_link==NULL)
temp->Right_link=newnode;
}
else
{
insert_node(temp->Left_link,newnode);
if(temp->Left_link==NULL)
temp->Left_link=newnode;
}
return temp;
}
void inorderdisplay(node *t = root)
{
if(root==NULL)
{
printf(" The Tree is Empty");
}
else if(t!=NULL)
{
inorderdisplay(t->Left_link);
cout<<t->data<<"-->";
inorderdisplay(t->Right_link);
}
}
node* TreeSearch(node *root, string key)
{
node *temp;
temp = root;
while (temp != NULL)
{
if(temp->data==key)
{
cout<<" The Element "<<temp->data<<"is Present"<<endl;
return temp;
}
if(temp->data > key)
temp = temp->Left_link;
else
temp = temp->Right_link;
}
return NULL;
}
void printSideways(node *root, int space)
{
if (root == NULL)
return;
space += COUNT;
printSideways(root->Right_link, space);
cout<<endl;
for(int i=COUNT;i<space;i++)
cout<<" ";
cout<<root->data;
printSideways(root->Left_link, space);
}
int main()
{
Binarytree obj;
string datavalue;
string words[]={"Beth", "Sue", "Dave", "Pat", "Mike", "Dawn", "Cindi", "Gina"};
for(int i=0;i<8;i++)
{
datavalue=words[i];
obj.add(datavalue);
}
int option;
cout<<" -------------------------"<<endl;
cout<<" Implementation of Binary Search tree"<<endl;
while(true)
{
cout<<" ** MENU **"<<endl;
cout<<" 1.Insert the Data"<<endl;
cout<<" 2.Display"<<endl;
cout<<" 3.Search"<<endl;
cout<<" 4.printSideways"<<endl;
cout<<" 5.exit"<<endl;
cout<<" Please Select any Choice"<<endl;
cin>>option;
switch(option)
{
case 1:
cout<<" Please Enter the New Node"<<endl;
cin>>datavalue;
obj.add(datavalue);
break;
case 2:
cout<<" Inorder Display"<<endl;
inorderdisplay();
break;
case 3:
cout<<" Enter the Data to Search:"<<endl;
cin>>datavalue;
TreeSearch(root,datavalue);
break;
case 4:
printSideways(root,4);
break;
case 5:
exit(0);
default:
cout<<" Invalid Option"<<endl;
}
}
}
header file(binary.h):-
-------------------------
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Binarytree
{
public:
bool add(string str);
public:
void Display();
void DrawTree(struct node *root, int space);
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.