ioana_martin98

Untitled

Oct 17th, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include<fstream.h>
  2. ifstream f("arbore.in");
  3. #include<iostream.h>
  4. typedef struct nod
  5. {
  6. int inf;
  7. nod *st,*dr;
  8. }ARB;
  9. void SRD(ARB *r)
  10. {
  11. if(r)
  12. {
  13. SRD(r->st);
  14. cout<<r->inf<<" ";
  15. SRD(r->dr);
  16. }
  17. }
  18. void DRS(ARB *r)
  19. {
  20. if(r)
  21. {
  22. DRS(r->dr);
  23. cout<<r->inf<<" ";
  24. DRS(r->st);
  25. }
  26. }
  27. void inserare(ARB*&r,int x)
  28. {
  29. if(r)
  30. {
  31. if(x<r->inf)
  32. inserare(r->st,x);
  33. else
  34. inserare(r->dr,x);
  35. }
  36. else
  37. {
  38. r=new ARB;
  39. r->inf=x;
  40. r->st=NULL;
  41. r->dr=NULL;
  42. }
  43. }
  44. int main()
  45. {
  46. int n,x,i;
  47. ARB *rad;
  48. rad=NULL;
  49. f>>n;
  50. for(i=1;i<=n;i++)
  51. {
  52. f>>x;
  53. inserare(rad,x);
  54. }
  55. //ordonare crescatoare
  56. cout<<"Ordonare crescatoare: ";
  57. SRD(rad);
  58. cout<<endl;
  59. //ordonare descrescatoare
  60. cout<<"Ordonare descrescatoare: ";
  61. DRS(rad);
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment