Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #define TREE_ENTRY(type) \
  43. struct { \
  44. struct type *avl_left; \
  45. struct type *avl_right; \
  46. int avl_height; \
  47. }
  48. #define TREE_HEAD(name, type) \
  49. struct name { \
  50. struct type *th_root; \
  51. int (*th_cmp)(struct type *lhs, struct type *rhs); \
  52. }
  53. #define TREE_INITIALIZER(cmp) { 0, cmp }
  54. #define TREE_DELTA(self, field) \
  55. (( (((self)->field.avl_left) ? (self)->field.avl_left->field.avl_height : 0)) \
  56. - (((self)->field.avl_right) ? (self)->field.avl_right->field.avl_height : 0))
  57. /* Recursion prevents the following from being defined as macros. */
  58. #define TREE_DEFINE(node, field) \
  59. \
  60. struct node *TREE_BALANCE_##node##_##field(struct node *); \
  61. \
  62. struct node *TREE_ROTL_##node##_##field(struct node *self) \
  63. { \
  64. struct node *r= self->field.avl_right; \
  65. self->field.avl_right= r->field.avl_left; \
  66. r->field.avl_left= TREE_BALANCE_##node##_##field(self); \
  67. return TREE_BALANCE_##node##_##field(r); \
  68. } \
  69. \
  70. struct node *TREE_ROTR_##node##_##field(struct node *self) \
  71. { \
  72. struct node *l= self->field.avl_left; \
  73. self->field.avl_left= l->field.avl_right; \
  74. l->field.avl_right= TREE_BALANCE_##node##_##field(self); \
  75. return TREE_BALANCE_##node##_##field(l); \
  76. } \
  77. \
  78. struct node *TREE_BALANCE_##node##_##field(struct node *self) \
  79. { \
  80. int delta= TREE_DELTA(self, field); \
  81. \
  82. if (delta < -TREE_DELTA_MAX) \
  83. { \
  84. if (TREE_DELTA(self->field.avl_right, field) > 0) \
  85. self->field.avl_right= TREE_ROTR_##node##_##field(self->field.avl_right); \
  86. return TREE_ROTL_##node##_##field(self); \
  87. } \
  88. else if (delta > TREE_DELTA_MAX) \
  89. { \
  90. if (TREE_DELTA(self->field.avl_left, field) < 0) \
  91. self->field.avl_left= TREE_ROTL_##node##_##field(self->field.avl_left); \
  92. return TREE_ROTR_##node##_##field(self); \
  93. } \
  94. self->field.avl_height= 0; \
  95. if (self->field.avl_left && (self->field.avl_left->field.avl_height > self->field.avl_height)) \
  96. self->field.avl_height= self->field.avl_left->field.avl_height; \
  97. if (self->field.avl_right && (self->field.avl_right->field.avl_height > self->field.avl_height)) \
  98. self->field.avl_height= self->field.avl_right->field.avl_height; \
  99. self->field.avl_height += 1; \
  100. return self; \
  101. } \
  102. \
  103. struct node *TREE_INSERT_##node##_##field \
  104. (struct node *self, struct node *elm, int (*compare)(struct node *lhs, struct node *rhs)) \
  105. { \
  106. if (!self) \
  107. return elm; \
  108. if (compare(elm, self) < 0) \
  109. self->field.avl_left= TREE_INSERT_##node##_##field(self->field.avl_left, elm, compare); \
  110. else \
  111. self->field.avl_right= TREE_INSERT_##node##_##field(self->field.avl_right, elm, compare); \
  112. return TREE_BALANCE_##node##_##field(self); \
  113. } \
  114. \
  115. struct node *TREE_FIND_##node##_##field \
  116. (struct node *self, struct node *elm, int (*compare)(struct node *lhs, struct node *rhs)) \
  117. { \
  118. if (!self) \
  119. return 0; \
  120. if (compare(elm, self) == 0) \
  121. return self; \
  122. if (compare(elm, self) < 0) \
  123. return TREE_FIND_##node##_##field(self->field.avl_left, elm, compare); \
  124. else \
  125. return TREE_FIND_##node##_##field(self->field.avl_right, elm, compare); \
  126. } \
  127. \
  128. struct node *TREE_MOVE_RIGHT(struct node *self, struct node *rhs) \
  129. { \
  130. if (!self) \
  131. return rhs; \
  132. self->field.avl_right= TREE_MOVE_RIGHT(self->field.avl_right, rhs); \
  133. return TREE_BALANCE_##node##_##field(self); \
  134. } \
  135. \
  136. struct node *TREE_REMOVE_##node##_##field \
  137. (struct node *self, struct node *elm, int (*compare)(struct node *lhs, struct node *rhs)) \
  138. { \
  139. if (!self) return 0; \
  140. \
  141. if (compare(elm, self) == 0) \
  142. { \
  143. struct node *tmp= TREE_MOVE_RIGHT(self->field.avl_left, self->field.avl_right); \
  144. self->field.avl_left= 0; \
  145. self->field.avl_right= 0; \
  146. return tmp; \
  147. } \
  148. if (compare(elm, self) < 0) \
  149. self->field.avl_left= TREE_REMOVE_##node##_##field(self->field.avl_left, elm, compare); \
  150. else \
  151. self->field.avl_right= TREE_REMOVE_##node##_##field(self->field.avl_right, elm, compare); \
  152. return TREE_BALANCE_##node##_##field(self); \
  153. } \
  154. \
  155. void TREE_FORWARD_APPLY_ALL_##node##_##field \
  156. (struct node *self, void (*function)(struct node *node, void *data), void *data) \
  157. { \
  158. if (self) \
  159. { \
  160. TREE_FORWARD_APPLY_ALL_##node##_##field(self->field.avl_left, function, data); \
  161. function(self, data); \
  162. TREE_FORWARD_APPLY_ALL_##node##_##field(self->field.avl_right, function, data); \
  163. } \
  164. } \
  165. \
  166. void TREE_REVERSE_APPLY_ALL_##node##_##field \
  167. (struct node *self, void (*function)(struct node *node, void *data), void *data) \
  168. { \
  169. if (self) \
  170. { \
  171. TREE_REVERSE_APPLY_ALL_##node##_##field(self->field.avl_right, function, data); \
  172. function(self, data); \
  173. TREE_REVERSE_APPLY_ALL_##node##_##field(self->field.avl_left, function, data); \
  174. } \
  175. }
  176. #define TREE_INSERT(head, node, field, elm) \
  177. ((head)->th_root= TREE_INSERT_##node##_##field((head)->th_root, (elm), (head)->th_cmp))
  178. #define TREE_FIND(head, node, field, elm) \
  179. (TREE_FIND_##node##_##field((head)->th_root, (elm), (head)->th_cmp))
  180. #define TREE_REMOVE(head, node, field, elm) \
  181. ((head)->th_root= TREE_REMOVE_##node##_##field((head)->th_root, (elm), (head)->th_cmp))
  182. #define TREE_DEPTH(head, field) \
  183. ((head)->th_root->field.avl_height)
  184. #define TREE_FORWARD_APPLY(head, node, field, function, data) \
  185. TREE_FORWARD_APPLY_ALL_##node##_##field((head)->th_root, function, data)
  186. #define TREE_REVERSE_APPLY(head, node, field, function, data) \
  187. TREE_REVERSE_APPLY_ALL_##node##_##field((head)->th_root, function, data)
  188. #define TREE_INIT(head, cmp) do { \
  189. (head)->th_root= 0; \
  190. (head)->th_cmp= (cmp); \
  191. } while (0)
  192. #endif /* __tree_h */