Advertisement
Dafuq313

Abc

May 27th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ifstream in ("date.in");
  5. ofstream out ("date.out");
  6. struct nod{ int info,frecv=1;
  7. nod *st, *dr;};
  8.  
  9. void adauga(nod *&r, int nr)
  10. {   if(!r)
  11.     {   r=new nod; r->info=nr; r->st=0; r->dr=0;}
  12.     else
  13.         if(nr<r->info)  adauga(r->st,nr);
  14.         else
  15.             if(nr>r->info)  adauga(r->dr,nr);
  16.             else r->frecv++;
  17. }
  18.  
  19. void SRD(nod *r)
  20. {
  21.     if(r->st)   SRD(r->st);
  22.     out<<r->info<<" "<<r->frecv<<endl;
  23.     if(r->dr)   SRD(r->dr);
  24. }
  25.  
  26. nod *cautare(nod *r, int x)
  27. {
  28.     if(!r)  return NULL;
  29.     if(r->info==x)  return r;
  30.     if(r->info<x)   return cautare(r->dr,x);
  31.     return cautare(r->st,x);
  32. }
  33.  
  34. nod *maxim(nod *r)
  35. {
  36.     if(r)
  37.     {
  38.         if(!r->dr)  return r;
  39.         else return maxim(r->dr);
  40.     }
  41.     else return NULL; //arbore vid
  42. }
  43.  
  44. nod *minim(nod *r)
  45. {
  46.     if(r)
  47.     {
  48.         if(!r->st)  return r;
  49.         else return minim(r->st);
  50.     }
  51.     else return NULL;
  52. }
  53.  
  54. int main()
  55. {
  56.     nod *r=NULL;
  57.     int nr,val,n;
  58.     in>>n;
  59.     for(int i=1;i<=n;i++)
  60.     {   in>>nr; adauga(r,nr);}
  61.     nod *aux=maxim(r);
  62.     cout<<"Maxim: "<<aux->info<<endl;
  63.     aux=minim(r);
  64.     cout<<"Minim: "<<aux->info<<endl;
  65.     cout<<"Introduceti numarul de cautat: ";
  66.     cin>>val;
  67.     if(cautare(r,val))  cout<<"Nr este memorat";
  68.     else cout<<"Nu e, sorry boss";
  69.     SRD(r);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement