Você está na página 1de 5

#include<iostream>

#include<string>
#include<conio.h>
#include<cstring>

using namespace std;

struct node {
int empid;
double empsalary;
char* empname;

node* left,*right;

};
node* buildbst(node* root, int j, int id, char* name,double salary)
{
if(root==NULL)
{
node* newnode= new node();

newnode->empid=id;

newnode->empname=name;

newnode->empsalary=salary;

newnode->left=newnode->right=NULL;
return newnode;
}

// ---------------------------------------------------page 1

if(root->empid==id)
{
return root;
}
else if(id<=root->empid)
{
root->left=buildbst(root->left,j,id,name,salary);
}

else
{
root->right= buildbst(root->right,j,id,name,salary);
}
}

void ascendingorder(node* root)


{
if(root==NULL)
{return;
}
ascendingorder(root->left);
cout<<root->empid<<"\t"<<root->empname<<"\t\t"<<root-
>empsalary<<endl;
ascendingorder(root->right );
//---------------------------------------------------------------------------------
---------------page 2
}
void postorder(node* root)
{ if(root==NULL)

{ return;
}

postorder(root->left);
postorder(root->right);

cout<< root->empid<<"\t"<< root->empname<<"\t" << root->empsalary<< endl;


}

main()

int id[]={32,56,93,5,10};
char* name[]={"Raza","sajid","Rabia","sehar","Ali"};
double salary[]= {30000,25000,19230,24000,22200};

int c;
int x=(sizeof(id)/sizeof(*id));
node* root=NULL;
char string[20];
for(int j=0;j<x; j++)
{

root = buildbst(root,j,id[j],name[j],salary[j]);
}

//---------------------------------------------------------------------------------
------------------page 3
p:cout<<endl<<"press 1 to enter data" <<"press 2 to retrieve the record ascending
order"<< "press 3 to retrieve the post order\n"<<"press 4 to exit";
cin >>c;

switch(c)
{
case 1:
{

q: cout<<endl<<"enter id";
cin>>id[x];
cout<<endl<<"enter salary";
cin>>salary[x];
cout<<endl<<"enter name";
cin>>string;

for(int i=0;i<x;i++)
{
if(id[x]==id[i])
{
cout<<endl<<"already exist";
goto y;
}
}
//---------------------------------------------------page 4
name[x]= new char[strlen(string)+1];
strcpy(name[x],string);

for(int j =0;j<(x+1);j++)
{
root=buildbst(root,j,id[j],name[j],salary[j]);
}
cout<< "record is entered";
char sd;
y: cout <<endl<<"do u want to record another record";
sd=getch();
if (sd=='y')
{ goto q;}
else if(sd=='n')
{ goto y;

}
else { cout<<"wrong";
}
break;
}
case 2: //{

cout<< "assending order \n";

ascendingorder(root);
break;
//}
case 3 :

cout<< "post order \n" ;

postorder(root);

break;

case 4:
return 0;
break ;
default:

cout<<"\n wrong input";


break;
}
char ch;
//----------------------------------------------------page 5

y: cout<<endl<<"do u want to do something press y\n";

ch= getch();
if(ch=='y')
{
goto p;
}

else
{
cout<<endl<<"\n wrong";
goto y;
}
}

Você também pode gostar