Advertisement
hegemon88676

Parcurgere arbori RSD SRD SDR

May 4th, 2018
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. ifstream f("arbore.in");
  5. int S[20], D[20], n, r;
  6. void RSD(int nod)
  7. {
  8.     if(nod)
  9.     {
  10.         cout<<nod<<" ";
  11.         RSD(S[nod]);
  12.         RSD(D[nod]);
  13.     }
  14. }
  15. void SRD(int nod)
  16. {
  17.     if(nod)
  18.     {
  19.         SRD(S[nod]);
  20.         cout<<nod<<" ";
  21.         SRD(D[nod]);
  22.     }
  23. }
  24. void SDR(int nod)
  25. {
  26.     if(nod)
  27.     {
  28.         SDR(S[nod]);
  29.         SDR(D[nod]);
  30.         cout<<nod<<" ";
  31.     }
  32. }
  33. int main()
  34. {
  35.     int i,j, OK;
  36.     f>>n;
  37.     for(i=1; i<=n; ++i)
  38.         f>>S[i];
  39.     for(j=1; j<=n; ++j)
  40.         f>>D[j];
  41.     ///caut radacina
  42.     for(i=1; i<=n; i++)
  43.     {
  44.         OK=0;
  45.         ///presupun ca i 'apartine taiat' S[], i 'apartine taiat' D[] <=> i este radacina
  46.         for(j=1; j<=n; j++)
  47.             if(S[j]==i||D[j]==i)
  48.                 OK=1;
  49.         if(!OK)
  50.             r=i;
  51.     }
  52.     cout<<"RSD:";
  53.     RSD(r);
  54.     cout<<"\nSRD:";
  55.     SRD(r);
  56.     cout<<"\nSDR:";
  57.     SDR(r);
  58. }
  59. /*
  60. abore.in
  61. 7
  62. 0 3 5 1 0 0 0
  63. 0 4 7 0 0 0 6
  64. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement