You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tree.h 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* tree.h -- AVL trees (in the spirit of BSD's 'queue.h') -*- C -*- */
  2. /* Copyright (c) 2005 Ian Piumarta
  3. *
  4. * All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the 'Software'), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, and/or sell copies of the
  10. * Software, and to permit persons to whom the Software is furnished to do so,
  11. * provided that the above copyright notice(s) and this permission notice appear
  12. * in all copies of the Software and that both the above copyright notice(s) and
  13. * this permission notice appear in supporting documentation.
  14. *
  15. * THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
  16. */
  17. /* This file defines an AVL balanced binary tree [Georgii M. Adelson-Velskii and
  18. * Evgenii M. Landis, 'An algorithm for the organization of information',
  19. * Doklady Akademii Nauk SSSR, 146:263-266, 1962 (Russian). Also in Myron
  20. * J. Ricci (trans.), Soviet Math, 3:1259-1263, 1962 (English)].
  21. *
  22. * An AVL tree is headed by pointers to the root node and to a function defining
  23. * the ordering relation between nodes. Each node contains an arbitrary payload
  24. * plus three fields per tree entry: the depth of the subtree for which it forms
  25. * the root and two pointers to child nodes (singly-linked for minimum space, at
  26. * the expense of direct access to the parent node given a pointer to one of the
  27. * children). The tree is rebalanced after every insertion or removal. The
  28. * tree may be traversed in two directions: forward (in-order left-to-right) and
  29. * reverse (in-order, right-to-left).
  30. *
  31. * Because of the recursive nature of many of the operations on trees it is
  32. * necessary to define a number of helper functions for each type of tree node.
  33. * The macro TREE_DEFINE(node_tag, entry_name) defines these functions with
  34. * unique names according to the node_tag. This macro should be invoked,
  35. * thereby defining the necessary functions, once per node tag in the program.
  36. *
  37. * For details on the use of these macros, see the tree(3) manual page.
  38. */
  39. #ifndef __tree_h
  40. #define __tree_h
  41. #define TREE_DELTA_MAX 1
  42. #ifndef _HU_FUNCTION
  43. # if defined(__GNUC__) || defined(__clang__)
  44. # define _HU_FUNCTION(x) __attribute__((__unused__)) x
  45. # else
  46. # define _HU_FUNCTION(x) x
  47. # endif
  48. #endif
  49. #define TREE_ENTRY(type) \
  50. struct { \
  51. struct type *avl_left; \
  52. struct type *avl_right; \
  53. int avl_height; \
  54. }
  55. #define TREE_HEAD(name, type) \
  56. struct name { \
  57. struct type *th_root; \
  58. int (*th_cmp)(struct type *lhs, struct type *rhs); \
  59. }
  60. #define TREE_INITIALIZER(cmp) { 0, cmp }
  61. #define TREE_DELTA(self, field) \
  62. (( (((self)->field.avl_left) ? (self)->field.avl_left->field.avl_height : 0)) \
  63. - (((self)->field.avl_right) ? (self)->field.avl_right->field.avl_height : 0))
  64. /* Recursion prevents the following from being defined as macros. */
  65. #define TREE_DEFINE(node, field) \
  66. \
  67. static struct node *_HU_FUNCTION(TREE_BALANCE_##node##_##field)(struct node *); \
  68. \
  69. static struct node *_HU_FUNCTION(TREE_ROTL_##node##_##field)(struct node *self) \
  70. { \
  71. struct node *r= self->field.avl_right; \
  72. self->field.avl_right= r->field.avl_left; \
  73. r->field.avl_left= TREE_BALANCE_##node##_##field(self); \
  74. return TREE_BALANCE_##node##_##field(r); \
  75. } \
  76. \
  77. static struct node *_HU_FUNCTION(TREE_ROTR_##node##_##field)(struct node *self) \
  78. { \
  79. struct node *l= self->field.avl_left; \
  80. self->field.avl_left= l->field.avl_right; \
  81. l->field.avl_right= TREE_BALANCE_##node##_##field(self); \
  82. return TREE_BALANCE_##node##_##field(l); \
  83. } \
  84. \
  85. static struct node *_HU_FUNCTION(TREE_BALANCE_##node##_##field)(struct node *self) \
  86. { \
  87. int delta= TREE_DELTA(self, field); \
  88. \
  89. if (delta < -TREE_DELTA_MAX) \
  90. { \
  91. if (TREE_DELTA(self->field.avl_right, field) > 0) \
  92. self->field.avl_right= TREE_ROTR_##node##_##field(self->field.avl_right); \
  93. return TREE_ROTL_##node##_##field(self); \
  94. } \
  95. else if (delta > TREE_DELTA_MAX) \
  96. { \
  97. if (TREE_DELTA(self->field.avl_left, field) < 0) \
  98. self->field.avl_left= TREE_ROTL_##node##_##field(self->field.avl_left); \
  99. return TREE_ROTR_##node##_##field(self); \
  100. } \
  101. self->field.avl_height= 0; \
  102. if (self->field.avl_left && (self->field.avl_left->field.avl_height > self->field.avl_height)) \
  103. self->field.avl_height= self->field.avl_left->field.avl_height; \
  104. if (self->field.avl_right && (self->field.avl_right->field.avl_height > self->field.avl_height)) \
  105. self->field.avl_height= self->field.avl_right->field.avl_height; \
  106. self->field.avl_height += 1; \
  107. return self; \
  108. } \
  109. \
  110. static struct node *_HU_FUNCTION(TREE_INSERT_##node##_##field) \
  111. (struct node *self, struct node *elm, int (*compare)(struct node *lhs, struct node *rhs)) \
  112. { \
  113. if (!self) \
  114. return elm; \
  115. if (compare(elm, self) < 0) \
  116. self->field.avl_left= TREE_INSERT_##node##_##field(self->field.avl_left, elm, compare); \
  117. else \
  118. self->field.avl_right= TREE_INSERT_##node##_##field(self->field.avl_right, elm, compare); \
  119. return TREE_BALANCE_##node##_##field(self); \
  120. } \
  121. \
  122. static struct node *_HU_FUNCTION(TREE_FIND_##node##_##field) \
  123. (struct node *self, struct node *elm, int (*compare)(struct node *lhs, struct node *rhs)) \
  124. { \
  125. if (!self) \
  126. return 0; \
  127. if (compare(elm, self) == 0) \
  128. return self; \
  129. if (compare(elm, self) < 0) \
  130. return TREE_FIND_##node##_##field(self->field.avl_left, elm, compare); \
  131. else \
  132. return TREE_FIND_##node##_##field(self->field.avl_right, elm, compare); \
  133. } \
  134. \
  135. static struct node *_HU_FUNCTION(TREE_MOVE_RIGHT)(struct node *self, struct node *rhs) \
  136. { \
  137. if (!self) \
  138. return rhs; \
  139. self->field.avl_right= TREE_MOVE_RIGHT(self->field.avl_right, rhs); \
  140. return TREE_BALANCE_##node##_##field(self); \
  141. } \
  142. \
  143. static struct node *_HU_FUNCTION(TREE_REMOVE_##node##_##field) \
  144. (struct node *self, struct node *elm, int (*compare)(struct node *lhs, struct node *rhs)) \
  145. { \
  146. if (!self) return 0; \
  147. \
  148. if (compare(elm, self) == 0) \
  149. { \
  150. struct node *tmp= TREE_MOVE_RIGHT(self->field.avl_left, self->field.avl_right); \
  151. self->field.avl_left= 0; \
  152. self->field.avl_right= 0; \
  153. return tmp; \
  154. } \
  155. if (compare(elm, self) < 0) \
  156. self->field.avl_left= TREE_REMOVE_##node##_##field(self->field.avl_left, elm, compare); \
  157. else \
  158. self->field.avl_right= TREE_REMOVE_##node##_##field(self->field.avl_right, elm, compare); \
  159. return TREE_BALANCE_##node##_##field(self); \
  160. } \
  161. \
  162. static void _HU_FUNCTION(TREE_FORWARD_APPLY_ALL_##node##_##field) \
  163. (struct node *self, void (*function)(struct node *node, void *data), void *data) \
  164. { \
  165. if (self) \
  166. { \
  167. TREE_FORWARD_APPLY_ALL_##node##_##field(self->field.avl_left, function, data); \
  168. function(self, data); \
  169. TREE_FORWARD_APPLY_ALL_##node##_##field(self->field.avl_right, function, data); \
  170. } \
  171. } \
  172. \
  173. static void _HU_FUNCTION(TREE_REVERSE_APPLY_ALL_##node##_##field) \
  174. (struct node *self, void (*function)(struct node *node, void *data), void *data) \
  175. { \
  176. if (self) \
  177. { \
  178. TREE_REVERSE_APPLY_ALL_##node##_##field(self->field.avl_right, function, data); \
  179. function(self, data); \
  180. TREE_REVERSE_APPLY_ALL_##node##_##field(self->field.avl_left, function, data); \
  181. } \
  182. }
  183. #define TREE_INSERT(head, node, field, elm) \
  184. ((head)->th_root= TREE_INSERT_##node##_##field((head)->th_root, (elm), (head)->th_cmp))
  185. #define TREE_FIND(head, node, field, elm) \
  186. (TREE_FIND_##node##_##field((head)->th_root, (elm), (head)->th_cmp))
  187. #define TREE_REMOVE(head, node, field, elm) \
  188. ((head)->th_root= TREE_REMOVE_##node##_##field((head)->th_root, (elm), (head)->th_cmp))
  189. #define TREE_DEPTH(head, field) \
  190. ((head)->th_root->field.avl_height)
  191. #define TREE_FORWARD_APPLY(head, node, field, function, data) \
  192. TREE_FORWARD_APPLY_ALL_##node##_##field((head)->th_root, function, data)
  193. #define TREE_REVERSE_APPLY(head, node, field, function, data) \
  194. TREE_REVERSE_APPLY_ALL_##node##_##field((head)->th_root, function, data)
  195. #define TREE_INIT(head, cmp) do { \
  196. (head)->th_root= 0; \
  197. (head)->th_cmp= (cmp); \
  198. } while (0)
  199. #endif /* __tree_h */