avl_tree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. avl_tree.c -- avl_ tree and linked list convenience
  3. Copyright (C) 1998 Michael H. Buselli
  4. 2000-2005 Ivo Timmermans,
  5. 2000-2015 Guus Sliepen <guus@tinc-vpn.org>
  6. 2000-2005 Wessel Dankers <wsl@tinc-vpn.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along
  16. with this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. Original AVL tree library by Michael H. Buselli <cosine@cosine.org>.
  19. Modified 2000-11-28 by Wessel Dankers <wsl@tinc-vpn.org> to use counts
  20. instead of depths, to add the ->next and ->prev and to generally obfuscate
  21. the code. Mail me if you found a bug.
  22. Cleaned up and incorporated some of the ideas from the red-black tree
  23. library for inclusion into tinc (https://www.tinc-vpn.org/) by
  24. Guus Sliepen <guus@tinc-vpn.org>.
  25. */
  26. #include "system.h"
  27. #include "avl_tree.h"
  28. #include "xalloc.h"
  29. #ifdef AVL_COUNT
  30. #define AVL_NODE_COUNT(n) ((n) ? (n)->count : 0)
  31. #define AVL_L_COUNT(n) (AVL_NODE_COUNT((n)->left))
  32. #define AVL_R_COUNT(n) (AVL_NODE_COUNT((n)->right))
  33. #define AVL_CALC_COUNT(n) (AVL_L_COUNT(n) + AVL_R_COUNT(n) + 1)
  34. #endif
  35. #ifdef AVL_DEPTH
  36. #define AVL_NODE_DEPTH(n) ((n) ? (n)->depth : 0)
  37. #define L_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->left))
  38. #define R_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->right))
  39. #define AVL_CALC_DEPTH(n) ((L_AVL_DEPTH(n)>R_AVL_DEPTH(n)?L_AVL_DEPTH(n):R_AVL_DEPTH(n)) + 1)
  40. #endif
  41. #ifndef AVL_DEPTH
  42. static int lg(unsigned int u) __attribute__((__const__));
  43. static int lg(unsigned int u) {
  44. int r = 1;
  45. if(!u) {
  46. return 0;
  47. }
  48. if(u & 0xffff0000) {
  49. u >>= 16;
  50. r += 16;
  51. }
  52. if(u & 0x0000ff00) {
  53. u >>= 8;
  54. r += 8;
  55. }
  56. if(u & 0x000000f0) {
  57. u >>= 4;
  58. r += 4;
  59. }
  60. if(u & 0x0000000c) {
  61. u >>= 2;
  62. r += 2;
  63. }
  64. if(u & 0x00000002) {
  65. r++;
  66. }
  67. return r;
  68. }
  69. #endif
  70. /* Internal helper functions */
  71. static int avl_check_balance(const avl_node_t *node) {
  72. #ifdef AVL_DEPTH
  73. int d;
  74. d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
  75. return d < -1 ? -1 : d > 1 ? 1 : 0;
  76. #else
  77. /* int d;
  78. * d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
  79. * d = d<-1?-1:d>1?1:0;
  80. */
  81. int pl, r;
  82. pl = lg(AVL_L_COUNT(node));
  83. r = AVL_R_COUNT(node);
  84. if(r >> pl + 1) {
  85. return 1;
  86. }
  87. if(pl < 2 || r >> pl - 2) {
  88. return 0;
  89. }
  90. return -1;
  91. #endif
  92. }
  93. static void avl_rebalance(avl_tree_t *tree, avl_node_t *node) {
  94. avl_node_t *child;
  95. avl_node_t *gchild;
  96. avl_node_t *parent;
  97. avl_node_t **superparent;
  98. while(node) {
  99. parent = node->parent;
  100. superparent =
  101. parent ? node ==
  102. parent->left ? &parent->left : &parent->right : &tree->root;
  103. switch(avl_check_balance(node)) {
  104. case -1:
  105. child = node->left;
  106. #ifdef AVL_DEPTH
  107. if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
  108. #else
  109. if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
  110. #endif
  111. node->left = child->right;
  112. if(node->left) {
  113. node->left->parent = node;
  114. }
  115. child->right = node;
  116. node->parent = child;
  117. *superparent = child;
  118. child->parent = parent;
  119. #ifdef AVL_COUNT
  120. node->count = AVL_CALC_COUNT(node);
  121. child->count = AVL_CALC_COUNT(child);
  122. #endif
  123. #ifdef AVL_DEPTH
  124. node->depth = AVL_CALC_DEPTH(node);
  125. child->depth = AVL_CALC_DEPTH(child);
  126. #endif
  127. } else {
  128. gchild = child->right;
  129. node->left = gchild->right;
  130. if(node->left) {
  131. node->left->parent = node;
  132. }
  133. child->right = gchild->left;
  134. if(child->right) {
  135. child->right->parent = child;
  136. }
  137. gchild->right = node;
  138. gchild->right->parent = gchild;
  139. gchild->left = child;
  140. gchild->left->parent = gchild;
  141. *superparent = gchild;
  142. gchild->parent = parent;
  143. #ifdef AVL_COUNT
  144. node->count = AVL_CALC_COUNT(node);
  145. child->count = AVL_CALC_COUNT(child);
  146. gchild->count = AVL_CALC_COUNT(gchild);
  147. #endif
  148. #ifdef AVL_DEPTH
  149. node->depth = AVL_CALC_DEPTH(node);
  150. child->depth = AVL_CALC_DEPTH(child);
  151. gchild->depth = AVL_CALC_DEPTH(gchild);
  152. #endif
  153. }
  154. break;
  155. case 1:
  156. child = node->right;
  157. #ifdef AVL_DEPTH
  158. if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
  159. #else
  160. if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
  161. #endif
  162. node->right = child->left;
  163. if(node->right) {
  164. node->right->parent = node;
  165. }
  166. child->left = node;
  167. node->parent = child;
  168. *superparent = child;
  169. child->parent = parent;
  170. #ifdef AVL_COUNT
  171. node->count = AVL_CALC_COUNT(node);
  172. child->count = AVL_CALC_COUNT(child);
  173. #endif
  174. #ifdef AVL_DEPTH
  175. node->depth = AVL_CALC_DEPTH(node);
  176. child->depth = AVL_CALC_DEPTH(child);
  177. #endif
  178. } else {
  179. gchild = child->left;
  180. node->right = gchild->left;
  181. if(node->right) {
  182. node->right->parent = node;
  183. }
  184. child->left = gchild->right;
  185. if(child->left) {
  186. child->left->parent = child;
  187. }
  188. gchild->left = node;
  189. gchild->left->parent = gchild;
  190. gchild->right = child;
  191. gchild->right->parent = gchild;
  192. *superparent = gchild;
  193. gchild->parent = parent;
  194. #ifdef AVL_COUNT
  195. node->count = AVL_CALC_COUNT(node);
  196. child->count = AVL_CALC_COUNT(child);
  197. gchild->count = AVL_CALC_COUNT(gchild);
  198. #endif
  199. #ifdef AVL_DEPTH
  200. node->depth = AVL_CALC_DEPTH(node);
  201. child->depth = AVL_CALC_DEPTH(child);
  202. gchild->depth = AVL_CALC_DEPTH(gchild);
  203. #endif
  204. }
  205. break;
  206. default:
  207. #ifdef AVL_COUNT
  208. node->count = AVL_CALC_COUNT(node);
  209. #endif
  210. #ifdef AVL_DEPTH
  211. node->depth = AVL_CALC_DEPTH(node);
  212. #endif
  213. }
  214. node = parent;
  215. }
  216. }
  217. /* (De)constructors */
  218. avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete) {
  219. avl_tree_t *tree;
  220. tree = xmalloc_and_zero(sizeof(avl_tree_t));
  221. tree->compare = compare;
  222. tree->delete = delete;
  223. return tree;
  224. }
  225. void avl_free_tree(avl_tree_t *tree) {
  226. free(tree);
  227. }
  228. avl_node_t *avl_alloc_node(void) {
  229. return xmalloc_and_zero(sizeof(avl_node_t));
  230. }
  231. void avl_free_node(avl_tree_t *tree, avl_node_t *node) {
  232. if(node->data && tree->delete) {
  233. tree->delete(node->data);
  234. }
  235. free(node);
  236. }
  237. /* Searching */
  238. void *avl_search(const avl_tree_t *tree, const void *data) {
  239. avl_node_t *node;
  240. node = avl_search_node(tree, data);
  241. return node ? node->data : NULL;
  242. }
  243. void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result) {
  244. avl_node_t *node;
  245. node = avl_search_closest_node(tree, data, result);
  246. return node ? node->data : NULL;
  247. }
  248. void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data) {
  249. avl_node_t *node;
  250. node = avl_search_closest_smaller_node(tree, data);
  251. return node ? node->data : NULL;
  252. }
  253. void *avl_search_closest_greater(const avl_tree_t *tree, const void *data) {
  254. avl_node_t *node;
  255. node = avl_search_closest_greater_node(tree, data);
  256. return node ? node->data : NULL;
  257. }
  258. avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data) {
  259. avl_node_t *node;
  260. int result;
  261. node = avl_search_closest_node(tree, data, &result);
  262. return result ? NULL : node;
  263. }
  264. avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
  265. int *result) {
  266. avl_node_t *node;
  267. int c;
  268. node = tree->root;
  269. if(!node) {
  270. if(result) {
  271. *result = 0;
  272. }
  273. return NULL;
  274. }
  275. for(;;) {
  276. c = tree->compare(data, node->data);
  277. if(c < 0) {
  278. if(node->left) {
  279. node = node->left;
  280. } else {
  281. if(result) {
  282. *result = -1;
  283. }
  284. break;
  285. }
  286. } else if(c > 0) {
  287. if(node->right) {
  288. node = node->right;
  289. } else {
  290. if(result) {
  291. *result = 1;
  292. }
  293. break;
  294. }
  295. } else {
  296. if(result) {
  297. *result = 0;
  298. }
  299. break;
  300. }
  301. }
  302. return node;
  303. }
  304. avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
  305. const void *data) {
  306. avl_node_t *node;
  307. int result;
  308. node = avl_search_closest_node(tree, data, &result);
  309. if(result < 0) {
  310. node = node->prev;
  311. }
  312. return node;
  313. }
  314. avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
  315. const void *data) {
  316. avl_node_t *node;
  317. int result;
  318. node = avl_search_closest_node(tree, data, &result);
  319. if(result > 0) {
  320. node = node->next;
  321. }
  322. return node;
  323. }
  324. /* Insertion and deletion */
  325. avl_node_t *avl_insert(avl_tree_t *tree, void *data) {
  326. avl_node_t *closest, *new;
  327. int result;
  328. if(!tree->root) {
  329. new = avl_alloc_node();
  330. new->data = data;
  331. avl_insert_top(tree, new);
  332. } else {
  333. closest = avl_search_closest_node(tree, data, &result);
  334. switch(result) {
  335. case -1:
  336. new = avl_alloc_node();
  337. new->data = data;
  338. avl_insert_before(tree, closest, new);
  339. break;
  340. case 1:
  341. new = avl_alloc_node();
  342. new->data = data;
  343. avl_insert_after(tree, closest, new);
  344. break;
  345. default:
  346. return NULL;
  347. }
  348. }
  349. #ifdef AVL_COUNT
  350. new->count = 1;
  351. #endif
  352. #ifdef AVL_DEPTH
  353. new->depth = 1;
  354. #endif
  355. return new;
  356. }
  357. avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node) {
  358. avl_node_t *closest;
  359. int result;
  360. if(!tree->root) {
  361. avl_insert_top(tree, node);
  362. } else {
  363. closest = avl_search_closest_node(tree, node->data, &result);
  364. switch(result) {
  365. case -1:
  366. avl_insert_before(tree, closest, node);
  367. break;
  368. case 1:
  369. avl_insert_after(tree, closest, node);
  370. break;
  371. case 0:
  372. return NULL;
  373. }
  374. }
  375. #ifdef AVL_COUNT
  376. node->count = 1;
  377. #endif
  378. #ifdef AVL_DEPTH
  379. node->depth = 1;
  380. #endif
  381. return node;
  382. }
  383. void avl_insert_top(avl_tree_t *tree, avl_node_t *node) {
  384. node->prev = node->next = node->parent = NULL;
  385. tree->head = tree->tail = tree->root = node;
  386. }
  387. void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
  388. avl_node_t *node) {
  389. if(!before) {
  390. if(tree->tail) {
  391. avl_insert_after(tree, tree->tail, node);
  392. } else {
  393. avl_insert_top(tree, node);
  394. }
  395. return;
  396. }
  397. node->next = before;
  398. node->parent = before;
  399. node->prev = before->prev;
  400. if(before->left) {
  401. avl_insert_after(tree, before->prev, node);
  402. return;
  403. }
  404. if(before->prev) {
  405. before->prev->next = node;
  406. } else {
  407. tree->head = node;
  408. }
  409. before->prev = node;
  410. before->left = node;
  411. avl_rebalance(tree, before);
  412. }
  413. void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node) {
  414. if(!after) {
  415. if(tree->head) {
  416. avl_insert_before(tree, tree->head, node);
  417. } else {
  418. avl_insert_top(tree, node);
  419. }
  420. return;
  421. }
  422. if(after->right) {
  423. avl_insert_before(tree, after->next, node);
  424. return;
  425. }
  426. node->prev = after;
  427. node->parent = after;
  428. node->next = after->next;
  429. if(after->next) {
  430. after->next->prev = node;
  431. } else {
  432. tree->tail = node;
  433. }
  434. after->next = node;
  435. after->right = node;
  436. avl_rebalance(tree, after);
  437. }
  438. avl_node_t *avl_unlink(avl_tree_t *tree, void *data) {
  439. avl_node_t *node;
  440. node = avl_search_node(tree, data);
  441. if(node) {
  442. avl_unlink_node(tree, node);
  443. }
  444. return node;
  445. }
  446. void avl_unlink_node(avl_tree_t *tree, avl_node_t *node) {
  447. avl_node_t *parent;
  448. avl_node_t **superparent;
  449. avl_node_t *subst, *left, *right;
  450. avl_node_t *balnode;
  451. if(node->prev) {
  452. node->prev->next = node->next;
  453. } else {
  454. tree->head = node->next;
  455. }
  456. if(node->next) {
  457. node->next->prev = node->prev;
  458. } else {
  459. tree->tail = node->prev;
  460. }
  461. parent = node->parent;
  462. superparent =
  463. parent ? node ==
  464. parent->left ? &parent->left : &parent->right : &tree->root;
  465. left = node->left;
  466. right = node->right;
  467. if(!left) {
  468. *superparent = right;
  469. if(right) {
  470. right->parent = parent;
  471. }
  472. balnode = parent;
  473. } else if(!right) {
  474. *superparent = left;
  475. left->parent = parent;
  476. balnode = parent;
  477. } else {
  478. subst = node->prev;
  479. if(!subst) { // This only happens if node is not actually in a tree at all.
  480. abort();
  481. }
  482. if(subst == left) {
  483. balnode = subst;
  484. } else {
  485. balnode = subst->parent;
  486. balnode->right = subst->left;
  487. if(balnode->right) {
  488. balnode->right->parent = balnode;
  489. }
  490. subst->left = left;
  491. left->parent = subst;
  492. }
  493. subst->right = right;
  494. subst->parent = parent;
  495. right->parent = subst;
  496. *superparent = subst;
  497. }
  498. avl_rebalance(tree, balnode);
  499. node->next = node->prev = node->parent = node->left = node->right = NULL;
  500. #ifdef AVL_COUNT
  501. node->count = 0;
  502. #endif
  503. #ifdef AVL_DEPTH
  504. node->depth = 0;
  505. #endif
  506. }
  507. void avl_delete_node(avl_tree_t *tree, avl_node_t *node) {
  508. avl_unlink_node(tree, node);
  509. avl_free_node(tree, node);
  510. }
  511. void avl_delete(avl_tree_t *tree, void *data) {
  512. avl_node_t *node;
  513. node = avl_search_node(tree, data);
  514. if(node) {
  515. avl_delete_node(tree, node);
  516. }
  517. }
  518. /* Fast tree cleanup */
  519. void avl_delete_tree(avl_tree_t *tree) {
  520. avl_node_t *node, *next;
  521. for(node = tree->head; node; node = next) {
  522. next = node->next;
  523. avl_free_node(tree, node);
  524. }
  525. avl_free_tree(tree);
  526. }
  527. /* Tree walking */
  528. void avl_foreach(const avl_tree_t *tree, avl_action_t action) {
  529. avl_node_t *node, *next;
  530. for(node = tree->head; node; node = next) {
  531. next = node->next;
  532. action(node->data);
  533. }
  534. }
  535. void avl_foreach_node(const avl_tree_t *tree, avl_action_t action) {
  536. avl_node_t *node, *next;
  537. for(node = tree->head; node; node = next) {
  538. next = node->next;
  539. action(node);
  540. }
  541. }
  542. /* Indexing */
  543. #ifdef AVL_COUNT
  544. unsigned int avl_count(const avl_tree_t *tree) {
  545. return AVL_NODE_COUNT(tree->root);
  546. }
  547. avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index) {
  548. avl_node_t *node;
  549. unsigned int c;
  550. node = tree->root;
  551. while(node) {
  552. c = AVL_L_COUNT(node);
  553. if(index < c) {
  554. node = node->left;
  555. } else if(index > c) {
  556. node = node->right;
  557. index -= c + 1;
  558. } else {
  559. return node;
  560. }
  561. }
  562. return NULL;
  563. }
  564. unsigned int avl_index(const avl_node_t *node) {
  565. avl_node_t *next;
  566. unsigned int index;
  567. index = AVL_L_COUNT(node);
  568. while((next = node->parent)) {
  569. if(node == next->right) {
  570. index += AVL_L_COUNT(next) + 1;
  571. }
  572. node = next;
  573. }
  574. return index;
  575. }
  576. #endif
  577. #ifdef AVL_DEPTH
  578. unsigned int avl_depth(const avl_tree_t *tree) {
  579. return AVL_NODE_DEPTH(tree->root);
  580. }
  581. #endif