SHARE
TWEET

Untitled

a guest Apr 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4.  
  5. struct Author
  6. {
  7.     char* name;
  8.     int age;
  9. };
  10.  
  11. struct Book
  12. {
  13.     int id;// for searching the books by id
  14.     Author author;
  15.     int noPages;
  16. };
  17.  
  18. Book readBook(FILE* f)
  19. {
  20.     Book b;
  21.     fscanf(f, "%d", &b.id); //for read from the file
  22.     char buffer[20];
  23.     fscanf(f, "%s", buffer);
  24.     b.author.name = (char*)malloc(sizeof(char)*(strlen(buffer) + 1));
  25.     strcpy(b.author.name, buffer); //& for the buffer isn t necessary
  26.     fscanf(f, "%d", &b.author.age);
  27.     fscanf(f, "%d", &b.noPages);
  28.  
  29.     return b;
  30. }
  31.  
  32. void printBook(Book b)
  33. {
  34.     printf("%d. Book writed by %s has %d pages. %d\n ", b.id, b.author.name, b.noPages, b.author.age);
  35. }
  36.  
  37. struct NODE
  38. {
  39.     Book info;
  40.     NODE* left;
  41.     NODE* right;
  42. };
  43.  
  44. NODE* initNode(Book info, NODE* left, NODE* right)
  45. {
  46.     NODE* newNode = (NODE*)malloc(sizeof(NODE));
  47.     newNode->info = info;//shallow copy
  48.     newNode->left = left;
  49.     newNode->right = right;
  50.  
  51.     return newNode;
  52. }
  53.  
  54. NODE* insertTree(NODE* root, Book book)
  55. {
  56.     if (root) {
  57.         if (book.id < root->info.id)
  58.         {
  59.             root->left = insertTree(root->left, book);
  60.             return root;
  61.         }
  62.         else
  63.         {
  64.             root->right = insertTree(root->right, book);
  65.             return root;
  66.         }
  67.     }
  68.     else
  69.     {
  70.         return initNode(book, NULL, NULL);
  71.     }
  72. }
  73.  
  74. void printTree(NODE* root)
  75. {
  76.     if (root)
  77.     {
  78.         printTree(root->left);
  79.         printBook(root->info);
  80.         printTree(root->right);
  81.     }
  82. }
  83.  
  84. //i will search a book by id
  85. Book findBookByID(NODE* root, int id)
  86. {
  87.     if (root)
  88.     {
  89.         if (root->info.id == id)
  90.         {
  91.             return root->info;
  92.         }
  93.         else
  94.         {
  95.             if (root->info.id > id)
  96.             {
  97.                 return findBookByID(root->left, id);
  98.             }
  99.             else
  100.             {
  101.                 return findBookByID(root->right, id);
  102.             }
  103.         }
  104.     }
  105.     else
  106.     {
  107.         Book b;
  108.         b.id = -1;
  109.         b.author.age = -1;
  110.         b.author.age = NULL;
  111.         b.noPages = -1;
  112.         return b;
  113.     }
  114. }
  115.  
  116. int height(NODE* root)
  117. {
  118.     if (root)
  119.     {
  120.         /*int h = 0;
  121.         if (root->left || root->right)
  122.         {
  123.  
  124.         }*/
  125.         int hL = height(root->left);
  126.         int hR = height(root->right);
  127.         return 1 + (hL > hR ? hL : hR);//return hL else hR
  128.     }
  129.     else
  130.     {
  131.         return 0;
  132.     }
  133. }
  134.  
  135. //sa afisam nodurile de pe un anumit nivel pe care l dam ca si parametru
  136. void afisareDePeNivel(NODE* root, int nivelDorit, int currentLevel)
  137. {
  138.     if (root)
  139.     {
  140.         if (nivelDorit == currentLevel)
  141.         {
  142.             printBook(root->info);
  143.         }
  144.         else
  145.         {//crestem si facem apel pe stanga si apel pe dreapta
  146.             afisareDePeNivel(root->left, nivelDorit, currentLevel + 1);
  147.             afisareDePeNivel(root->right, nivelDorit, currentLevel + 1);
  148.         }
  149.     }
  150. }
  151.  
  152. void main()
  153. {
  154.     FILE * f = fopen("books.txt", "r");
  155.     NODE* root = NULL;
  156.  
  157.     int noBooks = 0;
  158.     fscanf(f, "%d", &noBooks);
  159.     for (int i = 0; i < noBooks; i++)
  160.     {
  161.         root = insertTree(root, readBook(f));
  162.     }
  163.  
  164.     printTree(root);
  165.  
  166.     printf("\n\n");
  167.  
  168.     printBook(findBookByID(root, 6));
  169.  
  170.     printf("\n\n\n\n%d\n", height(root));
  171.  
  172.     afisareDePeNivel(root, 3, 1);
  173. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top