Advertisement
cd62131

AVL Tree

Jul 17th, 2014
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define max(a, b) \
  5.    ({ __typeof__ (a) _a = (a); \
  6.       __typeof__ (b) _b = (b); \
  7.       _a > _b ? _a : _b; })
  8. typedef struct node_t node_t;
  9. struct node_t {
  10.   int *val;
  11.   int height;
  12.   node_t *left, *right;
  13. };
  14. int main(void);
  15. static node_t *balance(node_t *node);
  16. static node_t *delete(node_t *node, int val);
  17. static node_t *find(node_t *node, int val);
  18. static node_t *insert(node_t *node, int val);
  19. static node_t *move_down(node_t *node, node_t *right);
  20. static node_t *new_node(int val);
  21. static node_t *rotate_left(node_t *node);
  22. static node_t *rotate_right(node_t *node);
  23. static void describe_find(node_t *node, int val);
  24. static void free_node(node_t *node);
  25. static int height(node_t *node);
  26. static void print_node(node_t *node, int depth);
  27. static int random_int(int upper);
  28. static int random_int(int upper) {
  29.   return (int) ((rand() / ((double) RAND_MAX + 1.)) * upper);
  30. }
  31. static node_t *new_node(int val) {
  32.   node_t *new = (node_t *) malloc(sizeof(node_t));
  33.   new->val = (int *) malloc(sizeof(int));
  34.   *new->val = val;
  35.   new->height = 1;
  36.   new->left = new->right = NULL;
  37.   return new;
  38. }
  39. static node_t *rotate_left(node_t *node) {
  40.   node_t *p = node->right;
  41.   node->right = p->left;
  42.   p->left = balance(node);
  43.   return balance(p);
  44. }
  45. static node_t *rotate_right(node_t *node) {
  46.   node_t *p = node->left;
  47.   node->left = p->right;
  48.   p->right = balance(node);
  49.   return balance(p);
  50. }
  51. static node_t *find(node_t *node, int val) {
  52.   if (!node) return NULL;
  53.   if (*node->val == val)
  54.     return node;
  55.   else if (*node->val > val)
  56.     return find(node->left, val);
  57.   else
  58.     return find(node->right, val);
  59. }
  60. static void describe_find(node_t *node, int val) {
  61.   if (!node) return;
  62.   if (*node->val == val)
  63.     printf("%d found.\n", val);
  64.   else if (*node->val > val) {
  65.     puts("left");
  66.     describe_find(node->left, val);
  67.   } else {
  68.     puts("right");
  69.     describe_find(node->right, val);
  70.   }
  71. }
  72. static int height(node_t *node) {
  73.   return node ? node->height : 0;
  74. }
  75. static node_t *balance(node_t *node) {
  76.   if (height(node->right) - height(node->left) < -1) {
  77.     if (height(node->left->right) - height(node->left->left) > 0) node->left = rotate_left(node->left);
  78.     return rotate_right(node);
  79.   }
  80.   if (height(node->left) - height(node->right) < -1) {
  81.     if (height(node->right->left) - height(node->right->right) > 0) node->right = rotate_right(node->right);
  82.     return rotate_left(node);
  83.   }
  84.   if (node) node->height = max(height(node->left), height(node->right)) + 1;
  85.   return node;
  86. }
  87. static node_t *insert(node_t *node, int val) {
  88.   if (find(node, val)) return node;
  89.   if (!node) return new_node(val);
  90.   if (*node->val > val)
  91.     node->left = insert(node->left, val);
  92.   else
  93.     node->right = insert(node->right, val);
  94.   return balance(node);
  95. }
  96. static node_t *move_down(node_t *node, node_t *right) {
  97.   if (!node) return right;
  98.   node->right = move_down(node->right, right);
  99.   return balance(node);
  100. }
  101. static void free_node(node_t *node) {
  102.   free(node->val);
  103.   free(node);
  104. }
  105. static node_t *delete(node_t *node, int val) {
  106.   node_t *left, *right;
  107.   if (!node) return NULL;
  108.   if (*node->val == val) {
  109.     left = node->left;
  110.     right = node->right;
  111.     free_node(node);
  112.     return move_down(left, right);
  113.   } else {
  114.     if (*node->val > val)
  115.       node->left = delete(node->left, val);
  116.     else
  117.       node->right = delete(node->right, val);
  118.     return balance(node);
  119.   }
  120. }
  121. static void print_node(node_t *node, int depth) {
  122.   int i;
  123.   if (!node) return;
  124.   if (node->left) print_node(node->left, depth + 1);
  125.   for (i = 0; i < depth; i++)
  126.     printf("\t");
  127.   printf("%2d:%d\n", *node->val, node->height);
  128.   if (node->right) print_node(node->right, depth + 1);
  129. }
  130. int main(void) {
  131.   const int n = 10, m = 100;
  132.   int i, c = 0, a[n], b[] = { 5, 9, 6, 11, 17, 19, 21, 23, 25, 35, 30, 28 };
  133.   node_t *root = NULL;
  134.   srand((unsigned) time(NULL));
  135.   for (i = 0; i < n; i++) {
  136.     a[i] = random_int(m);
  137.     root = insert(root, a[i]);
  138.     print_node(root, 0);
  139.     puts("----------------------------------------");
  140.   }
  141.   for (i = 0; i < m; i++) {
  142.     if (!find(root, i)) {
  143.       printf("%2d ", i);
  144.       if (++c % 20 == 0) puts("");
  145.     }
  146.     if (i == m - 1) puts("");
  147.   }
  148.   puts("----------------------------------------");
  149.   for (i = 0; i < n; i++) {
  150.     root = delete(root, a[i]);
  151.     print_node(root, 0);
  152.     puts("----------------------------------------");
  153.   }
  154.   for (i = 0; i < sizeof(b) / sizeof(int); i++) {
  155.     root = insert(root, b[i]);
  156.   }
  157.   print_node(root, 0);
  158.   puts("----------------------------------------");
  159.   describe_find(root, 11);
  160.   return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement