Advertisement
Guest User

Untitled

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