Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exercitii la arbori binari de cautare
- Pagina 322
- 5) #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream f("fractii.in");
- struct nod {
- int x,y;
- nod *st,*dr;
- };
- nod *rad;
- void creare(nod*&p)
- {
- int x,y;
- f >> x >> y;
- if(x==0&&y==0)
- p=NULL;
- else
- {
- p = new nod;
- p->x=x;
- p->y=y;
- // cout << p->x << " " << p->y <<endl;
- creare(p->st);
- creare(p->dr);
- }
- }
- int cmmdc(int a, int b)
- {
- int t;
- while (b != 0)
- {
- t=b;
- b=a%b;
- a=t;
- }
- return a;
- }
- void simplificare(nod*p)
- {
- if(p!=NULL)
- {
- int aux=cmmdc(p->x,p->y);
- p->x/=aux;
- p->y/=aux;
- simplificare(p->st);
- simplificare(p->dr);
- }
- }
- void SRD(nod*p)
- {
- if(p!=NULL)
- {
- SRD(p->st);
- cout << p->x << "/" << p->y<<endl;
- SRD(p->dr);
- }
- }
- int main()
- {
- rad=NULL;
- creare(rad);
- cout << "Arbore inainte de simplificare: "<<endl;
- SRD(rad);
- cout << "Arbore dupa simplificare: "<<endl;
- simplificare(rad);
- SRD(rad);
- }
- 6) #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream f("arbore.in");
- struct nod {
- int inf;
- nod *st,*dr;
- };
- nod *rad;
- int n[100],nr;
- void creare(nod*&p)
- {
- int x;
- f >> x;
- if(x==0)
- p=NULL;
- else
- {
- p = new nod;
- p->inf=x;
- creare(p->st);
- creare(p->dr);
- }
- }
- int ok;
- void frati(nod*p,int x)
- {
- if(p!=NULL && p->st!=NULL && p->dr!=NULL)
- {
- if(p->st->inf==x)
- {
- cout << "Fratele drept este: " << p->dr->inf <<endl;
- ok=1;
- }
- else
- if(p->dr->inf==x)
- {
- cout << "Fratele stang este: " << p->st->inf <<endl;
- ok=1;
- }
- frati(p->st,x);
- frati(p->dr,x);
- }
- }
- int main()
- {
- int x;
- rad=NULL;
- creare(rad);
- cout << "x= ";
- cin >> x;
- frati(rad,x);
- if(ok==0)
- cout << "nu exista";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement