Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "avl_tree.h"
  4.  
  5. int main()
  6. {
  7.     struct tree _tree;
  8.     struct tree* d = &_tree;
  9.     insert(d,4);
  10.     printf("%d",find(d,4));
  11.     return 0;
  12. }
  13.  
  14.  
  15.  
  16. #ifndef AVL_TREE_H_INCLUDED
  17. #define AVL_TREE_H_INCLUDED
  18. #define LEFT 0
  19. #define RIGHT 1
  20. struct node
  21. {
  22.     int data;
  23.     int balance;
  24.     struct node *leaf[2];
  25. };
  26. struct tree
  27. {
  28.     struct node *root;
  29. };
  30. struct node* single_rotation(struct node *root, int direction);
  31. struct node* double_rotation(struct node *root, int direction);
  32. void adjust_balance(struct node *root, int direction, int balance);
  33. struct node* insert_balance(struct node *root, int direction);
  34. struct node* insert_r(struct node *root, int data, int *done);
  35. struct node* make_node(int data);
  36. int insert(struct tree *_tree, int data);
  37. struct node* remove_balance(struct node* root, int direction, int *done);
  38. struct node* remove_r(struct node *root, int data, int *done);
  39. int _remove(struct tree *_tree, int data);
  40. int find(struct tree *_tree, int data);
  41. int find_r(struct node *root, int data);
  42. #endif // AVL_TREE_H_INCLUDED
  43.  
  44.  
  45. #include "avl_tree.h"
  46. #include <stdlib.h>
  47.  
  48. struct node* make_node(int data)
  49. {
  50.     struct node *rn = malloc(sizeof *rn);
  51.     if(rn != NULL)
  52.     {
  53.         rn->data = data;
  54.         rn->leaf[LEFT] = rn->leaf[RIGHT] = NULL;
  55.     }
  56.     return rn;
  57. }
  58. struct node* single_rotation(struct node *root, int direction)
  59. {
  60.     struct node *save = root->leaf[!direction];
  61.     root->leaf[!direction] = save->leaf[direction];
  62.     save->leaf[direction] = root;
  63.     return save;
  64. }
  65.  
  66. struct node* double_rotation(struct node *root, int direction)
  67. {
  68.     struct node *save = root->leaf[!direction]->leaf[direction];
  69.     root->leaf[!direction]->leaf[direction] = save->leaf[!direction];
  70.     save->leaf[!direction] = root->leaf[!direction];
  71.     root->leaf[!direction] = save;
  72.  
  73.     save = root->leaf[!direction];
  74.     root->leaf[!direction] = save->leaf[direction];
  75.     save->leaf[direction] = root;
  76.     return save;
  77. }
  78.  
  79. void adjust_balance(struct node *root, int direction, int balance)
  80. {
  81.     struct node *n = root->leaf[direction];
  82.     struct node *nn = root->leaf[!direction];
  83.  
  84.     if(nn->balance == 0)
  85.     {
  86.         root->balance = n->balance = 0;
  87.     }
  88.     else if (nn->balance == balance)
  89.     {
  90.         root->balance = -balance;
  91.         n->balance = 0;
  92.     }
  93.     else if(nn->balance == -balance)
  94.     {
  95.         root->balance = 0;
  96.         n->balance = balance;
  97.     }
  98.     nn->balance = 0;
  99. }
  100.  
  101. struct node *insert_balance(struct node *root, int direction)
  102. {
  103.     struct node *n = root->leaf[direction];
  104.     int balance;
  105.     if(direction == 0)
  106.     {
  107.         balance = -1;
  108.     }
  109.     else
  110.     {
  111.         balance = +1;
  112.     }
  113.     if(n->balance == balance)
  114.     {
  115.         root->balance = n->balance = 0;
  116.         root = single_rotation(root, !direction);
  117.     }
  118.     else
  119.     {
  120.         adjust_balance(root, direction, balance);
  121.         root = double_rotation(root, !direction);
  122.     }
  123.     return root;
  124. }
  125.  
  126. struct node* insert_r(struct node *root, int data, int *done)
  127. {
  128.     if(root == NULL)
  129.     {
  130.         root = make_node(data);
  131.     }
  132.     else
  133.     {
  134.         int direction = root->data < data;
  135.         root->leaf[direction] = insert_r(root->leaf[direction], data, done);
  136.         if(!*done)
  137.         {
  138.             if(direction == 0)
  139.             {
  140.                 root->balance--;
  141.             }
  142.             else
  143.             {
  144.                 root->balance++;
  145.             }
  146.             if(root->balance == 0)
  147.             {
  148.                 *done = 1;
  149.             }
  150.             else if(abs(root->balance) > 1)
  151.             {
  152.                 root = insert_balance(root,direction);
  153.                 *done = 1;
  154.             }
  155.         }
  156.     }
  157.     return root;
  158. }
  159.  
  160. int insert(struct tree *_tree, int data)
  161. {
  162.     int done = 0;
  163.     _tree->root = insert_r(_tree->root, data, &done);
  164.     return 1;
  165. }
  166.  
  167. struct node* remove_balance(struct node* root, int direction, int *done)
  168. {
  169.     struct node *n = root->leaf[!direction];
  170.     int balance = direction == 0 ? -1 : +1;
  171.     if(n->balance == -balance)
  172.     {
  173.         root->balance = n->balance = 0;
  174.         root = single_rotation(root,direction);
  175.     }
  176.     else if(n->balance == balance)
  177.     {
  178.         adjust_balance(root, !direction, -balance);
  179.         root = double_rotation(root,direction);
  180.     }
  181.     else if(n->balance == 0)
  182.     {
  183.         root->balance = -balance;
  184.         n->balance = balance;
  185.         root = single_rotation(root,direction);
  186.         *done = 1;
  187.     }
  188.     return root;
  189. }
  190.  
  191. struct node* remove_r(struct node *root, int data, int *done)
  192. {
  193.     if(root != NULL)
  194.     {
  195.         int direction;
  196.         if(root->data == data)
  197.         {
  198.  
  199.             if(root->leaf[LEFT] == NULL || root->leaf[1] == NULL)
  200.             {
  201.                 struct node *save;
  202.                 direction = root->leaf[LEFT] == NULL;
  203.                 save = root->leaf[direction];
  204.                 free(root);
  205.                 return save;
  206.             }
  207.             else
  208.             {
  209.                 struct node *heir = root->leaf[LEFT];
  210.                 while(heir->leaf[RIGHT] != NULL)
  211.                 {
  212.                     heir = heir->leaf[RIGHT];
  213.                 }
  214.                 root->data = heir->data;
  215.                 data = heir->data;
  216.             }
  217.         }
  218.         direction = root->data < data;
  219.         root->leaf[direction] = remove_r(root->leaf[direction], data, done);
  220.         if(!*done)
  221.         {
  222.             root->balance += direction != 0 ? -1 : +1;
  223.             if(abs(root->balance) == 1)
  224.             {
  225.                 *done = 1;
  226.             }
  227.             else if(abs(root->balance) > 1)
  228.             {
  229.                 root = remove_balance(root,direction,done);
  230.             }
  231.         }
  232.     }
  233.     return root;
  234. }
  235.  
  236. int _remove(struct tree *_tree, int data)
  237. {
  238.     int done = 0;
  239.     _tree->root = remove_r(_tree->root, data, &done);
  240.     return 1;
  241. }
  242.  
  243. int find_r(struct node *root, int data)
  244. {
  245.     if(root == NULL)
  246.     {
  247.         return 0-1;
  248.     }
  249.     else if(root->data == data)
  250.     {
  251.         return data;
  252.     }
  253.     else
  254.     {
  255.        int direction = root->data < data;
  256.        return find_r(root->leaf[direction], data);
  257.     }
  258. }
  259.  
  260. int find(struct tree *_tree, int data)
  261. {
  262.     return find_r(_tree->root, data);
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement