Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. # include <iostream>
  2. # include <conio.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5. // arbore binar de cautare
  6. struct nod
  7. {
  8. int n;
  9. nod *leg_st;
  10. nod *leg_dr;
  11. };
  12. nod *prim,*aux,*aux2, *elem;
  13.  
  14. int j,z=0;
  15.  
  16. nod*cauta_poz(nod*elem,int x)
  17. {if(x<elem->n)
  18. {if(elem->leg_st==NULL)
  19. return elem;
  20. else
  21. return cauta_poz(elem->leg_st,x);
  22. }
  23. else
  24. if(elem->leg_dr==NULL)
  25. return elem;
  26. else
  27. return cauta_poz(elem->leg_dr,x);
  28.  
  29. }
  30.  
  31. void numarare_pare(nod *rad)
  32. {
  33. if(rad!=NULL)
  34. {if(rad->n%2==0)
  35. {cout<<rad->n<<",";
  36. z=z+1;}
  37. numarare_pare(rad->leg_st);
  38. numarare_pare(rad->leg_dr);
  39. }
  40. }
  41.  
  42.  
  43.  
  44. void preordine(nod *rad)
  45. {
  46. if(rad!=NULL)
  47. {cout<<rad->n;
  48. preordine(rad->leg_st);
  49. preordine(rad->leg_dr);
  50. }
  51. }
  52.  
  53. void inordine(nod *rad)
  54. {
  55. if(rad!=NULL)
  56. {inordine(rad->leg_st);
  57. cout<<rad->n;
  58. inordine(rad->leg_dr);
  59. }
  60. }
  61.  
  62. void postordine(nod *rad)
  63. {
  64. if(rad!=NULL)
  65. {inordine(rad->leg_st);
  66. inordine(rad->leg_dr);
  67. cout<<rad->n;
  68. }
  69. }
  70.  
  71. nod* cauta_val(nod *rad,int s)
  72. {
  73. if(rad!=NULL)
  74.  
  75. if(rad->n==s)
  76. return rad;
  77. else
  78. if(s<rad->n)
  79. return cauta_val(rad->leg_st,s);
  80. else
  81. cauta_val(rad->leg_dr,s);
  82. else
  83. return 0;}
  84.  
  85.  
  86. int main()
  87. {
  88. int x,i,w,k,j,V[100],t=1,l;
  89. cout<<"introduceti valoarea care doriti sa o cautati=";
  90. cin>>w;
  91. cout<<endl;
  92. prim=new nod;
  93. cout<<"valoare radacina= ";
  94. cin>>prim->n;
  95. prim->leg_st=NULL;
  96. prim->leg_dr=NULL;
  97. cout<<endl;
  98. for(i=1;i<=7;i++)
  99. {cout<<"dati urmatorul element:";
  100. aux=new nod;
  101. cin>>aux->n;
  102. aux->leg_st=aux->leg_dr=NULL;
  103. aux2=cauta_poz(prim,aux->n);
  104. if(aux->n<aux2->n)
  105. aux2->leg_st=aux;
  106. else
  107. aux2->leg_dr=aux;
  108. cout<<endl;
  109. }
  110. cout<<"afisare valori pare:";
  111. numarare_pare(prim);
  112. cout<<endl;
  113. cout<<"totalul numerelor pare=";
  114. cout<<z<<endl;
  115. cout<<"afisare preordine:";
  116. preordine(prim);
  117. cout<<endl;
  118. cout<<"afisare inordine:";
  119. inordine(prim);
  120. cout<<endl;
  121. cout<<"afisare postordine:";
  122. postordine(prim);
  123. cout<<endl;
  124. cout<<"afisare nr prime:";
  125. cout<<endl;
  126. cout<<"rezultat cautare:";
  127. nod* cauta_val(nod *prim,int w);
  128. cout<<endl;
  129. getch();
  130. return 0;
  131.  
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement