gl_avltree_oset.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /* Ordered set data type implemented by a binary tree.
  2. Copyright (C) 2006-2007, 2009-2013 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2006.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "gl_avltree_oset.h"
  17. #include <stdlib.h>
  18. /* An AVL tree is a binary tree where
  19. 1. The height of each node is calculated as
  20. heightof(node) = 1 + max (heightof(node.left), heightof(node.right)).
  21. 2. The heights of the subtrees of each node differ by at most 1:
  22. | heightof(right) - heightof(left) | <= 1.
  23. 3. The index of the elements in the node.left subtree are smaller than
  24. the index of node.
  25. The index of the elements in the node.right subtree are larger than
  26. the index of node.
  27. */
  28. /* -------------------------- gl_oset_t Data Type -------------------------- */
  29. /* Tree node implementation, valid for this file only. */
  30. struct gl_oset_node_impl
  31. {
  32. struct gl_oset_node_impl *left; /* left branch, or NULL */
  33. struct gl_oset_node_impl *right; /* right branch, or NULL */
  34. /* Parent pointer, or NULL. The parent pointer is not needed for most
  35. operations. It is needed so that a gl_oset_node_t can be returned
  36. without memory allocation, on which the functions gl_oset_remove_node,
  37. gl_oset_add_before, gl_oset_add_after can be implemented. */
  38. struct gl_oset_node_impl *parent;
  39. int balance; /* heightof(right) - heightof(left),
  40. always = -1 or 0 or 1 */
  41. const void *value;
  42. };
  43. typedef struct gl_oset_node_impl * gl_oset_node_t;
  44. /* Concrete gl_oset_impl type, valid for this file only. */
  45. struct gl_oset_impl
  46. {
  47. struct gl_oset_impl_base base;
  48. struct gl_oset_node_impl *root; /* root node or NULL */
  49. size_t count; /* number of nodes */
  50. };
  51. /* An AVL tree of height h has at least F_(h+2) [Fibonacci number] and at most
  52. 2^h - 1 elements. So, h <= 84 (because a tree of height h >= 85 would have
  53. at least F_87 elements, and because even on 64-bit machines,
  54. sizeof (gl_oset_node_impl) * F_87 > 2^64
  55. this would exceed the address space of the machine. */
  56. #define MAXHEIGHT 83
  57. /* Ensure the tree is balanced, after an insertion or deletion operation.
  58. The height of NODE is incremented by HEIGHT_DIFF (1 or -1).
  59. PARENT = NODE->parent. (NODE can also be NULL. But PARENT is non-NULL.)
  60. Rotation operations are performed starting at PARENT (not NODE itself!). */
  61. static void
  62. rebalance (gl_oset_t set,
  63. gl_oset_node_t node, int height_diff, gl_oset_node_t parent)
  64. {
  65. for (;;)
  66. {
  67. gl_oset_node_t child;
  68. int previous_balance;
  69. int balance_diff;
  70. gl_oset_node_t nodeleft;
  71. gl_oset_node_t noderight;
  72. child = node;
  73. node = parent;
  74. previous_balance = node->balance;
  75. /* The balance of NODE is incremented by BALANCE_DIFF: +1 if the right
  76. branch's height has increased by 1 or the left branch's height has
  77. decreased by 1, -1 if the right branch's height has decreased by 1 or
  78. the left branch's height has increased by 1, 0 if no height change. */
  79. if (node->left != NULL || node->right != NULL)
  80. balance_diff = (child == node->right ? height_diff : -height_diff);
  81. else
  82. /* Special case where above formula doesn't work, because the caller
  83. didn't tell whether node's left or right branch shrunk from height 1
  84. to NULL. */
  85. balance_diff = - previous_balance;
  86. node->balance += balance_diff;
  87. if (balance_diff == previous_balance)
  88. {
  89. /* node->balance is outside the range [-1,1]. Must rotate. */
  90. gl_oset_node_t *nodep;
  91. if (node->parent == NULL)
  92. /* node == set->root */
  93. nodep = &set->root;
  94. else if (node->parent->left == node)
  95. nodep = &node->parent->left;
  96. else if (node->parent->right == node)
  97. nodep = &node->parent->right;
  98. else
  99. abort ();
  100. nodeleft = node->left;
  101. noderight = node->right;
  102. if (balance_diff < 0)
  103. {
  104. /* node->balance = -2. The subtree is heavier on the left side.
  105. Rotate from left to right:
  106. *
  107. / \
  108. h+2 h
  109. */
  110. gl_oset_node_t nodeleftright = nodeleft->right;
  111. if (nodeleft->balance <= 0)
  112. {
  113. /*
  114. * h+2|h+3
  115. / \ / \
  116. h+2 h --> / h+1|h+2
  117. / \ | / \
  118. h+1 h|h+1 h+1 h|h+1 h
  119. */
  120. node->left = nodeleftright;
  121. nodeleft->right = node;
  122. nodeleft->parent = node->parent;
  123. node->parent = nodeleft;
  124. if (nodeleftright != NULL)
  125. nodeleftright->parent = node;
  126. nodeleft->balance += 1;
  127. node->balance = - nodeleft->balance;
  128. *nodep = nodeleft;
  129. height_diff = (height_diff < 0
  130. ? /* noderight's height had been decremented from
  131. h+1 to h. The subtree's height changes from
  132. h+3 to h+2|h+3. */
  133. nodeleft->balance - 1
  134. : /* nodeleft's height had been incremented from
  135. h+1 to h+2. The subtree's height changes from
  136. h+2 to h+2|h+3. */
  137. nodeleft->balance);
  138. }
  139. else
  140. {
  141. /*
  142. * h+2
  143. / \ / \
  144. h+2 h --> h+1 h+1
  145. / \ / \ / \
  146. h h+1 h L R h
  147. / \
  148. L R
  149. */
  150. gl_oset_node_t L = nodeleft->right = nodeleftright->left;
  151. gl_oset_node_t R = node->left = nodeleftright->right;
  152. nodeleftright->left = nodeleft;
  153. nodeleftright->right = node;
  154. nodeleftright->parent = node->parent;
  155. if (L != NULL)
  156. L->parent = nodeleft;
  157. if (R != NULL)
  158. R->parent = node;
  159. nodeleft->parent = nodeleftright;
  160. node->parent = nodeleftright;
  161. nodeleft->balance = (nodeleftright->balance > 0 ? -1 : 0);
  162. node->balance = (nodeleftright->balance < 0 ? 1 : 0);
  163. nodeleftright->balance = 0;
  164. *nodep = nodeleftright;
  165. height_diff = (height_diff < 0
  166. ? /* noderight's height had been decremented from
  167. h+1 to h. The subtree's height changes from
  168. h+3 to h+2. */
  169. -1
  170. : /* nodeleft's height had been incremented from
  171. h+1 to h+2. The subtree's height changes from
  172. h+2 to h+2. */
  173. 0);
  174. }
  175. }
  176. else
  177. {
  178. /* node->balance = 2. The subtree is heavier on the right side.
  179. Rotate from right to left:
  180. *
  181. / \
  182. h h+2
  183. */
  184. gl_oset_node_t noderightleft = noderight->left;
  185. if (noderight->balance >= 0)
  186. {
  187. /*
  188. * h+2|h+3
  189. / \ / \
  190. h h+2 --> h+1|h+2 \
  191. / \ / \ |
  192. h|h+1 h+1 h h|h+1 h+1
  193. */
  194. node->right = noderightleft;
  195. noderight->left = node;
  196. noderight->parent = node->parent;
  197. node->parent = noderight;
  198. if (noderightleft != NULL)
  199. noderightleft->parent = node;
  200. noderight->balance -= 1;
  201. node->balance = - noderight->balance;
  202. *nodep = noderight;
  203. height_diff = (height_diff < 0
  204. ? /* nodeleft's height had been decremented from
  205. h+1 to h. The subtree's height changes from
  206. h+3 to h+2|h+3. */
  207. - noderight->balance - 1
  208. : /* noderight's height had been incremented from
  209. h+1 to h+2. The subtree's height changes from
  210. h+2 to h+2|h+3. */
  211. - noderight->balance);
  212. }
  213. else
  214. {
  215. /*
  216. * h+2
  217. / \ / \
  218. h h+2 --> h+1 h+1
  219. / \ / \ / \
  220. h+1 h h L R h
  221. / \
  222. L R
  223. */
  224. gl_oset_node_t L = node->right = noderightleft->left;
  225. gl_oset_node_t R = noderight->left = noderightleft->right;
  226. noderightleft->left = node;
  227. noderightleft->right = noderight;
  228. noderightleft->parent = node->parent;
  229. if (L != NULL)
  230. L->parent = node;
  231. if (R != NULL)
  232. R->parent = noderight;
  233. node->parent = noderightleft;
  234. noderight->parent = noderightleft;
  235. node->balance = (noderightleft->balance > 0 ? -1 : 0);
  236. noderight->balance = (noderightleft->balance < 0 ? 1 : 0);
  237. noderightleft->balance = 0;
  238. *nodep = noderightleft;
  239. height_diff = (height_diff < 0
  240. ? /* nodeleft's height had been decremented from
  241. h+1 to h. The subtree's height changes from
  242. h+3 to h+2. */
  243. -1
  244. : /* noderight's height had been incremented from
  245. h+1 to h+2. The subtree's height changes from
  246. h+2 to h+2. */
  247. 0);
  248. }
  249. }
  250. node = *nodep;
  251. }
  252. else
  253. {
  254. /* No rotation needed. Only propagation of the height change to the
  255. next higher level. */
  256. if (height_diff < 0)
  257. height_diff = (previous_balance == 0 ? 0 : -1);
  258. else
  259. height_diff = (node->balance == 0 ? 0 : 1);
  260. }
  261. if (height_diff == 0)
  262. break;
  263. parent = node->parent;
  264. if (parent == NULL)
  265. break;
  266. }
  267. }
  268. static gl_oset_node_t
  269. gl_tree_nx_add_first (gl_oset_t set, const void *elt)
  270. {
  271. /* Create new node. */
  272. gl_oset_node_t new_node =
  273. (struct gl_oset_node_impl *) malloc (sizeof (struct gl_oset_node_impl));
  274. if (new_node == NULL)
  275. return NULL;
  276. new_node->left = NULL;
  277. new_node->right = NULL;
  278. new_node->balance = 0;
  279. new_node->value = elt;
  280. /* Add it to the tree. */
  281. if (set->root == NULL)
  282. {
  283. set->root = new_node;
  284. new_node->parent = NULL;
  285. }
  286. else
  287. {
  288. gl_oset_node_t node;
  289. for (node = set->root; node->left != NULL; )
  290. node = node->left;
  291. node->left = new_node;
  292. new_node->parent = node;
  293. node->balance--;
  294. /* Rebalance. */
  295. if (node->right == NULL && node->parent != NULL)
  296. rebalance (set, node, 1, node->parent);
  297. }
  298. set->count++;
  299. return new_node;
  300. }
  301. static gl_oset_node_t
  302. gl_tree_nx_add_before (gl_oset_t set, gl_oset_node_t node, const void *elt)
  303. {
  304. /* Create new node. */
  305. gl_oset_node_t new_node =
  306. (struct gl_oset_node_impl *) malloc (sizeof (struct gl_oset_node_impl));
  307. bool height_inc;
  308. if (new_node == NULL)
  309. return NULL;
  310. new_node->left = NULL;
  311. new_node->right = NULL;
  312. new_node->balance = 0;
  313. new_node->value = elt;
  314. /* Add it to the tree. */
  315. if (node->left == NULL)
  316. {
  317. node->left = new_node;
  318. node->balance--;
  319. height_inc = (node->right == NULL);
  320. }
  321. else
  322. {
  323. for (node = node->left; node->right != NULL; )
  324. node = node->right;
  325. node->right = new_node;
  326. node->balance++;
  327. height_inc = (node->left == NULL);
  328. }
  329. new_node->parent = node;
  330. /* Rebalance. */
  331. if (height_inc && node->parent != NULL)
  332. rebalance (set, node, 1, node->parent);
  333. set->count++;
  334. return new_node;
  335. }
  336. static gl_oset_node_t
  337. gl_tree_nx_add_after (gl_oset_t set, gl_oset_node_t node, const void *elt)
  338. {
  339. /* Create new node. */
  340. gl_oset_node_t new_node =
  341. (struct gl_oset_node_impl *) malloc (sizeof (struct gl_oset_node_impl));
  342. bool height_inc;
  343. if (new_node == NULL)
  344. return NULL;
  345. new_node->left = NULL;
  346. new_node->right = NULL;
  347. new_node->balance = 0;
  348. new_node->value = elt;
  349. /* Add it to the tree. */
  350. if (node->right == NULL)
  351. {
  352. node->right = new_node;
  353. node->balance++;
  354. height_inc = (node->left == NULL);
  355. }
  356. else
  357. {
  358. for (node = node->right; node->left != NULL; )
  359. node = node->left;
  360. node->left = new_node;
  361. node->balance--;
  362. height_inc = (node->right == NULL);
  363. }
  364. new_node->parent = node;
  365. /* Rebalance. */
  366. if (height_inc && node->parent != NULL)
  367. rebalance (set, node, 1, node->parent);
  368. set->count++;
  369. return new_node;
  370. }
  371. static bool
  372. gl_tree_remove_node (gl_oset_t set, gl_oset_node_t node)
  373. {
  374. gl_oset_node_t parent = node->parent;
  375. if (node->left == NULL)
  376. {
  377. /* Replace node with node->right. */
  378. gl_oset_node_t child = node->right;
  379. if (child != NULL)
  380. child->parent = parent;
  381. if (parent == NULL)
  382. set->root = child;
  383. else
  384. {
  385. if (parent->left == node)
  386. parent->left = child;
  387. else /* parent->right == node */
  388. parent->right = child;
  389. rebalance (set, child, -1, parent);
  390. }
  391. }
  392. else if (node->right == NULL)
  393. {
  394. /* It is not absolutely necessary to treat this case. But the more
  395. general case below is more complicated, hence slower. */
  396. /* Replace node with node->left. */
  397. gl_oset_node_t child = node->left;
  398. child->parent = parent;
  399. if (parent == NULL)
  400. set->root = child;
  401. else
  402. {
  403. if (parent->left == node)
  404. parent->left = child;
  405. else /* parent->right == node */
  406. parent->right = child;
  407. rebalance (set, child, -1, parent);
  408. }
  409. }
  410. else
  411. {
  412. /* Replace node with the rightmost element of the node->left subtree. */
  413. gl_oset_node_t subst;
  414. gl_oset_node_t subst_parent;
  415. gl_oset_node_t child;
  416. for (subst = node->left; subst->right != NULL; )
  417. subst = subst->right;
  418. subst_parent = subst->parent;
  419. child = subst->left;
  420. /* The case subst_parent == node is special: If we do nothing special,
  421. we get confusion about node->left, subst->left and child->parent.
  422. subst_parent == node
  423. <==> The 'for' loop above terminated immediately.
  424. <==> subst == subst_parent->left
  425. [otherwise subst == subst_parent->right]
  426. In this case, we would need to first set
  427. child->parent = node; node->left = child;
  428. and later - when we copy subst into node's position - again
  429. child->parent = subst; subst->left = child;
  430. Altogether a no-op. */
  431. if (subst_parent != node)
  432. {
  433. if (child != NULL)
  434. child->parent = subst_parent;
  435. subst_parent->right = child;
  436. }
  437. /* Copy subst into node's position.
  438. (This is safer than to copy subst's value into node, keep node in
  439. place, and free subst.) */
  440. if (subst_parent != node)
  441. {
  442. subst->left = node->left;
  443. subst->left->parent = subst;
  444. }
  445. subst->right = node->right;
  446. subst->right->parent = subst;
  447. subst->balance = node->balance;
  448. subst->parent = parent;
  449. if (parent == NULL)
  450. set->root = subst;
  451. else if (parent->left == node)
  452. parent->left = subst;
  453. else /* parent->right == node */
  454. parent->right = subst;
  455. /* Rebalancing starts at child's parent, that is subst_parent -
  456. except when subst_parent == node. In this case, we need to use
  457. its replacement, subst. */
  458. rebalance (set, child, -1, subst_parent != node ? subst_parent : subst);
  459. }
  460. set->count--;
  461. if (set->base.dispose_fn != NULL)
  462. set->base.dispose_fn (node->value);
  463. free (node);
  464. return true;
  465. }
  466. /* Generic binary tree code. */
  467. #include "gl_anytree_oset.h"
  468. /* For debugging. */
  469. static unsigned int
  470. check_invariants (gl_oset_node_t node, gl_oset_node_t parent, size_t *counterp)
  471. {
  472. unsigned int left_height =
  473. (node->left != NULL ? check_invariants (node->left, node, counterp) : 0);
  474. unsigned int right_height =
  475. (node->right != NULL ? check_invariants (node->right, node, counterp) : 0);
  476. int balance = (int)right_height - (int)left_height;
  477. if (!(node->parent == parent))
  478. abort ();
  479. if (!(balance >= -1 && balance <= 1))
  480. abort ();
  481. if (!(node->balance == balance))
  482. abort ();
  483. (*counterp)++;
  484. return 1 + (left_height > right_height ? left_height : right_height);
  485. }
  486. void
  487. gl_avltree_oset_check_invariants (gl_oset_t set)
  488. {
  489. size_t counter = 0;
  490. if (set->root != NULL)
  491. check_invariants (set->root, NULL, &counter);
  492. if (!(set->count == counter))
  493. abort ();
  494. }
  495. const struct gl_oset_implementation gl_avltree_oset_implementation =
  496. {
  497. gl_tree_nx_create_empty,
  498. gl_tree_size,
  499. gl_tree_search,
  500. gl_tree_search_atleast,
  501. gl_tree_nx_add,
  502. gl_tree_remove,
  503. gl_tree_oset_free,
  504. gl_tree_iterator,
  505. gl_tree_iterator_next,
  506. gl_tree_iterator_free
  507. };