Pagini recente » Cod sursa (job #2308695) | Cod sursa (job #992836) | Cod sursa (job #1225348) | Cod sursa (job #2401838) | Cod sursa (job #2204914)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("arbore.in");
ofstream fout("arbore.out");
struct nod{
char nume[25],prenume[55];
int cnp;
nod *st,*dr;
}*p;
char s1[55],s2[55];
int k,s,x,y,z;
void creare(nod *&p)
{
int x;
fin>>x;
if(x==0)
p=NULL;
else
{
fin.getline(s1,55,' ');
fin.getline(s2,55);
p=new nod;
strcpy(p->nume,s1);
strcpy(p->prenume,s2);
p->cnp=x;
creare(p->st);
creare(p->dr);
}
}
void nivel_k(nod *p,int niv)
{
if(p!=NULL)
{
if(niv==k)
fout<<p->nume<<" "<<p->prenume<<"\n";
else{
nivel_k(p->st,niv+1);
nivel_k(p->dr,niv+1);
}
}
}
int x_e_nepot_a_lui_p(nod *p,int x)
{
if(p->st!=NULL)
if(p->st->st!=NULL)
if(p->st->st->cnp==x)
return 1;
if(p->st!=NULL)
if(p->st->dr!=NULL)
if(p->st->dr->cnp==x)
return 1;
if(p->dr!=NULL)
if(p->dr->st!=NULL)
if(p->dr->st->cnp==x)
return 2;
if(p->dr!=NULL)
if(p->dr->dr!=NULL)
if(p->dr->dr->cnp==x)
return 2;
return 0;
}
int cond;
void veri_primari(nod *p)
{
if(p!=NULL)
{
if(x_e_nepot_a_lui_p(p,x))
{
k=2;
cond=x_e_nepot_a_lui_p(p,x);
if(cond==1)
nivel_k(p->dr,1);
else
nivel_k(p->st,1);
}
else
{
veri_primari(p->st);
veri_primari(p->dr);
}
}
}
void fiu(nod *p)
{
if(p!=NULL)
{
if(p->cnp==y)
{
k=1;
nivel_k(p,0);
}
else
{
fiu(p->st);
fiu(p->dr);
}
}
}
void bunic(nod *p)
{
if(p!=NULL)
{
if(x_e_nepot_a_lui_p(p,z))
fout<<p->nume<<" "<<p->prenume<<"\n";
else
{
bunic(p->st);
bunic(p->dr);
}
}
}
void stranepoti(nod *p)
{
if(p!=NULL)
{
if(p->cnp==s)
{
k=3;
nivel_k(p,0);
}
else
{
stranepoti(p->st);
stranepoti(p->dr);
}
}
}
int main()
{
fin>>k>>x>>y>>z>>s;
p=NULL;
creare(p);
fout<<"persoanele de pe nivelul "<<k<<" sunt:\n";
nivel_k(p,1);
fout<<".................................\nveri primari a lui "<<x<<" sunt:\n";
veri_primari(p);
fout<<".................................\nfii lui "<<y<<" sunt:\n";
fiu(p);
fout<<".................................\nbunicul lui "<<z<<" este:\n";
bunic(p);
fout<<".................................\nstranepotii lui "<<s<<" sunt:\n";
stranepoti(p);
return 0;
}