Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct nod
- {
- int cheie;
- nod *st, *dr;
- };
- nod *rad, *p;
- int val;
- void adauga(nod *&c, int val)
- {
- if(c != 0)
- if(c->cheie == val)
- cout<<"Acest nod exista deja!";
- else if(c->cheie < val)
- adauga(c->dr, val);
- else
- adauga(c->st, val);
- else
- {
- c = new nod;
- c->cheie = val;
- c->st = 0;
- c->dr = 0;
- }
- }
- void preord (nod *p)
- {
- cout<<p->cheie<<" ";
- if(p->st != 0) preord(p->st);
- if(p->dr != 0) preord(p->dr);
- }
- void inord (nod *p)
- {
- if(p->st != 0) inord(p->st);
- cout<<p->cheie<<" ";
- if(p->dr != 0) inord(p->dr);
- }
- void postord (nod *p)
- {
- if(p->st != 0) postord(p->st);
- if(p->dr != 0) postord(p->dr);
- cout<<p->cheie<<" ";
- }
- void cauta (nod *p, int x)
- {
- if(p==NULL)
- cout<<"Nu exista informatia in arbore.\n";
- else if(x < p->cheie)
- cauta(p->st, x);
- else if(x > p->cheie)
- cauta(p->dr, x);
- else
- cout<<"Nodul cautata a fost gasit.\n";
- }
- int main()
- {
- rad=NULL;
- do
- {
- cout<<"Dati valoarea: "; cin>>val;
- if(val != 0) adauga(rad, val);
- }
- while(val != 0);
- cout<<"\n Parcurgearea in preordine: "<<endl;
- preord(rad);
- cout<<"\n Parcurgerea in inordine: "<<endl;
- inord(rad);
- cout<<endl;
- cout<<"\n Parcurgerea in postordine: "<<endl;
- postord(rad);
- cout<<endl;
- cout<<"\n Informatia de cautat: "; cin>>val;
- cauta(rad, val);
- cout<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement