Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef int Data;
  5.  
  6. struct Node {
  7. Data val; // данные в узле
  8. struct Node * left; // левый ребенок
  9. size_t freq;
  10. struct Node * right; // правый ребенок
  11. };
  12.  
  13. struct queue {
  14. struct Node * Data;
  15. size_t size;
  16. size_t start;
  17. size_t capacity;
  18. };
  19.  
  20. struct queue Q;
  21. size_t N = 0;
  22.  
  23. void Q_construct (struct queue * Q);
  24. void Q_destruct (struct queue * Q);
  25. void Q_realloc (struct queue * Q);
  26. void Q_push (struct queue * Q, struct Node t);
  27. struct Node Q_pop (struct queue * Q);
  28. void tree_floors (struct Node * tree);
  29.  
  30. struct Node * tree_add (struct Node * tree, Data x);
  31. void tree_print (struct Node * tree);
  32. void tree_destroy (struct Node * tree);
  33. size_t tree_height (struct Node * tree, size_t curHeight);
  34. void tree_freq (struct Node * tree);
  35.  
  36. int main()
  37. {
  38. Q_construct (&Q);
  39. struct Node * tree=NULL;
  40. Data tmp = 0;
  41. scanf ("%d", &tmp);
  42. while (tmp != 0) {
  43. N++;
  44. tree = tree_add (tree, tmp);
  45. scanf ("%d", &tmp);
  46. }
  47. tree_floors (tree);
  48. tree_destroy (tree);
  49. Q_destruct (&Q);
  50. return 0;
  51. }
  52.  
  53. struct Node * tree_add (struct Node * tree, Data x)
  54. {
  55. if (tree == NULL) {
  56. tree = (struct Node *) calloc (1, sizeof (struct Node));
  57. tree->val = x;
  58. tree->left = NULL;
  59. tree->right = NULL;
  60. tree->freq = 1;
  61. return tree;
  62. }
  63. if (x > tree->val) {
  64. tree->right = tree_add (tree->right, x);
  65. }
  66. if (x < tree->val) {
  67. tree->left = tree_add (tree->left, x);
  68. }
  69. if (x == tree->val) {
  70. tree->freq += 1;
  71. N--;
  72. }
  73. return tree;
  74. }
  75.  
  76. void tree_print (struct Node * tree)
  77. {
  78. if (tree == NULL)
  79. return;
  80. tree_print (tree->left);
  81. printf ("%d ", tree->val);
  82. tree_print (tree->right);
  83. }
  84.  
  85. void tree_destroy (struct Node * tree)
  86. {
  87. if (tree == NULL)
  88. return;
  89. tree_destroy (tree->left);
  90. tree_destroy (tree->right);
  91. free (tree);
  92. }
  93.  
  94. size_t tree_height (struct Node * tree, size_t curHeight)
  95. {
  96. if (tree == NULL)
  97. return curHeight;
  98. size_t H1, H2;
  99. H1 = tree_height (tree->left, curHeight + 1);
  100. H2 = tree_height (tree->right, curHeight + 1);
  101. if (H1 > H2)
  102. return H1;
  103. return H2;
  104. }
  105.  
  106. void tree_freq (struct Node * tree)
  107. {
  108. if (tree == NULL)
  109. return;
  110. tree_freq (tree->left);
  111. printf ("%d %ld\n", tree->val, tree->freq);
  112. tree_freq (tree->right);
  113. }
  114.  
  115. void tree_floors (struct Node * tree)
  116. {
  117. Q_push (&Q, *tree);
  118. while (N) {
  119. struct Node TR = Q_pop (&Q);
  120. printf ("%d ", TR.val);
  121. if (TR.left != NULL)
  122. Q_push (&Q, *(TR.left));
  123. if (TR.right != NULL)
  124. Q_push (&Q, *(TR.right));
  125. N--;
  126. }
  127. return;
  128. }
  129.  
  130. void Q_construct (struct queue * Q)
  131. {
  132. Q->Data = (struct Node *) calloc (1000, sizeof (struct Node));
  133. Q->capacity = 1000;
  134. Q->size = 0;
  135. Q->start = 0;
  136. }
  137.  
  138. void Q_destruct (struct queue * Q)
  139. {
  140. free(Q->Data);
  141. }
  142.  
  143. void Q_realloc (struct queue * Q)
  144. {
  145. size_t New_Size = Q->capacity + 1000;
  146. struct Node * New_Data = (struct Node *) calloc (New_Size, sizeof (struct Node));
  147.  
  148. for (size_t i = Q->start; i < Q->capacity; i++) {
  149. *(New_Data + i - Q->start) = *(Q->Data + i);
  150. }
  151. free (Q->Data);
  152. Q->Data = New_Data;
  153. Q->capacity = New_Size;
  154. Q->start = 0;
  155. Q->size -= Q->start;
  156. return 0;
  157. }
  158.  
  159. void Q_push (struct queue * Q, struct Node t)
  160. {
  161. if (Q->size >= Q->capacity - 1)
  162. Q_realloc (Q);
  163. *(Q->Data + Q->size) = t;
  164. Q->size++;
  165. }
  166. struct Node Q_pop (struct queue * Q)
  167. {
  168. Q->start++;
  169. return (*(Q->Data + Q->start - 1));
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement