Advertisement
Guest User

Untitled

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