avl_tree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*!
  2. Temelia - AVL tree implementation source file.
  3. Some operations are adapted from the article:
  4. http://infoarena.ro/multe-smenuri-de-programare-in-cc-si-nu-numai
  5. Copyright (C) 2008, 2009 Ceata (http://ceata.org/proiecte/temelia).
  6. @author Dascalu Laurentiu
  7. This program is free software; you can redistribute it and
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 3
  10. of the License, or (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
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "include/avl_tree.h"
  20. #include "include/stack.h"
  21. #include <stdlib.h>
  22. /*!
  23. * Because AVL trees works with nodes height, I keep additional two parameters in a node.
  24. * User should not access this structure directly.
  25. */
  26. struct _avl_tree_node_t
  27. {
  28. /*! user's key */
  29. void *key;
  30. /*! node's height */
  31. int height;
  32. };
  33. #define GET_AVL_MEMBER(x, member) (((avl_tree_node_t)(x))->member)
  34. /*!
  35. * Internal functions used to make the layer between AVL and BST invisible for the user.
  36. * Because I don't have OOP in C i'm abusing pointers in order to change functions behavior.
  37. */
  38. extern stack_t compare_stack;
  39. extern stack_t iterating_handlers;
  40. //! Encapsulates a key into an avl tree node.
  41. static avl_tree_node_t _avl_tree_node_new(void *key, int height);
  42. //! Compares two avl tree nodes. For comparing the keys, library uses a global
  43. //! pointer where the function given by the user is stored.
  44. static int _avl_tree_internal_compare(void *x, void *y, void *context);
  45. //! Wrapper printing functions over user input.
  46. static void
  47. _avl_tree_internal_printing_level(void *x, int level, void *context);
  48. static void _avl_tree_internal_handling(void *x, void *context);
  49. static avl_tree_t _avl_tree_balance(avl_tree_t x);
  50. static void _avl_tree_node_delete_node(avl_tree_node_t node);
  51. static void _avl_tree_debug(avl_tree_t avl, void key_handler(void *x,
  52. int height, int level, void *context), void *context, int level);
  53. void *avl_tree_node_get_key(avl_tree_node_t node)
  54. {
  55. _ASSERT(node, ==, NULL, NULL_POINTER, NULL);
  56. return node->key;
  57. }
  58. int avl_tree_node_get_height(avl_tree_node_t node)
  59. {
  60. _ASSERT(node, ==, NULL, NULL_POINTER, -1);
  61. return node->height;
  62. }
  63. avl_tree_t avl_tree_new(void *key)
  64. {
  65. LOGGER("[avl tree new] key %p\n", key);
  66. return _binary_tree_new(_avl_tree_node_new(key, 1));
  67. }
  68. void avl_tree_delete_node(avl_tree_t avl)
  69. {
  70. LOGGER("[avl tree delete node] node %p\n", avl);
  71. _ASSERT(avl, ==, NULL, NULL_POINTER,);
  72. if (_binary_tree_get_key(avl))
  73. _avl_tree_node_delete_node(_binary_tree_get_key(avl));
  74. _delete(avl);
  75. }
  76. void avl_tree_delete(avl_tree_t avl)
  77. {
  78. LOGGER("[avl tree delete] node %p\n", avl);
  79. _ASSERT(avl, ==, NULL, NULL_POINTER,);
  80. if (_binary_tree_get_left_child(avl))
  81. avl_tree_delete(_binary_tree_get_left_child(avl));
  82. if (_binary_tree_get_right_child(avl))
  83. avl_tree_delete(_binary_tree_get_right_child(avl));
  84. avl_tree_delete_node(avl);
  85. }
  86. int avl_tree_compare_trees(avl_tree_t avl1, avl_tree_t avl2, int compare(
  87. void *x, void *y, void *context), void *context)
  88. {
  89. int result;
  90. LOGGER(
  91. "[avl tree compare trees] tree1 %p, tree2 %p, compare %p, context %p\n",
  92. avl1, avl2, compare, context);
  93. _ASSERT(avl1, ==, NULL, NULL_POINTER, -1);
  94. _ASSERT(avl2, ==, NULL, NULL_POINTER, -1);
  95. _ASSERT(compare, ==, NULL, NULL_POINTER, -1);
  96. stack_push(compare_stack, compare);
  97. result = _binary_tree_compare_trees(avl1, avl2, _avl_tree_internal_compare,
  98. context);
  99. stack_pop(compare_stack);
  100. return result;
  101. }
  102. int avl_tree_is_empty(avl_tree_t avl)
  103. {
  104. LOGGER("[avl tree is empty] node %p\n", avl);
  105. _ASSERT(avl, ==, NULL, NULL_POINTER, -1);
  106. return (_binary_tree_get_parent(avl) == NULL)
  107. && (_binary_tree_get_left_child(avl) == NULL)
  108. && (_binary_tree_get_right_child(avl) == NULL)
  109. && (GET_AVL_MEMBER(_binary_tree_get_key(avl), key) == NULL);
  110. }
  111. int avl_tree_is_leaf(avl_tree_t avl)
  112. {
  113. return _binary_tree_leaf(avl);
  114. }
  115. int avl_tree_get_size(avl_tree_t avl)
  116. {
  117. return _binary_tree_get_size(avl);
  118. }
  119. avl_tree_t avl_tree_get_root(avl_tree_t avl)
  120. {
  121. return _binary_tree_get_root(avl);
  122. }
  123. void *avl_tree_get_key(avl_tree_t avl)
  124. {
  125. return _binary_tree_get_key(avl);
  126. }
  127. avl_tree_t avl_tree_get_parent(avl_tree_t avl)
  128. {
  129. return _binary_tree_get_parent(avl);
  130. }
  131. avl_tree_t avl_tree_get_left_child(avl_tree_t avl)
  132. {
  133. return _binary_tree_get_left_child(avl);
  134. }
  135. avl_tree_t avl_tree_get_right_child(avl_tree_t avl)
  136. {
  137. return _binary_tree_get_right_child(avl);
  138. }
  139. int avl_tree_get_height(avl_tree_t avl)
  140. {
  141. avl_tree_node_t node;
  142. _ASSERT(avl, ==, NULL, NULL_POINTER, -1);
  143. node = _binary_tree_get_key(avl);
  144. return node->height;
  145. }
  146. int avl_tree_get_depth(avl_tree_t avl)
  147. {
  148. return _binary_tree_get_depth(avl);
  149. }
  150. avl_tree_t avl_tree_search(avl_tree_t avl, void *key, int compare(void *x,
  151. void *y, void *context), void *context)
  152. {
  153. /*
  154. * Encapsulate key into a node and search for that node in the tree.
  155. * After the search is completed, free the memory allocated for the auxiliary node
  156. * and return whatever search returns.
  157. */
  158. avl_tree_node_t search_node = _avl_tree_node_new(key, 0);
  159. avl_tree_t result;
  160. stack_push(compare_stack, compare);
  161. result = _binary_search_tree_search(avl, search_node,
  162. _avl_tree_internal_compare, context);
  163. _avl_tree_node_delete_node(search_node);
  164. stack_pop(compare_stack);
  165. return result;
  166. }
  167. avl_tree_t avl_tree_insert(avl_tree_t *avl, void *key, int compare(void *x,
  168. void *y, void *context), void *context, int adjust_root)
  169. {
  170. avl_tree_t add = NULL, add_returned;
  171. avl_tree_node_t add_node = NULL;
  172. avl_tree_node_t root;
  173. _ASSERT(avl, ==, NULL, NULL_POINTER, NULL);
  174. _ASSERT(*avl, ==, NULL, NULL_POINTER, NULL);
  175. _ASSERT(key, ==, NULL, NULL_POINTER, NULL);
  176. _ASSERT(compare, ==, NULL, NULL_POINTER, NULL);
  177. root = (avl_tree_node_t) (*avl)->key;
  178. // NULL key in the root means empty tree
  179. if (root->key == NULL)
  180. {
  181. root->key = key;
  182. return *avl;
  183. }
  184. stack_push(compare_stack, compare);
  185. // Add the node as in a binary search tree and then start tree balancing.
  186. // If the key already exists, I have to free the memory allocated by
  187. // the encapsulation layer.
  188. add_node = _avl_tree_node_new(key, 1);
  189. add = _binary_search_tree_insert(*avl, add_node,
  190. _avl_tree_internal_compare, context);
  191. if (add == NULL)
  192. {
  193. _avl_tree_node_delete_node(add_node);
  194. return NULL;
  195. }
  196. add_returned = add;
  197. add = add->parent;
  198. while (add)
  199. {
  200. LOGGER("[avl_tree_insert] moving from %p to it's parent %p\n", add,
  201. add->parent);
  202. add = _avl_tree_balance(add);
  203. if (add)
  204. add = add->parent;
  205. }
  206. while (adjust_root && (*avl)->parent)
  207. *avl = (*avl)->parent;
  208. stack_pop(compare_stack);
  209. return add_returned;
  210. }
  211. int avl_tree_remove(avl_tree_t *avl, void *key, int compare(void *x, void *y,
  212. void *context), void *context, int adjust_root)
  213. {
  214. avl_tree_t replaced = NULL, my_deleted = NULL;
  215. avl_tree_node_t key_encapsulated = NULL;
  216. _ASSERT(avl, ==, NULL, NULL_POINTER, -1);
  217. _ASSERT(*avl, ==, NULL, NULL_POINTER, -1);
  218. _ASSERT(compare, ==, NULL, NULL_POINTER, -1);
  219. if (compare(key, GET_AVL_MEMBER((*avl)->key, key), context) == 0
  220. && (*avl)->parent == NULL && (*avl)->left == NULL && (*avl)->right
  221. == NULL)
  222. {
  223. avl_tree_delete(*avl);
  224. *avl = NULL;
  225. return 0;
  226. }
  227. stack_push(compare_stack, compare);
  228. /*
  229. * Remove the node as in a binary search tree and then start tree balancing.
  230. */
  231. key_encapsulated = _avl_tree_node_new(key, 0);
  232. replaced = _binary_search_tree_remove(avl, key_encapsulated,
  233. _avl_tree_internal_compare, context, &my_deleted);
  234. _avl_tree_node_delete_node(key_encapsulated);
  235. _ASSERT(my_deleted, ==, NULL, ELEMENT_NOT_FOUND, -1);
  236. avl_tree_delete_node(my_deleted);
  237. while (replaced)
  238. {
  239. LOGGER("[avl_tree_remove] moving from %p to it's parent %p\n",
  240. replaced, replaced->parent);
  241. replaced = _avl_tree_balance(replaced);
  242. if (replaced)
  243. replaced = replaced->parent;
  244. }
  245. while (adjust_root && (*avl)->parent)
  246. *avl = (*avl)->parent;
  247. stack_pop(compare_stack);
  248. return 0;
  249. }
  250. avl_tree_t avl_tree_get_min(avl_tree_t avl)
  251. {
  252. return _binary_search_tree_get_min(avl);
  253. }
  254. avl_tree_t avl_tree_get_max(avl_tree_t avl)
  255. {
  256. return _binary_search_tree_get_max(avl);
  257. }
  258. void avl_tree_preorder(avl_tree_t avl,
  259. void key_handler(void *x, void *context), void *context)
  260. {
  261. stack_push(iterating_handlers, key_handler);
  262. _binary_tree_preorder(avl, _avl_tree_internal_handling, context);
  263. stack_pop(iterating_handlers);
  264. }
  265. void avl_tree_inorder(avl_tree_t avl, void key_handler(void *x, void *context),
  266. void *context)
  267. {
  268. stack_push(iterating_handlers, key_handler);
  269. _binary_tree_inorder(avl, _avl_tree_internal_handling, context);
  270. stack_pop(iterating_handlers);
  271. }
  272. void avl_tree_reverse_inorder(avl_tree_t avl, void key_handler(void *x,
  273. void *context), void *context)
  274. {
  275. stack_push(iterating_handlers, key_handler);
  276. _binary_tree_reverse_inorder(avl, _avl_tree_internal_handling, context);
  277. stack_pop(iterating_handlers);
  278. }
  279. void avl_tree_postorder(avl_tree_t avl,
  280. void key_handler(void *x, void *context), void *context)
  281. {
  282. stack_push(iterating_handlers, key_handler);
  283. _binary_tree_postorder(avl, _avl_tree_internal_handling, context);
  284. stack_pop(iterating_handlers);
  285. }
  286. void avl_tree_level_order(avl_tree_t avl, void key_handler(void *x,
  287. void *context), void *context)
  288. {
  289. stack_push(iterating_handlers, key_handler);
  290. _binary_tree_level_order(avl, _avl_tree_internal_handling, context);
  291. stack_pop(iterating_handlers);
  292. }
  293. void avl_tree_show_indented(avl_tree_t avl, void key_handler(void *x,
  294. int level, void *context), void *context)
  295. {
  296. stack_push(iterating_handlers, key_handler);
  297. _binary_tree_show_indented(avl, _avl_tree_internal_printing_level, context,
  298. 0);
  299. stack_pop(iterating_handlers);
  300. }
  301. static avl_tree_node_t _avl_tree_node_new(void *key, int height)
  302. {
  303. avl_tree_node_t node;
  304. node = (avl_tree_node_t) _new(sizeof(struct _avl_tree_node_t));
  305. node->key = key;
  306. node->height = height;
  307. return node;
  308. }
  309. static int _avl_tree_internal_compare(void *x, void *y, void *context)
  310. {
  311. /*
  312. * This is the callback function and can compare AVL tree nodes
  313. *
  314. * For comparing key from nodes, I save the user comparison function in a global pointers stack
  315. */
  316. int (*user_comparison_function)(void *, void *, void *);
  317. user_comparison_function = stack_top(compare_stack);
  318. _ASSERT(user_comparison_function, ==, NULL, NULL_POINTER, -1);
  319. return user_comparison_function(GET_AVL_MEMBER(x, key),
  320. GET_AVL_MEMBER(y, key), context);
  321. }
  322. static void _avl_tree_internal_printing_level(void *x, int level, void *context)
  323. {
  324. void (*user_printing_function_level)(void *, int, void *);
  325. user_printing_function_level = stack_top(iterating_handlers);
  326. _ASSERT(user_printing_function_level, ==, NULL, NULL_POINTER,);
  327. user_printing_function_level(GET_AVL_MEMBER(x, key), level, context);
  328. }
  329. static void _avl_tree_internal_handling(void *x, void *context)
  330. {
  331. void (*user_handling_function)(void *, void *);
  332. user_handling_function = stack_top(iterating_handlers);
  333. _ASSERT(user_handling_function, ==, NULL, NULL_POINTER,);
  334. user_handling_function(GET_AVL_MEMBER(x, key), context);
  335. }
  336. /*
  337. * Code taken from infoarena.ro
  338. */
  339. static void geth(avl_tree_t node)
  340. {
  341. avl_tree_t left, right;
  342. int left_height, right_height;
  343. left = node->left;
  344. right = node->right;
  345. if (left && left->key)
  346. left_height = ((avl_tree_node_t) node->left->key)->height;
  347. else
  348. left_height = 0;
  349. if (right && right->key)
  350. right_height = ((avl_tree_node_t) node->right->key)->height;
  351. else
  352. right_height = 0;
  353. ((avl_tree_node_t) node->key)->height = 1 + MAX(left_height, right_height);
  354. }
  355. static avl_tree_t rotleft(avl_tree_t x)
  356. {
  357. avl_tree_t y, px, b;
  358. _ASSERT(x, ==, NULL, NULL_POINTER, NULL);
  359. y = x->left;
  360. _ASSERT(y, ==, NULL, NULL_POINTER, x);
  361. LOGGER("[avl tree rotate LEFT] %p %p\n", x, y);
  362. b = y->right;
  363. x->left = b;
  364. y->right = x;
  365. px = x->parent;
  366. x->parent = y;
  367. if (b)
  368. b->parent = x;
  369. y->parent = px;
  370. if (px != NULL)
  371. {
  372. if (px->left == x)
  373. px->left = y;
  374. else
  375. px->right = y;
  376. }
  377. geth(x);
  378. geth(y);
  379. return y;
  380. }
  381. static avl_tree_t rotright(avl_tree_t x)
  382. {
  383. avl_tree_t y, px, b;
  384. _ASSERT(x, ==, NULL, NULL_POINTER, NULL);
  385. y = x->right;
  386. _ASSERT(y, ==, NULL, NULL_POINTER, x);
  387. LOGGER("[avl tree rotate RIGHT] %p %p\n", x, y);
  388. b = y->left;
  389. x->right = b;
  390. y->left = x;
  391. px = x->parent;
  392. x->parent = y;
  393. if (b)
  394. b->parent = x;
  395. y->parent = px;
  396. if (px != NULL)
  397. {
  398. if (px->left == x)
  399. px->left = y;
  400. else
  401. px->right = y;
  402. }
  403. geth(x);
  404. geth(y);
  405. return y;
  406. }
  407. static avl_tree_t _avl_tree_balance(avl_tree_t x)
  408. {
  409. avl_tree_t left, right, aux_left, aux_right;
  410. avl_tree_node_t key;
  411. int left_height, right_height;
  412. _ASSERT(x, ==, NULL, NULL_POINTER, NULL);
  413. left = avl_tree_get_left_child(x);
  414. right = avl_tree_get_right_child(x);
  415. key = x->key;
  416. if (left && left->key)
  417. left_height = GET_AVL_MEMBER(left->key, height);
  418. else
  419. left_height = 0;
  420. if (right && right->key)
  421. right_height = GET_AVL_MEMBER(right->key, height);
  422. else
  423. right_height = 0;
  424. key->height = 1 + MAX(left_height, right_height);
  425. LOGGER("[_avl_tree_balance] node %d, left %d, right %d\n", key->height,
  426. left_height, right_height);
  427. if (left_height > right_height + 1)
  428. {
  429. aux_left = avl_tree_get_left_child(left);
  430. aux_right = avl_tree_get_right_child(left);
  431. if (aux_left && aux_left->key && aux_right && aux_right->key
  432. && GET_AVL_MEMBER(aux_left->key, height)
  433. < GET_AVL_MEMBER(aux_right->key, height))
  434. x->left = rotright(x->left);
  435. x = rotleft(x);
  436. }
  437. else if (right_height > left_height + 1)
  438. {
  439. aux_left = avl_tree_get_left_child(right);
  440. aux_right = avl_tree_get_right_child(right);
  441. if (aux_left && aux_left->key && aux_right && aux_right->key
  442. && GET_AVL_MEMBER(aux_left->key, height)
  443. < GET_AVL_MEMBER(aux_right->key, height))
  444. x->right = rotleft(x->right);
  445. x = rotright(x);
  446. }
  447. return x;
  448. }
  449. static void _avl_tree_node_delete_node(avl_tree_node_t node)
  450. {
  451. _ASSERT(node, ==, NULL, NULL_POINTER,);
  452. _delete(node);
  453. }
  454. void avl_tree_debug(avl_tree_t avl, void key_handler(void *x, int height,
  455. int level, void *context), void *context)
  456. {
  457. _avl_tree_debug(avl, key_handler, context, 0);
  458. }
  459. static void _avl_tree_debug(avl_tree_t avl, void key_handler(void *x,
  460. int height, int level, void *context), void *context, int level)
  461. {
  462. _ASSERT(avl, ==, NULL, NOT_EXCEPTION,);
  463. _ASSERT(key_handler, ==, NULL, NULL_POINTER,);
  464. _avl_tree_debug(avl->right, key_handler, context, level + 1);
  465. key_handler(GET_AVL_MEMBER(avl->key, key),
  466. GET_AVL_MEMBER(avl->key, height), level, context);
  467. _avl_tree_debug(avl->left, key_handler, context, level + 1);
  468. }