Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <fstream>
  4. #include <math.h>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. ifstream in("arbori.in");
  9. ofstream out("arbori.out");
  10.  
  11. struct nod{
  12. int info;
  13. nod* st;
  14. nod* dr;
  15. };
  16.  
  17. struct per{
  18. int nod;
  19. int inaltime;
  20. };
  21.  
  22. per perchi[100];
  23. int index = 0;
  24.  
  25. void creare(nod*& root);
  26. void sumPare(nod*& root);
  27. void Frunze(nod*& root);
  28. int inaltime(nod*& root);
  29. void noduriPeNivel(nod*& root, int k);
  30.  
  31. nod *root;
  32. int s = 0;
  33.  
  34. int main(){
  35.  
  36. int a;
  37. creare(root);
  38. sumPare(root);
  39. out<<s;
  40. out<<endl;
  41. Frunze(root);
  42. out<<(inaltime(root)) - 1;
  43. out<<endl;
  44. int iMax = inaltime(root);
  45.  
  46. noduriPeNivel(root, 0);
  47.  
  48. int nivel = 0;
  49. for(int i = 0; i < index; i++){
  50. if(iMax < nivel - 1)
  51. break;
  52. out<<nivel<<" n: ";
  53.  
  54. for(int j = 0; j < index; j++){
  55. if(nivel == perchi[j].inaltime){
  56. out<<perchi[j].nod<<" ";
  57. }
  58. }
  59. nivel++;
  60. out<<endl;
  61. }
  62.  
  63.  
  64. return 0;
  65. }
  66.  
  67. void creare(nod*& root){
  68. int x;
  69. in>>x;
  70. if(x != 0){
  71. root = new nod;
  72. root->info = x;
  73. creare(root->st);
  74. creare(root->dr);
  75. } else {
  76. root = NULL;
  77. }
  78. }
  79.  
  80. void sumPare(nod*& root){
  81. if(root == NULL){
  82. return;
  83. }
  84.  
  85. if(root->info % 2 == 0){
  86. s+=root->info;
  87. }
  88. sumPare(root->st);
  89. sumPare(root->dr);
  90.  
  91. }
  92.  
  93. void Frunze(nod*& root){
  94. if(root == NULL){
  95. return;
  96. }
  97.  
  98. if(root->st == NULL && root->dr == NULL){
  99. out<<root->info<<" ";
  100. return;
  101. }
  102. Frunze(root->st);
  103. Frunze(root->dr);
  104. }
  105.  
  106. int inaltime(nod*& root){
  107. if(root == NULL){
  108. return 0;
  109. }
  110.  
  111. return 1 + max(inaltime(root->st) , inaltime(root->dr));
  112.  
  113. }
  114.  
  115. void noduriPeNivel(nod*& root, int k){
  116. if(root == NULL){
  117. return;
  118. }
  119.  
  120. per p;
  121. p.inaltime = k;
  122. p.nod = root->info;
  123.  
  124. perchi[index++] = p;
  125.  
  126. noduriPeNivel(root->st, k + 1);
  127. noduriPeNivel(root->dr, k + 1);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement