Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #define nmax 1234
- using namespace std;
- ifstream in("date.txt");
- struct nod{
- int inf;
- nod *st,*dr;
- };
- nod *rad;
- void creare (nod *&p,int x)
- {
- if(p!=NULL)
- {
- if(x<p->inf)
- creare(p->st,x);
- else
- {
- if(x>p->inf)
- creare(p->dr,x);
- else
- cout<<"Valoare existenta"<<endl;
- }
- }
- else
- {
- p=new nod;
- p->inf=x;
- p->st=p->dr=NULL;
- }
- }
- void SRD(nod *rad)
- {
- if(rad !=NULL)
- {
- SRD(rad->st);
- cout<<rad->inf<<" ";
- SRD(rad->dr);
- }
- }
- void cmd(nod* &p, nod* &f)
- {
- nod *aux;
- if(f->dr)
- cmd(p,f->dr);
- else
- {
- p->inf=f->inf;
- aux=f;
- f=f->st;
- delete aux;
- }
- }
- void sterg(nod *&p, int k)
- {
- nod *aux;
- if(p!=NULL)
- if(p->inf==k)
- if(p->st==0&&p->dr==0) ///daca e nod terminal
- {
- delete p;
- p=0;
- }
- else
- if(p->st==0) ///are numai subordonat drept
- {
- aux=p->dr;
- delete p;
- p=aux;
- }
- else
- if(p->dr==0) ///are numai subordonat drept
- {
- aux=p->st;
- delete p;
- p=aux;
- }
- else
- cmd(p,p->st); ///are ambii subordonati
- else
- if(p->inf!=k)
- sterg(p->dr,k);
- else
- sterg(p->st,k);
- else
- cout<<"valoarea de sters nu se gaseste in arbore ";
- }
- int main()
- {
- rad=NULL;
- while(!in.eof())
- {int info;
- in>>info;
- creare(rad,info);}
- SRD(rad);
- int x;
- cout<<endl<<"valoarea de sters ";
- cin>>x;
- cout<<endl;
- sterg(rad,x);
- cout<<endl<<"arborele contine dupa stergere urmatoarele noduri "<<endl;
- SRD(rad);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement