Advertisement
a53

BiLatime

a53
Dec 28th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #define INFINIT 2000000000
  4. using namespace std;
  5.  
  6. ifstream fin("bilatime.in");
  7. ofstream fout("bilatime.out");
  8.  
  9. struct nod
  10. {
  11. int info;
  12. nod * st, * dr;
  13. };
  14.  
  15. struct elem
  16. {
  17. nod * adr;
  18. elem * next;
  19. };
  20.  
  21. void citire(nod * & p)
  22. {
  23. int x;
  24. fin >> x;
  25. if(x == 0)
  26. p = NULL;
  27. else
  28. {
  29. p = new nod;
  30. p -> info = x;
  31. citire(p -> st);
  32. citire(p -> dr);
  33. }
  34. }
  35.  
  36. void Adauga(nod * ce, elem * & ultim)
  37. {
  38. elem * aux = new elem;
  39. aux -> adr = ce;
  40. aux -> next = NULL;
  41. ultim -> next = aux;
  42. ultim = aux;
  43. }
  44.  
  45. void Elimina(elem * & prim)
  46. {
  47. elem * aux = prim;
  48. prim = prim -> next;
  49. delete aux;
  50. }
  51.  
  52. void BFS(nod * p)
  53. {
  54. elem * prim, * ultim;
  55. prim = new elem;
  56. ultim = prim;
  57. prim -> next = NULL;
  58. prim -> adr = p;
  59. while(prim != NULL)
  60. {
  61. nod * aux = prim -> adr;
  62. fout << aux -> info << " ";
  63. if(aux -> st != NULL)
  64. Adauga(aux -> st , ultim);
  65. if(aux -> dr != NULL)
  66. Adauga(aux -> dr , ultim);
  67. Elimina(prim);
  68. }
  69. }
  70.  
  71. void stergeTot(nod * & p)
  72. {
  73. if(p != NULL)
  74. {
  75. stergeTot(p -> st);
  76. stergeTot(p -> dr);
  77. delete p;
  78. p = NULL;
  79. }
  80. }
  81.  
  82. int main()
  83. {
  84. nod * p = NULL;
  85. citire(p);
  86. BFS(p);
  87. stergeTot(p);
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement