Advertisement
galahad231

ABBxAVL.c

Jan 20th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <time.h>
  6. int c = 1, w = 1;
  7. typedef struct binary_tree{
  8.     int item;
  9.     int h;
  10.     struct binary_tree *left;
  11.     struct binary_tree *right;
  12. }binary_tree;
  13. binary_tree * create_empty_tree(){
  14.     return NULL;
  15. }
  16. binary_tree *create_tree(int item, binary_tree *left, binary_tree *right){
  17. binary_tree *new_tree = (binary_tree*) malloc(sizeof(binary_tree));
  18. new_tree->item = item;
  19. new_tree->left = left;
  20. new_tree->right = right;
  21. return new_tree;
  22. }
  23. void print_in_order(binary_tree *bt){
  24.     if(bt != NULL){
  25.         print_in_order(bt->left);
  26.         printf("%d\n" , bt->item);
  27.         print_in_order(bt->right);
  28.     }
  29. }
  30. void print_pre_order(binary_tree *bt){
  31.     if(bt != NULL){
  32.         printf("%d\n", bt->item);
  33.         print_pre_order(bt->left);
  34.         print_pre_order(bt->right);
  35.     }
  36. }
  37. void print_post_order(binary_tree *bt){
  38.     if(bt != NULL){
  39.         print_post_order(bt->left);
  40.         print_post_order(bt->right);
  41.         printf("%d\n", bt->item);
  42.     }
  43. }
  44. int search_abb(binary_tree *bt, int item){
  45.     if((bt == NULL) || (bt->item == item)){
  46.         return c;
  47.     } else if(bt->item > item){
  48.         c = c + 1;
  49.         return search_abb(bt->left, item);
  50.     } else{
  51.         c = c + 1;
  52.         return search_abb(bt->right, item);
  53.     }
  54. }
  55. binary_tree *add_abb(binary_tree *bt, int item){
  56.     if(bt == NULL){
  57.         bt = create_tree(item, NULL, NULL);
  58.     } else if(bt->item > item){
  59.         bt->left = add_abb(bt->left, item);
  60.     } else{
  61.         bt->right = add_abb(bt->right, item);
  62.     }
  63.     return bt;
  64. }
  65. int balance_factor(binary_tree *bt){
  66.     if(bt == NULL){
  67.         return 0;
  68.     }else if((bt->left != NULL) && (bt->right != NULL)){
  69.         return (bt->left->h - bt->right->h);
  70.     }else if((bt->left != NULL) && (bt->right == NULL)){
  71.         return (1 + bt->left->h);
  72.     }else{
  73.         return (-bt->right->h - 1);
  74.     }
  75. }
  76. int max(int a, int b){
  77.     return (a > b) ? a : b;
  78. }
  79. int h(binary_tree *bt){
  80.     if(bt == NULL){
  81.         return -1;
  82.     } else{
  83.         return 1 + max(h(bt->left), h(bt->right));
  84.     }
  85. }
  86. int is_balanced(binary_tree *bt){
  87.     int bf = h(bt->left) - h(bt->right);
  88.     return ((-1 <= bf) && (bf <= 1));
  89. }
  90. binary_tree *rotate_left(binary_tree *bt){
  91.     binary_tree *subtree_root = NULL;
  92.     if(bt != NULL && bt->right != NULL){
  93.         subtree_root = bt->right;
  94.         bt->right = subtree_root->left;
  95.         subtree_root->left = bt;
  96.     }
  97.     subtree_root->h = h(subtree_root);
  98.     bt->h = h(bt);
  99.     return subtree_root;
  100. }
  101. binary_tree *rotate_right(binary_tree *bt){
  102.     binary_tree *subtree_root = NULL;
  103.     if(bt != NULL && bt->left != NULL){
  104.         subtree_root = bt->left;
  105.         bt->left = subtree_root->right;
  106.         subtree_root->right = bt;
  107.     }
  108.     subtree_root->h = h(subtree_root);
  109.     bt->h = h(bt);
  110.     return subtree_root;
  111. }
  112. int search_avl(binary_tree *bt, int item){
  113.     if((bt == NULL) || (bt->item == item)){
  114.         return w;
  115.     } else if(bt->item > item){
  116.         w = w + 1;
  117.         return search_avl(bt->left, item);
  118.     } else{
  119.         w = w + 1;
  120.         return search_avl(bt->right, item);
  121.     }
  122. }
  123. binary_tree *add_avl(binary_tree *bt, int item){
  124.     if(bt == NULL){
  125.         return create_tree(item, NULL, NULL);
  126.     }else if(bt->item > item){
  127.         bt->left = add_avl(bt->left, item);
  128.     }else{
  129.         bt->right = add_avl(bt->right, item);
  130.     }
  131.     bt->h = h(bt);
  132.     binary_tree *child = NULL;
  133.     if(balance_factor(bt) == 2 || balance_factor(bt) == -2){
  134.         if(balance_factor(bt) == 2){
  135.             child = bt->left;
  136.             if(balance_factor(child) == -1){
  137.                 bt->left = rotate_left(child);
  138.             }
  139.             bt = rotate_right(bt);
  140.         }else if(balance_factor(bt) == -2){
  141.             child = bt->right;
  142.             if(balance_factor(child) == 1){
  143.                 bt->right = rotate_right(child);
  144.             }
  145.             bt = rotate_left(bt);
  146.         }
  147.     }
  148.     return bt;
  149. }
  150. void comparator(){
  151.     long long int n, element, i, position, element_search, ate;
  152.     char choise;
  153.     printf("Quantos números serão gerado?\n");
  154.     scanf("%lld", &n);
  155.     printf("Até qual elemento?\n");
  156.     scanf("%lld", &ate);
  157.     printf("Escolher o a posição do número a ser procurado? s/n\n");
  158.     getchar();
  159.     scanf("%c", &choise);
  160.     srand(time(NULL));
  161.     if(choise == 's'){
  162.         printf("Qual posição, entre 0/%lld\n", n - 1);
  163.         scanf("%lld", &position);
  164.         position = position - 1;
  165.     }else position = rand() % n;
  166.     binary_tree *ABB = create_empty_tree();
  167.     binary_tree *AVL   = create_empty_tree();
  168.     for(i=0;i<n;i++){
  169.         element = rand() % ate;
  170.         ABB = add_abb(ABB, element);
  171.         AVL = add_avl(AVL, element);
  172.         if(i == position) element_search = element;
  173.     }
  174.     // printf("ABB:\n");
  175.     // print_pre_order(ABB);
  176.     // printf("AVL:\n");
  177.     // print_pre_order(AVL);
  178.     printf("Elemento procurado: %lld, foi %lld a ser inserido\n", element_search, position + 1);
  179.     printf("Comparações ABB: %d\n", search_abb(ABB, element_search));
  180.     printf("Comparações AVL: %d\n", search_avl(AVL, element_search));
  181. }
  182. void main(){
  183.     comparator();
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement