Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <iostream>
- # include <conio.h>
- #include <stdlib.h>
- using namespace std;
- // arbore binar de cautare
- struct nod
- {
- int n;
- nod *leg_st;
- nod *leg_dr;
- };
- nod *prim,*aux,*aux2, *elem;
- int j,z=0;
- nod*cauta_poz(nod*elem,int x)
- {if(x<elem->n)
- {if(elem->leg_st==NULL)
- return elem;
- else
- return cauta_poz(elem->leg_st,x);
- }
- else
- if(elem->leg_dr==NULL)
- return elem;
- else
- return cauta_poz(elem->leg_dr,x);
- }
- void numarare_pare(nod *rad)
- {
- if(rad!=NULL)
- {if(rad->n%2==0)
- {cout<<rad->n<<",";
- z=z+1;}
- numarare_pare(rad->leg_st);
- numarare_pare(rad->leg_dr);
- }
- }
- void preordine(nod *rad)
- {
- if(rad!=NULL)
- {cout<<rad->n;
- preordine(rad->leg_st);
- preordine(rad->leg_dr);
- }
- }
- void inordine(nod *rad)
- {
- if(rad!=NULL)
- {inordine(rad->leg_st);
- cout<<rad->n;
- inordine(rad->leg_dr);
- }
- }
- void postordine(nod *rad)
- {
- if(rad!=NULL)
- {inordine(rad->leg_st);
- inordine(rad->leg_dr);
- cout<<rad->n;
- }
- }
- nod* cauta_val(nod *rad,int s)
- {
- if(rad!=NULL)
- if(rad->n==s)
- return rad;
- else
- if(s<rad->n)
- return cauta_val(rad->leg_st,s);
- else
- cauta_val(rad->leg_dr,s);
- else
- return 0;}
- int main()
- {
- int x,i,w,k,j,V[100],t=1,l;
- cout<<"introduceti valoarea care doriti sa o cautati=";
- cin>>w;
- cout<<endl;
- prim=new nod;
- cout<<"valoare radacina= ";
- cin>>prim->n;
- prim->leg_st=NULL;
- prim->leg_dr=NULL;
- cout<<endl;
- for(i=1;i<=7;i++)
- {cout<<"dati urmatorul element:";
- aux=new nod;
- cin>>aux->n;
- aux->leg_st=aux->leg_dr=NULL;
- aux2=cauta_poz(prim,aux->n);
- if(aux->n<aux2->n)
- aux2->leg_st=aux;
- else
- aux2->leg_dr=aux;
- cout<<endl;
- }
- cout<<"afisare valori pare:";
- numarare_pare(prim);
- cout<<endl;
- cout<<"totalul numerelor pare=";
- cout<<z<<endl;
- cout<<"afisare preordine:";
- preordine(prim);
- cout<<endl;
- cout<<"afisare inordine:";
- inordine(prim);
- cout<<endl;
- cout<<"afisare postordine:";
- postordine(prim);
- cout<<endl;
- cout<<"afisare nr prime:";
- cout<<endl;
- cout<<"rezultat cautare:";
- nod* cauta_val(nod *prim,int w);
- cout<<endl;
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement