tsearch_avl.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <stdlib.h>
  2. #include <search.h>
  3. /*
  4. avl tree implementation using recursive functions
  5. the height of an n node tree is less than 1.44*log2(n+2)-1
  6. (so the max recursion depth in case of a tree with 2^32 nodes is 45)
  7. */
  8. struct node {
  9. const void *key;
  10. struct node *left;
  11. struct node *right;
  12. int height;
  13. };
  14. static int delta(struct node *n) {
  15. return (n->left ? n->left->height:0) - (n->right ? n->right->height:0);
  16. }
  17. static void updateheight(struct node *n) {
  18. n->height = 0;
  19. if (n->left && n->left->height > n->height)
  20. n->height = n->left->height;
  21. if (n->right && n->right->height > n->height)
  22. n->height = n->right->height;
  23. n->height++;
  24. }
  25. static struct node *rotl(struct node *n) {
  26. struct node *r = n->right;
  27. n->right = r->left;
  28. r->left = n;
  29. updateheight(n);
  30. updateheight(r);
  31. return r;
  32. }
  33. static struct node *rotr(struct node *n) {
  34. struct node *l = n->left;
  35. n->left = l->right;
  36. l->right = n;
  37. updateheight(n);
  38. updateheight(l);
  39. return l;
  40. }
  41. static struct node *balance(struct node *n) {
  42. int d = delta(n);
  43. if (d < -1) {
  44. if (delta(n->right) > 0)
  45. n->right = rotr(n->right);
  46. return rotl(n);
  47. } else if (d > 1) {
  48. if (delta(n->left) < 0)
  49. n->left = rotl(n->left);
  50. return rotr(n);
  51. }
  52. updateheight(n);
  53. return n;
  54. }
  55. static struct node *find(struct node *n, const void *k,
  56. int (*cmp)(const void *, const void *))
  57. {
  58. int c;
  59. if (!n)
  60. return 0;
  61. c = cmp(k, n->key);
  62. if (c == 0)
  63. return n;
  64. if (c < 0)
  65. return find(n->left, k, cmp);
  66. else
  67. return find(n->right, k, cmp);
  68. }
  69. static struct node *insert(struct node **n, const void *k,
  70. int (*cmp)(const void *, const void *), int *new)
  71. {
  72. struct node *r = *n;
  73. int c;
  74. if (!r) {
  75. *n = r = malloc(sizeof **n);
  76. if (r) {
  77. r->key = k;
  78. r->left = r->right = 0;
  79. r->height = 1;
  80. }
  81. *new = 1;
  82. return r;
  83. }
  84. c = cmp(k, r->key);
  85. if (c == 0)
  86. return r;
  87. if (c < 0)
  88. r = insert(&r->left, k, cmp, new);
  89. else
  90. r = insert(&r->right, k, cmp, new);
  91. if (*new)
  92. *n = balance(*n);
  93. return r;
  94. }
  95. static struct node *movr(struct node *n, struct node *r) {
  96. if (!n)
  97. return r;
  98. n->right = movr(n->right, r);
  99. return balance(n);
  100. }
  101. static struct node *remove(struct node **n, const void *k,
  102. int (*cmp)(const void *, const void *), struct node *parent)
  103. {
  104. int c;
  105. if (!*n)
  106. return 0;
  107. c = cmp(k, (*n)->key);
  108. if (c == 0) {
  109. struct node *r = *n;
  110. *n = movr(r->left, r->right);
  111. free(r);
  112. return parent;
  113. }
  114. if (c < 0)
  115. parent = remove(&(*n)->left, k, cmp, *n);
  116. else
  117. parent = remove(&(*n)->right, k, cmp, *n);
  118. if (parent)
  119. *n = balance(*n);
  120. return parent;
  121. }
  122. void *tdelete(const void *restrict key, void **restrict rootp,
  123. int(*compar)(const void *, const void *))
  124. {
  125. /* last argument is arbitrary non-null pointer
  126. which is returned when the root node is deleted */
  127. return remove((void*)rootp, key, compar, *rootp);
  128. }
  129. void *tfind(const void *key, void *const *rootp,
  130. int(*compar)(const void *, const void *))
  131. {
  132. return find(*rootp, key, compar);
  133. }
  134. void *tsearch(const void *key, void **rootp,
  135. int (*compar)(const void *, const void *))
  136. {
  137. int new = 0;
  138. return insert((void*)rootp, key, compar, &new);
  139. }
  140. static void walk(const struct node *r, void (*action)(const void *, VISIT, int), int d)
  141. {
  142. if (r == 0)
  143. return;
  144. if (r->left == 0 && r->right == 0)
  145. action(r, leaf, d);
  146. else {
  147. action(r, preorder, d);
  148. walk(r->left, action, d+1);
  149. action(r, postorder, d);
  150. walk(r->right, action, d+1);
  151. action(r, endorder, d);
  152. }
  153. }
  154. void twalk(const void *root, void (*action)(const void *, VISIT, int))
  155. {
  156. walk(root, action, 0);
  157. }