Advertisement
nicuvlad76

Untitled

Feb 15th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 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. int info;
  11. nod * st, * dr;
  12. };
  13.  
  14. struct elem{
  15. nod * adr;
  16. elem * next;
  17. };
  18.  
  19. void citire(nod * & p)
  20. {
  21. int x;
  22. fin >> x;
  23. if(x == 0)
  24. p = NULL;
  25. else
  26. {
  27. p = new nod;
  28. p -> info = x;
  29. citire(p -> st);
  30. citire(p -> dr);
  31. }
  32. }
  33.  
  34. void Adauga(nod * ce, elem * & ultim)
  35. {
  36. elem * aux = new elem;
  37. aux -> adr = ce;
  38. aux -> next = NULL;
  39. ultim -> next = aux;
  40. ultim = aux;
  41. }
  42.  
  43. void Elimina(elem * & prim)
  44. {
  45. elem * aux = prim;
  46. prim = prim -> next;
  47. delete aux;
  48. }
  49.  
  50. void BFS(nod * p)
  51. {
  52. elem * prim, * ultim;
  53. prim = new elem;
  54. ultim = prim;
  55. prim -> next = NULL;
  56. prim -> adr = p;
  57. while(prim != NULL)
  58. {
  59. nod * aux = prim -> adr;
  60. fout << aux -> info << " ";
  61. if(aux -> st != NULL)
  62. Adauga(aux -> st , ultim);
  63. if(aux -> dr != NULL)
  64. Adauga(aux -> dr , ultim);
  65. Elimina(prim);
  66. }
  67. }
  68.  
  69. void stergeTot(nod * & p)
  70. {
  71. if(p != NULL)
  72. {
  73. stergeTot(p -> st);
  74. stergeTot(p -> dr);
  75. delete p;
  76. p = NULL;
  77. }
  78. }
  79.  
  80. int main()
  81. {
  82. nod * p = NULL;
  83. citire(p);
  84. BFS(p);
  85. stergeTot(p);
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement