You are on page 1of 23

// DF

void DF1(int x)
{
viz1[x]=1;
for(int i=1; i<=n; i++)
if(a[x][i]==1 && viz1[i]==0)
DF1(i);
}

// BF
void BF(int R)
{
p=u=1;///curatare a lui viz
c[1]=R;viz[R]=1;
while(p<=u)
{
///prelucrez c[p];
for(i=1; i<=n; i++)
if(a[c[p]][i]==1 && viz[i]==0)
{
viz[i]=1;
u++;c[u]=i;
cout<<i<<" ";
}
p++;
}}

//Nivelurile si verisorii unui nod


#include <fstream>
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
int R,T[100],viz[100],c[100],n;
/// Functia BF calculeaza nivelele fiecarui nod
void BF(int x)
{
int p=1,u=1;
c[1]=x;
viz[x]=0;// in viz se retine nivelul nodului(si e marcat in
acelasi timp)
while(p<=u&&u<=n)
{
for(int i=1; i<=n; i++)
if(T[i]==c[p])
{
c[++u]=i;
viz[i]=viz[c[p]]+1;
}
p++;
}
}
void reset()
{
for(int i=1;i<=n;i++)

viz[i]=0;
}
int main()
{
f>>n;
for(int i=1; i<=n; i++)
{
f>>T[i];
if(T[i]==0)R=i;
}
BF(R);
for(int i=1; i<=n; i++)
g<<viz[i]<<' ';
reset();
g<<endl;
///descendentii nodului X:
int X;
f>>X;
BF(X);
for(int i=1;i<=n;i++)
if(viz[i]>=1)
g<<i<<' ';
reset();
g<<endl;
/// verisorii unui nod
int Y;
f>>Y;

BF(Y);
int nr=0;
for(int i=1;i<=n;i++)
if(viz[Y]==viz[i] && T[Y]!=T[i] && T[T[Y]]==T[T[i]])
{g<<i<<' ';nr++;}
if(nr==0)
g<<Y<<" nu are verisori";
return 0;
}

// Nivelurile unui arbore


#include<iostream.h>
int n,max=0, r, T[100], a[100][100], p[100];
void citire()
{ int i,x,y;
cin>>n;
for(i=1;i<=n-1;i++)
{ cin>>x>>y;
a[x][y]=a[y][x]=1;;
}
cin>>r;
}
void DF(int r, int niv)
{ p[r]=1;
if(niv>max) max=niv;
for(int i=1;i<=n;i++)
if(a[r][i] &&!p[i])
DF(i,niv+1);
}
void main()
{ int i;
citire();
DF(r,1);
cout<<max;
}

// Frunzele

#include<iostream.h>
int n, T[100], p[100];
void main()
{ int i;
cin>>n;
for(i=1;i<=n;i++)
{ cin>>T[i];
p[T[i]]=1;
}
for(i=1;i<=n;i++)
if(!p[i]) cout<<i<<" ";
}

//Se citeste un arbore cu n varfuri dat


prin vectorul TATA si apoi un varf k. Sa se
afiseze vectorul TATA obtinut prin mutarea
radacinii arborelui in varful k.
Ex: Pentru vectorul de tati 2 0 2 1 3 si
nodul k=5 se va afisa vectorul 2 3 5 1 0.

#include<iostream.h>
int n,t[100],k;
void citire()
{ cin>>n;
for(int i=1;i<=n;i++) cin>>t[i];
cin>>k;
}
void f(int k)
{ if(t[k]) f(t[k]);
t[t[k]]=k;
}
void afis()
{ for(int i=1;i<=n;i++) cout<<t[i]<<" ";
}
void main()
{ citire();
f(k);
t[k]=0;
afis();
}

////////////////////////////////////////////////////

#include<fstream.h>
fstream fin("date.in",ios::in);
fstream fout("date.out",ios::out);
int a[20][20],t[100],n,max,mx,my,p[100],f[100];
void citire()
{ int i;
fin>>n;
for(i=1;i<=n;i++)
{fin>>t[i];
f[t[i]]=1;
a[i][t[i]]=1;
a[t[i]][i]=1;
}
}
void df( int r, int niv,int k)
{p[k]=1;
if(niv>max){ max=niv;
mx=r;
my=k;
}
for(int i=1;i<=n;i++)
if( !p[i] && a[k][i])
df(r,niv+1,i);
}
void ff(int r)
{ if(t[r]) ff(t[r]);
t[t[r]]=r;
}
void lant(int r)
{ if(t[r]) lant(t[r]);
fout<<r<<" ";
}
void main()
{ citire();
for(int i=1;i<=n;i++)
if(f[i]==0)
{ for(int j=1;j<=n;j++)
p[j]=0;
df(i,0,i);
}
ff(mx);
t[mx]=0;
lant(my);
}

//Se da un arbore cu n varfuri prin


vectorul TATA. Afisati care dintre
varfurile arborelui por fi alese ca

radacina a acestuia astfel incat arborele


sa aiba inaltime minima.
#include<fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int n,t[100];
void citire()
{ fin>>n;
for(int i=1;i<=n;i++) fin>>t[i];
}
void f(int k)
{ if(t[k]) f(t[k]);
t[t[k]]=k;
}
int inaltime()
{
int i,j,h,maxx=0;
for(i=1;i<=n;i++)
{
h=0; j=i;
while(t[j])
{
h++;
j=t[j];
}
if(h>maxx) maxx=h;
}
return maxx;
}
int main()
{
int h[100],k,minn;
citire();
minn=n;
for(k=1;k<=n;k++)
{
f(k);
t[k]=0;
h[k]=inaltime();
if(h[k]<minn) minn=h[k];
}
for(k=1;k<=n;k++)
if(h[k]==minn) fout<<k<<" ";
fin.close();
fout.close();
return 0;
}

//Se da un arbore cu n noduri prin vectorul


de referinte de tip tata. Sa se determine
daca arborele poate fi reprezentat ca un
lant.
#include<fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int n,t[100],g[100];
void citire()
{ fin>>n;
for(int i=1;i<=n;i++)
{
fin>>t[i];
if(t[i])
{
g[i]++;
g[t[i]]++;
}
}
}
int main()
{
citire();
int ok=1;
for(int i=1;i<=n;i++)
if(g[i]>2) ok=0;
if(ok) fout<<"da";
else fout<<"nu";
return 0;
}

// Se dau doi arbori cu radacina prin doi


vectori de tip tata. Determinati daca cei
doi arbori cu radacina reprezinta acelasi
arbore (difera doar in urma alegerii
radacinilor diferite)
#include<fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int n,t[100],s[100];
void citire()
{ fin>>n;
for(int i=1;i<=n;i++)

fin>>t[i];
for(int i=1;i<=n;i++)
fin>>s[i];

int main()
{
citire();
int ok=1,gasit;
for(int i=1;i<=n;i++)
if(t[i])
{
gasit=0;
for(int j=1;j<=n;j++)
if(i==j && t[i]==s[j] || i==s[j] && t[i]==j)
gasit=1;
if(!gasit) ok=0;
}
if(ok) fout<<"da";
else fout<<"nu";
return 0;
}

///stramosul cel mai apropiat de doua


noduri
#include <fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int x,y,n,r,t[100],viz[100],a[100][100],niv[100],nivx,nivy,tata;
void inaltime(int r)
{for(int i=1;i<=n;i++)
if(a[r][i]==1 && viz[i]==0)
{niv[i]=niv[r]+1;
viz[i]=1;
inaltime(i);}}
int main()
{fin>>n>>r>>x>>y;
for(int i=1;i<=n;i++)
{fin>>t[i];
if(t[i]==0)
{a[r][i]=1;
a[i][r]=1;}
else
{a[i][t[i]]=1;
a[t[i]][i]=1;}}
inaltime(r);

//????????????????????????????????????????????????????
for(int i=1;i<=n;i++)
if(i==x) nivx=niv[i];
else
if(i==y) nivy=niv[i];
if(nivx>nivy)
while(nivx>nivy)
{x=t[x];
nivx--;}
else
while(nivx<nivy)
{y=t[y];
nivy--;}
while(x!=y)
{x=t[x];
y=t[y];}
fout<<x;
return 0;}

///radacina astfel incat inaltimea


arborelui sa fie maxima
#include <fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int x,y,a[100][100],n,maxim,radacina,niv[100],viz[100];
void inaltime(int r)
{for(int i=1;i<=n;i++)
if(a[r][i]==1 && viz[i]==0)
{niv[i]=niv[r]+1;
viz[i]=1;
inaltime(i);}}
int main()
{fin>>n;
for(int i=1;i<=n-1;i++)
{fin>>x>>y;
a[x][y]=1;
a[y][x]=1;}
for(int i=1;i<=n;i++)
{for(int j=1;j<=n;j++)
{viz[j]=0;
niv[j]=0;}
niv[i]=0;

viz[i]=1;
inaltime(i);
for(int p=1;p<=n;p++)
if(niv[p]>maxim)
{maxim=niv[p];
radacina=i;}}
fout<<radacina<<" "<<maxim;
return 0;}

///transformare in arbore partial


#include <fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int n,m,x,y,a[100][100],L[100];
int parcurgere(int n)
{int ok=0,x=L[1];
for(int i=2;i<=n;i++)
if(L[i]!=L[1])
{ok=1;
break;}
return ok;}
int main()
{fin>>n>>m;
for(int i=1;i<=n;i++)
L[i]=i;
for(int i=1;i<=m;i++)
{fin>>x>>y;
if(L[x]!=L[y] && parcurgere(n))
{int k=L[x];
for(int j=1;j<=n;j++)
if(L[j]==k)
L[j]=L[y];
fout<<x<<" "<<y<<endl;}}
return 0;}

///transformare in arbore partial ! pt un


graf neconex !
#include <fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int n,m,x,y,L[100],p,t,maxim;

struct muchie{int x,y;}mc[100];


int parcurgere(int n)
{int ok=0,x=L[1];
for(int i=2;i<=n;i++)
if(L[i]!=L[1])
{ok=1;
break;}
return ok;}
int main()
{fin>>n>>m;
for(int i=1;i<=n;i++)
L[i]=i;
for(int i=1;i<=m;i++)
{fin>>x>>y;
if(L[x]!=L[y] && parcurgere(n))
{int k=L[x];
for(int j=1;j<=n;j++)
if(L[j]==k)
L[j]=L[y];
mc[++p].x=x;
mc[++t].y=y;}}
while(parcurgere(n))
{for(int i=2;i<=n;i++)
if(L[i]!=L[1])
{int k=L[i];
for(int j=1;j<=n;j++)
if(L[j]==k)
L[j]=L[1];
mc[++p].x=1;
mc[++t].y=i;}}
for(int i=1;i<=p;i++)
fout<<mc[i].x<<" "<<mc[i].y<<endl;
return 0;}

///determinare vector tati


#include <fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int n,r,t[100],viz[100],a[100][100],x,y;

void bf(int r)
{int c[100],p=1,u=1;
c[1]=r;
viz[r]=1;
while(p<=u)
{for(int i=1;i<=n;i++)
if(a[c[p]][i]==1 && viz[i]==0)
{viz[i]=1;
t[i]=c[p];
c[++u]=i;
}
p++;
}}
int main()
{fin>>n>>r;
for(int i=1;i<=n-1;i++)
{fin>>x>>y;
a[x][y]=1;
a[y][x]=1;}
t[r]=0;
bf(r);
for(int i=1;i<=n;i++)
fout<<t[i]<<" ";
return 0;}

Arbori de Cautare
///dublare chei frunze
#include <fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int n,v[100],i,minim,maxim;
struct nod{int info; nod*st,*dr;}*r;
void adaugare(int n, nod*&r)

{if(r)
{if(n<r->info) adaugare(n,r->st);
else adaugare(n,r->dr);}
else{r=new nod;
r->info=n;
r->st=0;
r->dr=0;}}
void SRD(nod *r)
{if(r)
{SRD(r->st);
fout<<r->info<<" ";
SRD(r->dr);}}
void parcurgere(nod *r)
{if(r)
{if(r->st==0 && r->dr==0)
r->info=2*r->info;
parcurgere(r->st);
parcurgere(r->dr);}}
int main()
{fin>>n;
for(int i=1;i<=n;i++)
{fin>>v[i];
adaugare(v[i],r);}
parcurgere(r);
SRD(r);
return 0;}

///interschimbare cheie minima si cheie maxima


#include <fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int n,v[100],i,minim,maxim;
struct nod{int info; nod*st,*dr;}*r;
void adaugare(int n, nod*&r)
{if(r)
{if(n<r->info) adaugare(n,r->st);
else adaugare(n,r->dr);}
else{r=new nod;
r->info=n;
r->st=0;
r->dr=0;}}
void SRD(nod *r)
{if(r)
{SRD(r->st);
fout<<r->info<<" ";
SRD(r->dr);}}
int main()
{fin>>n;
for(int i=1;i<=n;i++)
{fin>>v[i];
adaugare(v[i],r);}

nod* i=r;
while(i->st)
{minim=i->st->info;
i=i->st;}
nod* j=r;
while(j->dr)
{maxim=j->dr->info;
j=j->dr;}
fout<<minim<<" "<<maxim<<endl;
SRD(r);
return 0;}

/// Arborele daca se schimba radacina


#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("date.in");
ofstream fout ("date.out");
int T[100], F[100], n, R, x;
int main()
{
fin>>n;
for(int i=1; i<=n; i++)
{fin>>T[i];
F[T[i]]=i;}
for(int i=1; i<=n; i++)

{
if(T[i]==0)
R=i;
}
fout<<R<<endl;
fin>>x;
T[R]=x;
T[x]=0;
for(int i=1; i<=n; i++)
fout<<T[i]<<"

";

return 0;
}
//Creare ABC
void adaugare (nod *&R, int n)
{
if(R!=0)
{
if(n<R->info)
adaugare(R->st,n);
else adaugare(R->dr,n);
}
else
{
R=new nod;
R->info=n;

R->st=0;
R->dr=0;

}
}
//Creare AB
nod * creare()
{
int n;
nod *c;
fin>>n;
if(n==0) return NULL;
else
{
c=new nod;
c->info=n;
c->st=creare();
c->dr=creare();
return c;
}
}

#include <iostream>

#include <fstream>
using namespace std;
ifstream fin ("date.in");
ofstream fout ("date.out");
struct nod
{
int info;
nod *st, *dr;
}*R;
int ok=0, ok1=0, ok2=0;
void creare2(nod *& r)
{
int n;
fin>>n;
if(n==0) r=NULL;
else
{
r= new nod;
r->info=n;
creare2(r->st);
creare2(r->dr);

}
}
void RSD(nod *r)

{
if(r!= NULL)
{
fout<<r->info<<"

";

RSD(r->st);
RSD(r->dr);
}
}
/// Verifica daca AB este ABC
void verificare(nod *r)
{
if(r!= NULL)
{
if( (r->st!=NULL &&r->st->info>r->info) || (r->dr!=NULL
&&r->dr->info<r->info))
{
fout<<"Nu este ABC";
ok=1;
return;
}
else
{
verificare(r->st);
verificare(r->dr);
}
}

}
/// Verificati daca AB este echilibrat
int inaltime(nod *r)
{
if(r==NULL) return 0;
else return 1+max(inaltime(r->st),inaltime(r->dr));
}
void echil(nod *r)
{
if(r)
{
if(inaltime(r->st)-inaltime(r->dr)>1)
{
ok1=1;
fout<<endl;
fout<<"Nu este echilibrat"<<endl;
return ;
}
else
{
echil(r->st);
echil(r->dr);
}

}
/// Verificati daca AB este perfect echilibrat
int noduri(nod *r)
{
if(r==NULL) return 0;
else return 1+noduri(r->st)+ noduri(r->dr);
}
void perfect_echil(nod *r)
{
if(r)
{
if(noduri(r->st)-noduri(r->dr)>1)
{
ok2=1;
fout<<endl;
fout<<"Nu este perfect echilibrat"<<endl;
return ;
}
else
{
perfect_echil(r->st);
perfect_echil(r->dr);
}
}
}

int main()
{
creare2(R);
RSD(R);
fout<<endl;
verificare(R);
fout<<endl;
if(ok==0) fout<<"Este ABC"<<endl;
echil(R);
if(ok1==0) fout<<"Este echilibrat"<<endl;
fout<<"Inaltimea arborelui este "<<inaltime(R)-1<<endl;
fout<<"Numarul de noduri ale arborelui este
"<<noduri(R)<<endl;
perfect_echil(R);
if(ok2==0) fout<<"Este perfect echilibrat"<<endl;

return 0;
}