Advertisement
KsaneK

C - BST

May 23rd, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.33 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct bst
  4. {
  5.     struct bst * parent;
  6.     struct bst * left_node;
  7.     struct bst * right_node;
  8.     int key;
  9. };
  10.  
  11. int get_height(struct bst *tree)
  12. {
  13.     if(tree == NULL)
  14.         return 0;
  15.     int left_height = get_height(tree->left_node);
  16.     int right_height = get_height(tree->right_node);
  17.     if(left_height > right_height)
  18.         return left_height + 1;
  19.     else
  20.         return right_height + 1;
  21. }
  22.  
  23. void add_node(struct bst *tree, int key)
  24. {
  25.     short finished = 0;
  26.     struct bst * tmp = tree;
  27.     while(!finished)
  28.     {
  29.         if(key < tmp->key)
  30.         {
  31.             if(tmp->left_node != NULL)
  32.                 tmp = tmp->left_node;
  33.             else
  34.             {
  35.                 struct bst * new_node = (struct bst *)malloc(sizeof(struct bst));
  36.                 new_node->key = key;
  37.                 new_node->left_node = new_node->right_node = NULL;
  38.                 new_node->parent = tmp;
  39.                 tmp->left_node = new_node;
  40.                 finished = 1;
  41.             }
  42.         } else if(tmp->key < key) {
  43.             if(tmp->right_node != NULL)
  44.                 tmp = tmp->right_node;
  45.             else
  46.             {
  47.                 struct bst * new_node = (struct bst *)malloc(sizeof(struct bst));
  48.                 new_node->key = key;
  49.                 new_node->left_node = new_node->right_node = NULL;
  50.                 new_node->parent = tmp;
  51.                 tmp->right_node = new_node;
  52.                 finished = 1;
  53.             }
  54.         } else {
  55.             printf("Podana wartosc juz znajduje sie w drzewie");
  56.             finished = 1;
  57.         }
  58.     }
  59. }
  60.  
  61. void draw_vertically(struct bst * Tree, int level)
  62. {
  63.     if(!Tree)return;
  64.     draw_vertically(Tree->left_node, level + 1);
  65.     int i;
  66.     for(i = 0; i < level * 5; i++)
  67.         printf(" ");
  68.     printf("%d\n\n", Tree->key);
  69.     draw_vertically(Tree->right_node, level + 1);
  70. }
  71.  
  72. int find_node(struct bst *Tree, int key)
  73. {
  74.     if(!Tree)
  75.     {
  76.         printf("Nie znaleziono podanego elementu");
  77.         return -1;
  78.     }
  79.     if(key == Tree->key)
  80.     {
  81.         printf("%d", key);
  82.         return 0;
  83.     } else {
  84.         printf("%d -> ", Tree->key);
  85.         if(key < Tree->key)
  86.             return 1 + find_node(Tree->left_node, key);
  87.         else
  88.             return 1 + find_node(Tree->right_node, key);
  89.     }
  90. }
  91.  
  92. struct bst * get_node(struct bst * Tree, int key)
  93. {
  94.     if(!Tree)return NULL;
  95.     if(Tree->key == key)
  96.         return Tree;
  97.     if(key < Tree->key)
  98.         return get_node(Tree->left_node, key);
  99.     else
  100.         return get_node(Tree->right_node, key);
  101. }
  102.  
  103. int leaf_count(struct bst * Tree)
  104. {
  105.     if(!Tree)return 0;
  106.     int x, y;
  107.     if(!Tree->left_node && !Tree->right_node)
  108.         return 1;
  109.     else
  110.     {
  111.         x = leaf_count(Tree->left_node);
  112.         y = leaf_count(Tree->right_node);
  113.         return x + y;
  114.     }
  115. }
  116.  
  117. float avg_route(struct bst * Tree, int level)
  118. {
  119.     int x, y;
  120.     if(!Tree)return 0;
  121.     if(!Tree->left_node && !Tree->right_node)
  122.         return level;
  123.     else
  124.     {
  125.         x = avg_route(Tree->left_node, level + 1);
  126.         y = avg_route(Tree->right_node, level + 1);
  127.         return x + y;
  128.     }
  129. }
  130.  
  131. int find_next(struct bst * Tree, int key)
  132. {
  133.     Tree = get_node(Tree, key);
  134.     if(!Tree)return -1;
  135.     if(Tree->right_node)
  136.     {
  137.         struct bst *right = Tree->right_node;
  138.         while(right->left_node)right=right->left_node;
  139.         return right->key;
  140.     }
  141.     struct bst * tmp = Tree->parent;
  142.     while(tmp && Tree == tmp->right_node)
  143.     {
  144.         Tree = tmp;
  145.         tmp = tmp->parent;
  146.     }
  147.     if(tmp)return tmp->key;
  148.     else return -1;
  149. }
  150.  
  151. int main()
  152. {
  153.     struct bst Tree;
  154.     struct bst * TreePtr = &Tree;
  155.     short empty = 1;
  156.     int option = -1;
  157.     int key;
  158.     int height, i, j, spaces;
  159.     while(option != 0)
  160.     {
  161.         printf("Wybierz opcje:\n");
  162.         printf("1) Dodaj element do drzewa\n");
  163.         printf("2) Rysuj drzewo\n");
  164.         printf("3) Znajdz element\n");
  165.         printf("4) Oblicz ilosc lisci w drzewie\n");
  166.         printf("5) Znajdz nastepnik wybranego elementu\n");
  167.         printf("6) Oblicz srednia dlugosc sciezek z korzenia do lisci\n\n");
  168.         printf("0) Zakoncz program\n\n");
  169.         printf("Wybor: ");
  170.         scanf("%d", &option);
  171.         if(option == 1)
  172.         {
  173.             printf("Podaj wartosc do dodania: ");
  174.             scanf("%d", &key);
  175.             if(empty)
  176.             {
  177.                 Tree.key = key;
  178.                 Tree.parent = Tree.left_node = Tree.right_node = NULL;
  179.                 empty = 0;
  180.             }
  181.             else
  182.                 add_node(TreePtr, key);
  183.         }
  184.         else if(option == 2)
  185.         {
  186.             draw_vertically(TreePtr, 0);
  187.         }
  188.         else if(option == 3)
  189.         {
  190.             if(empty)
  191.                 printf("\nDrzewo jest puste.");
  192.             else
  193.             {
  194.                 int x;
  195.                 printf("\nPodaj szukany element: ");
  196.                 scanf("%d", &x);
  197.                 printf("\nWyszukiwanie wartosci %d: ", x);
  198.                 x = find_node(TreePtr, x);
  199.                 printf("\nPodana wartosc zostala znaleziona na poziomie %d.", x);
  200.                 printf("\n\n");
  201.             }
  202.         } else if(option == 4) {
  203.             int x = leaf_count(TreePtr);
  204.             printf("\nLiczba lisci w drzewie wynosi: %d.\n\n", x);
  205.         } else if(option == 5) {
  206.             int s, y;
  207.             printf("Podaj element: ");
  208.             scanf("%d", &s);
  209.             y = find_next(TreePtr, s);
  210.             printf("\nNastepnik elementu %d jest %d\n\n", s, y);
  211.         } else if(option == 6) {
  212.             float x = avg_route(TreePtr, 0);
  213.             printf("\nSrednia dlugosc sciezek wynosi %f.\n\n", x/leaf_count(TreePtr));
  214.         }
  215.     }
  216.     return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement