Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. ifstream fin("arbore.in");
  5. int n,t[20],i,r,h,niv,x,a,j;
  6. int radacina()
  7. {
  8. for(i=1;i<=n;i++)
  9. if(t[i]==0)
  10. {
  11. return i;
  12. }
  13. }
  14. void frunze()
  15. {
  16. int j,ok;
  17. for(i=1;i<=n;i++)
  18. {
  19. ok=0;
  20. for(j=1;j<=n;j++)
  21. if(t[j]==i)
  22. {
  23. ok=1;
  24. break;
  25. }
  26. if(ok==0)
  27. cout<<i<<" ";
  28. }
  29. }
  30. void neterminale()
  31. {
  32. int j,ok;
  33. for(i=1;i<=n;i++)
  34. {
  35. ok=0;
  36. for(j=1;j<=n;j++)
  37. if(t[j]==i)
  38. {
  39. ok=1;
  40. break;
  41. }
  42. if(ok==1)
  43. cout<<i<<" ";
  44. }
  45. }
  46. void inaltime(int x,int l)
  47. {
  48. int i;
  49. if(l>h)
  50. h=l;
  51. for(i=1;i<=n;i++)
  52. if(t[i]==x )
  53. inaltime(i,l+1);
  54.  
  55. }
  56. void nivele(int x,int l,int a)
  57. {
  58. int i;
  59. if(l==a)
  60. cout<<x<<" ";
  61. for(i=1;i<=n;i++)
  62. if(t[i]==x )
  63. nivele(i,l+1,a);
  64.  
  65.  
  66. }
  67. void descendenti_directi(int x)
  68. {
  69. for(i=1;i<=n;i++)
  70. if(t[i]==x)
  71. cout<<i<<" ";
  72. }
  73. void descendentii(int x)
  74. {
  75. int i;
  76. for(i=1;i<=n;i++)
  77. if(t[i]==x )
  78. {
  79. cout<<i<<" ";
  80. descendentii(i);
  81. }
  82. }
  83. void ascendentii(int x)
  84. {
  85. int i;
  86. if(t[x]!=0)
  87. {
  88. cout<<t[x]<<" ";
  89. ascendentii(t[x]);
  90. }
  91. }
  92. int main()
  93. {
  94. fin>>n;
  95. for(i=1;i<=n;i++)
  96. fin>>t[i];
  97. r=radacina();
  98. cout<<"Radacina este "<<r<<endl;
  99. cout<<"Frunzele sunt ";
  100. frunze();
  101. cout<<endl;
  102. cout<<"Nodurile neterminale sunt ";
  103. neterminale();
  104. cout<<endl;
  105. inaltime(r,0);
  106. cout<<"Inaltimea arborelui este "<<h<<endl;
  107. for(j=0;j<=h;j++)
  108. {
  109. cout<<endl;
  110. cout<<"Nivelul "<<j<<" : ";
  111. nivele(r,0,j);
  112.  
  113. }
  114. cout<<endl<<"Nodul este ";
  115. cin>>x;
  116. cout<<"Descendentii directi a lui "<<x<<" sunt ";
  117. descendenti_directi(x);
  118. cout<<endl;
  119. cout<<"Toti descendentii lui "<<x<<" sunt ";
  120. descendentii(x);
  121. cout<<endl<<"Ascendentii lui "<<x<<" sunt ";
  122. ascendentii(x);
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement