Documente Academic
Documente Profesional
Documente Cultură
int lung)
max, lung+1);
&& rad->dr==NULL && max>lung)
max, lung+1);
r=rot_s_dr(r);
return r;
}
arb* rot_d_st(arb* r)
{
r->dr=rot_s_dr(r->dr);
r=rot_s_st(r);
return r;
}
arb* echilibrare(arb* r)
{
arb* w;
calc_echi(r);
if(r->ech==-2)
{
w=r->st;
if(w->ech==1)
r=rot_d_dr(r);
else r=rot_d_st(r);
}
else
if(r->ech==2)
{
w=r->dr;
if(w->ech==-1)
r=rot_d_st(r);
else r=rot_d_dr(r);
}
return r;
}
arb* insereaza(arb* r, int x)
{
if(r==NULL)
{
r=new arb;
r->info=x;
r->dr=NULL;
r->st=NULL;
r->ech=0;
return r;
}
else
if (x<r->info)
r->st=insereaza(r->st,x);
else
if (x>r->info)
r->dr=insereaza(r->dr,x);
else cout<<"nu exista ceva";
r=echilibrare(r);
return r;
}
arb* stergere(arb* r, int x)
{
arb* q, *p, *w;
if(r!=NULL)
if(x<r->info)
r->st=stergere(r->st,x);
else
if(x>r->info)
r->dr=stergere(r->dr,x);
else
{
q=r;
if(q->dr==NULL)
r=q->st;
else
if(q->st==NULL)
r=q->dr;
else
{
w=q->st;
p=q;
if(w->dr!=NULL)
{
while(w->dr!=NULL)
{
p=w;
w=w->dr;
}
r->info=w->info;
q=w;
p->dr=w->st;
p=r->st;
w=w->st;
if(p!=NULL)
while(p!=w && p!
=NULL)
{
p=echili
brare(p);
p=p->dr;
}
}
else
{
r->info=w->info;
r->st=w->st;
q=w;
}
}
delete(q);
}
if(r!=NULL)
r=echilibrare(r);
return r;
}
void blanc(int n)
{
for (int i=0; i<n; i++)
{
putchar(' ');
}
}
void main()
{
arb* r;
int x,n=0;
cout<<"NOD: "; cin>>x; cout<<endl;
while(x!=-1)
{
r=insereaza(r,x);
af_niv(r,n);
cout<<"NOD: ";cin>>x; cout<<endl;
}
}