Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream in ("date.in");
- ofstream out ("date.out");
- struct nod{ int info,frecv=1;
- nod *st, *dr;};
- void adauga(nod *&r, int nr)
- { if(!r)
- { r=new nod; r->info=nr; r->st=0; r->dr=0;}
- else
- if(nr<r->info) adauga(r->st,nr);
- else
- if(nr>r->info) adauga(r->dr,nr);
- else r->frecv++;
- }
- void SRD(nod *r)
- {
- if(r->st) SRD(r->st);
- out<<r->info<<" "<<r->frecv<<endl;
- if(r->dr) SRD(r->dr);
- }
- nod *cautare(nod *r, int x)
- {
- if(!r) return NULL;
- if(r->info==x) return r;
- if(r->info<x) return cautare(r->dr,x);
- return cautare(r->st,x);
- }
- nod *maxim(nod *r)
- {
- if(r)
- {
- if(!r->dr) return r;
- else return maxim(r->dr);
- }
- else return NULL; //arbore vid
- }
- nod *minim(nod *r)
- {
- if(r)
- {
- if(!r->st) return r;
- else return minim(r->st);
- }
- else return NULL;
- }
- int main()
- {
- nod *r=NULL;
- int nr,val,n;
- in>>n;
- for(int i=1;i<=n;i++)
- { in>>nr; adauga(r,nr);}
- nod *aux=maxim(r);
- cout<<"Maxim: "<<aux->info<<endl;
- aux=minim(r);
- cout<<"Minim: "<<aux->info<<endl;
- cout<<"Introduceti numarul de cautat: ";
- cin>>val;
- if(cautare(r,val)) cout<<"Nr este memorat";
- else cout<<"Nu e, sorry boss";
- SRD(r);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement