Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4. ifstream fin("arbore.in");
  5. ofstream fout("arbore.out");
  6.  
  7. int r, n, st[51], dr[51];
  8. void citire();
  9. {
  10. fin >> n >> r;
  11. for(int i = 1; i <= n; i++)
  12. fin >> st[i];
  13. for(i = 1; i <= n; i++)
  14. fin >> dr[i];
  15. }
  16. void preordine(int nod) {
  17. if(nod != 0) {
  18. fout << nod << ' ';
  19. preordine(st[nod]);
  20. preordine(dr[nod]);
  21. }
  22. }
  23. void inordine(int nod) {
  24. if(nod != 0) {
  25. inordine(st[nod]);
  26. fout << nod << ' ';
  27. inordine(dr[nod]);
  28. }
  29. }
  30. void postordine(int nod) {
  31. if(nod != 0) {
  32. postordine(st[nod]);
  33. postordine(dr[nod]);
  34. fout << nod << ' ';
  35. }
  36. }
  37. int main() {
  38. citire();
  39. fout << "preordine:";
  40. preordine(r);
  41. fout << "\n inordine:";
  42. inordine(r);
  43. fout << "\n postordine:";
  44. postordine(r);
  45.  
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement