Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.67 KB | None | 0 0
  1. #ifdef _MSC_VER
  2.     // Annex K нинужен. Блядские ворнинги - тем более.
  3.     #define _CRT_SECURE_NO_WARNINGS
  4. #endif
  5.  
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10.  
  11. //дерево
  12. typedef struct bTREE
  13. {
  14.     int inf;
  15.     struct bTREE* left;
  16.     struct bTREE* right;
  17. }TREE;
  18. //очередь
  19. typedef struct qQUEUE
  20. {
  21.     TREE *tr;
  22.     struct qQUEUE *next;
  23. }QUEUE;
  24. QUEUE *q;
  25. //функция добавления элемента в очередь
  26. void Put(TREE *a)
  27. {
  28.     QUEUE *n, *prev;
  29.     prev = q;
  30.     n = malloc(sizeof(QUEUE));
  31.     n->tr = a;
  32.     n->next = NULL;
  33.     if (q != NULL)
  34.     {
  35.         while (prev->next != NULL)
  36.             prev = prev->next;
  37.         prev->next = n;
  38.     }
  39.     else
  40.         q = n;
  41. }
  42. //функция удаления элемента из очереди
  43. TREE* Get(void)
  44. {
  45.     QUEUE *prev;
  46.     TREE *a;
  47.     if (q != NULL)
  48.     {
  49.         prev = q;
  50.         a = q->tr;
  51.         if (q->next != NULL)
  52.             q = q->next;
  53.         else
  54.             q = NULL;
  55.         free(prev);
  56.         return a;
  57.     }
  58.     else
  59.         return NULL;
  60. }
  61. //функция проверки на пустоту очереди
  62. int Empty(QUEUE *q)
  63. {
  64.     if (q == NULL) return 1;
  65.     else return 0;
  66. }
  67. //функция добавления элемента в дерево
  68. TREE* Add(TREE *root, int x)
  69. {
  70.     if (root == NULL)
  71.     {
  72.         root = malloc(sizeof(TREE));
  73.         root->inf = x;
  74.         root->left = root->right = NULL;
  75.     }
  76.     else {
  77.         if (x <= root->inf)
  78.             root->left = Add(root->left, x);
  79.         else
  80.             root->right = Add(root->right, x);
  81.     }
  82.     return root;
  83. }
  84.  
  85.  
  86. //функция вывода дерева на экран обходя дерево в ширину
  87. int ShowTree(TREE *tree, size_t target_depth)
  88. {
  89.     TREE *node;
  90.     size_t depth = 0;
  91.  
  92.     Put(tree);
  93.     Put(NULL);
  94.  
  95.     while (1)
  96.     {
  97.         if (depth == target_depth) {
  98.             // Находимся на нужном уровне? Выводим содержимое очереди. В конце
  99.             // будет NULL, проверка на Empty избыточна.
  100.             size_t count = 0;
  101.             printf("\nList of elements at depth == %zu: ", target_depth);
  102.             while ((node = Get()) != NULL) {
  103.                 printf("%d ", node->inf);
  104.                 count++;
  105.             }
  106.             printf("\n");
  107.             return count;
  108.         }
  109.  
  110.         node = Get();
  111.         if (node == NULL) {
  112.             printf("(at depth %zu)\n", depth);
  113.  
  114.             // У нас закончились элементы для текущей глубины, но ни у одного
  115.             // из них не было потомков? Значит, мы уже не сможем опуститься до
  116.             // запрашиваемой глубины, поэтому выходим нахуй, чтобы не попасть
  117.             // в бесконечный цикл.
  118.             if (Empty(q)) {
  119.                 return 0;
  120.             }
  121.  
  122.             Put(NULL);
  123.             depth++;
  124.         } else {
  125.             if (node->left != NULL)
  126.                 Put(node->left);
  127.             if (node->right != NULL)
  128.                 Put(node->right);
  129.             printf("%d ", node->inf);
  130.         }
  131.     }
  132. }
  133.  
  134. int main(void)
  135. {
  136.     TREE *tree = NULL;
  137.     static int tree_data[] = { 13, 10, 5, 12, 20, 15, };
  138.     for (size_t i = 0; i < sizeof(tree_data) / sizeof(tree_data[0]); i++) {
  139.         tree = Add(tree, tree_data[i]);
  140.     }
  141.  
  142.     printf("Number of elements at given depth = %zu\n", ShowTree(tree, 2));
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement