Advertisement
zhenialeks

Untitled

May 24th, 2020
1,616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <stdbool.h>
  5.  
  6. struct tree{
  7.     int info;
  8.     struct tree *left;
  9.     struct tree *right;
  10. };
  11. struct q_node;
  12.  
  13. struct queue{
  14.     struct q_node *first;
  15.     struct q_node *last;
  16. };
  17.  
  18. struct q_node{
  19.     struct tree *ptr;
  20.     struct q_node *next;
  21. };
  22. /*
  23. //debug
  24. void print_queue(struct queue *q){
  25.     for (struct q_node *tmp = q->first; tmp != NULL; tmp = tmp->next)
  26.         printf("%d ", tmp->ptr->info);
  27.     printf("\n");
  28.     fflush(stdout);
  29. }
  30. //enddebug
  31. */
  32.  
  33. struct q_node *init_q_node(struct tree *ptr, struct q_node *next){
  34.     struct q_node *tmp = (struct q_node *) malloc(sizeof(struct q_node));
  35.     tmp->ptr = ptr;
  36.     tmp->next = next;
  37.     return tmp;
  38. }
  39.  
  40. void push_back(struct queue *q, struct q_node *node){
  41.  
  42.     if (q->first == NULL)
  43.         q->first = q->last = node;
  44.  
  45.     else {
  46.         q->last->next = node;
  47.         q->last = q->last->next;
  48.     }
  49. }
  50.  
  51.  
  52. struct q_node *pop(struct queue *q){
  53.     /*debug*/
  54. /*
  55.     printf("before:\n");
  56.     print_queue(q);
  57.  
  58. */
  59.  
  60.     if (q->first == NULL)
  61.         return NULL;
  62.  
  63.     struct q_node *cur_ptr = q->first;
  64.     q->first = q->first->next;
  65.  
  66.     if (cur_ptr == q->last)
  67.         q->last = NULL;
  68.  
  69.    /* printf("after:\n");
  70.     print_queue(q);*/
  71.     return cur_ptr;
  72. };
  73.  
  74. void BFS(struct queue *q){
  75.     while (q->first != NULL){
  76.         struct q_node *Node = pop(q);
  77.         struct tree *cur_el = (Node->ptr);
  78.  
  79.         if (cur_el->left != NULL)
  80.             push_back(q, init_q_node(cur_el->left, NULL));
  81.         if (cur_el->right != NULL)
  82.             push_back(q, init_q_node(cur_el->right, NULL));
  83.  
  84.         printf("%i ", Node->ptr->info);
  85. //        free(Node->ptr);
  86.         free(Node);
  87.     }
  88. }
  89.  
  90. struct tree *tree_init(int info){
  91.     struct tree *tmp = (struct tree*) malloc(sizeof(struct tree));
  92.     tmp->info = info;
  93.     tmp->left = NULL;
  94.     tmp->right = NULL;
  95.     return tmp;
  96. }
  97.  
  98. void Delete_tree(struct tree * root){
  99.     if (root->right != NULL)
  100.         Delete_tree(root->right);
  101.  
  102.     if (root->left != NULL)
  103.         Delete_tree(root->left);
  104.  
  105.     free(root);
  106. }
  107.  
  108. int main(){
  109.  
  110.     FILE *fin = fopen("input.txt", "r");
  111.  
  112.     int numb, key;
  113.     printf("print number of elements: \n");
  114.     fscanf(fin, "%i", &numb);
  115.  
  116.     printf("print key of your tree: \n");
  117.     fscanf(fin, "%i", &key);
  118.  
  119.     struct tree *tmp, *root;
  120.     root = tree_init(key);
  121.  
  122.     int count;
  123.     printf("print elements of your tree: \n");
  124.     for (int i = 0; i < numb; i++){
  125.         fscanf(fin, "%i", &count);
  126.         tmp = root;
  127.         while ( true ){
  128.             if (count >= tmp->info){
  129.                 if (tmp->right != NULL)
  130.                     tmp = tmp->right;
  131.                 else {
  132.                     tmp->right = tree_init(count);
  133.                     break;
  134.                 }
  135.             }
  136.             else {
  137.                 if (tmp->left != NULL)
  138.                     tmp = tmp->left;
  139.                 else {
  140.                     tmp->left = tree_init(count);
  141.                     break;
  142.                 }
  143.             }
  144.         }
  145.     }
  146.  
  147.     tmp = root;
  148.     struct queue *q = (struct queue*) malloc(sizeof(struct queue));
  149.     q->first = q->last = NULL;
  150.     push_back(q, init_q_node(tmp, NULL));
  151.     BFS(q);
  152.  
  153.     Delete_tree(root);
  154.     free(q);
  155.     getch();
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement