Advertisement
Guest User

binary tree

a guest
Oct 14th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. //  MATEUSZ ZAMARLIK
  2. //  2017-02-28
  3. //  Lista 1 - Zadanie 4 - Drzewa binarne
  4.  
  5. /*  Zadanie 4. Zdefiniuj własny typ DrzewoBinarne reprezentujące drzewo
  6.     binarnych poszukiwań. DrzewoBinarne przechowujące w węzłach struktury
  7.     ustalonego typu str wraz z procedurami wstawiania i wyszukiwania elementów
  8.     w drzewie. Zaprogramuj również funkcję int rozmiar zwracającą liczbę
  9.     elementów drzewa.  */
  10.  
  11. // DYGRESJA
  12.     /*  zakladam, ze drzewo sie w zadnym punkcie nie zapetla, a wezly wystepuja
  13.         w drzewie jednorazowo tj. nie ma dwoch galezi dochodzacych do tego
  14.         samego elementu     */
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. typedef struct binary_t {
  20.     struct binary_t* left;
  21.     char* str;
  22.     struct binary_t* right;
  23. } BinaryTree;
  24.  
  25.  
  26.  
  27. // FUNKCJA WYSZUKIWANIA
  28. BinaryTree* search_tree(char* str, BinaryTree* tree) {
  29.     if(tree==NULL) return NULL;
  30.     while(strcmp(str,tree->str)!=0) {
  31.         if(strcmp(str,tree->str)<0)
  32.             if(tree->left!=NULL) tree=tree->left;
  33.             else {return NULL;}
  34.         else {
  35.             if(tree->right!=NULL) tree=tree->right;
  36.             else {return NULL;}
  37.         }
  38.     }
  39.     return tree;
  40. }
  41.  
  42.  
  43. // FUNKCJA WSTAWIANIA
  44. void insert_into_tree(char* str, BinaryTree *tree) {
  45.     BinaryTree* elem;
  46.     elem=(BinaryTree*)calloc(1,sizeof(BinaryTree));
  47.     if(search_tree(str,tree)==NULL) {
  48.         elem->str=str;
  49.         if(tree==NULL) {
  50.             tree=elem;
  51.         }
  52.         else while(strcmp(str,tree->str)!=0) {
  53.             if(strcmp(str,tree->str)<0)
  54.                 if(tree->left!=NULL) tree=tree->left;
  55.                 else {
  56.                     tree->left=elem;
  57.                     break;
  58.                 }
  59.             else {
  60.                 if(tree->right!=NULL) tree=tree->right;
  61.                 else {
  62.                     tree->right=elem;
  63.                     break;
  64.                 }
  65.             }
  66.         }
  67.     } else {free(elem); }
  68. }
  69.  
  70.  
  71. // FUNKCJA LICZACA ROZMIAR
  72. int size_of_tree(BinaryTree* tree) {
  73.     int result=1;
  74.     if(tree==NULL) return 0;
  75.     if(tree->left!=NULL) result+=size_of_tree(tree->left);
  76.     if(tree->right!=NULL) result+=size_of_tree(tree->right);
  77.     return result;
  78. }
  79.  
  80. void print_tree(BinaryTree *tree) {
  81.     if(tree=NULL) puts("Tree is empty.");
  82.     else {
  83.         printf("%s",tree->str);
  84.         if(tree->left!=NULL) {
  85.             printf("Left branch: ");
  86.             printt_tree()
  87.         }
  88.     }
  89. }
  90.  
  91. int main(void) {
  92.     BinaryTree *root = NULL;
  93.     insert_into_tree("abc",&root);
  94.     insert_into_tree("ab",&root);
  95.     insert_into_tree("bc",&root);
  96.     insert_into_tree("abbc",&root);
  97.     printf("Size: %d",size_of_tree(root));
  98.     return 0;  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement