lwp_avl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-10-12 Jesven first version
  9. */
  10. #include <rtthread.h>
  11. #include <lwp_avl.h>
  12. /**
  13. * @brief Rebalances an AVL tree after insertion or deletion
  14. *
  15. * @param[in,out] nodeplaces_ptr Pointer to stack of node pointers that need rebalancing
  16. * @param[in] count Number of nodes in the stack that need rebalancing
  17. *
  18. * @note This function performs AVL tree rebalancing by checking and correcting height imbalances
  19. * between left and right subtrees. It handles:
  20. * - Left-left case (single right rotation)
  21. * - Left-right case (double rotation: left then right)
  22. * - Right-right case (single left rotation)
  23. * - Right-left case (double rotation: right then left)
  24. */
  25. static void lwp_avl_rebalance(struct lwp_avl_struct ***nodeplaces_ptr, int count)
  26. {
  27. for (; count > 0; count--)
  28. {
  29. struct lwp_avl_struct **nodeplace = *--nodeplaces_ptr;
  30. struct lwp_avl_struct *node = *nodeplace;
  31. struct lwp_avl_struct *nodeleft = node->avl_left;
  32. struct lwp_avl_struct *noderight = node->avl_right;
  33. int heightleft = heightof(nodeleft);
  34. int heightright = heightof(noderight);
  35. if (heightright + 1 < heightleft)
  36. {
  37. struct lwp_avl_struct *nodeleftleft = nodeleft->avl_left;
  38. struct lwp_avl_struct *nodeleftright = nodeleft->avl_right;
  39. int heightleftright = heightof(nodeleftright);
  40. if (heightof(nodeleftleft) >= heightleftright)
  41. {
  42. node->avl_left = nodeleftright;
  43. nodeleft->avl_right = node;
  44. nodeleft->avl_height = 1 + (node->avl_height = 1 + heightleftright);
  45. *nodeplace = nodeleft;
  46. }
  47. else
  48. {
  49. nodeleft->avl_right = nodeleftright->avl_left;
  50. node->avl_left = nodeleftright->avl_right;
  51. nodeleftright->avl_left = nodeleft;
  52. nodeleftright->avl_right = node;
  53. nodeleft->avl_height = node->avl_height = heightleftright;
  54. nodeleftright->avl_height = heightleft;
  55. *nodeplace = nodeleftright;
  56. }
  57. }
  58. else if (heightleft + 1 < heightright)
  59. {
  60. struct lwp_avl_struct *noderightright = noderight->avl_right;
  61. struct lwp_avl_struct *noderightleft = noderight->avl_left;
  62. int heightrightleft = heightof(noderightleft);
  63. if (heightof(noderightright) >= heightrightleft)
  64. {
  65. node->avl_right = noderightleft;
  66. noderight->avl_left = node;
  67. noderight->avl_height = 1 + (node->avl_height = 1 + heightrightleft);
  68. *nodeplace = noderight;
  69. }
  70. else
  71. {
  72. noderight->avl_left = noderightleft->avl_right;
  73. node->avl_right = noderightleft->avl_left;
  74. noderightleft->avl_right = noderight;
  75. noderightleft->avl_left = node;
  76. noderight->avl_height = node->avl_height = heightrightleft;
  77. noderightleft->avl_height = heightright;
  78. *nodeplace = noderightleft;
  79. }
  80. }
  81. else
  82. {
  83. int height = (heightleft < heightright ? heightright : heightleft) + 1;
  84. if (height == node->avl_height)
  85. break;
  86. node->avl_height = height;
  87. }
  88. }
  89. }
  90. /**
  91. * @brief Removes a node from an AVL tree while maintaining balance
  92. *
  93. * @param[in] node_to_delete The node to be removed from the AVL tree
  94. * @param[in,out] ptree Pointer to the root node pointer of the AVL tree
  95. *
  96. * @note This function removes the specified node from the AVL tree and performs
  97. * necessary rebalancing operations. It handles both cases where the node
  98. * has no left child (simple removal) and where it has a left child (finding
  99. * the rightmost node in the left subtree as replacement).
  100. * It uses a stack to track the removal path for rebalancing.
  101. */
  102. void lwp_avl_remove(struct lwp_avl_struct *node_to_delete, struct lwp_avl_struct **ptree)
  103. {
  104. avl_key_t key = node_to_delete->avl_key;
  105. struct lwp_avl_struct **nodeplace = ptree;
  106. struct lwp_avl_struct **stack[avl_maxheight];
  107. uint32_t stack_count = 0;
  108. struct lwp_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
  109. struct lwp_avl_struct **nodeplace_to_delete;
  110. for (;;)
  111. {
  112. struct lwp_avl_struct *node = *nodeplace;
  113. if (node == AVL_EMPTY)
  114. {
  115. return;
  116. }
  117. *stack_ptr++ = nodeplace;
  118. stack_count++;
  119. if (key == node->avl_key)
  120. break;
  121. if (key < node->avl_key)
  122. nodeplace = &node->avl_left;
  123. else
  124. nodeplace = &node->avl_right;
  125. }
  126. nodeplace_to_delete = nodeplace;
  127. if (node_to_delete->avl_left == AVL_EMPTY)
  128. {
  129. *nodeplace_to_delete = node_to_delete->avl_right;
  130. stack_ptr--;
  131. stack_count--;
  132. }
  133. else
  134. {
  135. struct lwp_avl_struct ***stack_ptr_to_delete = stack_ptr;
  136. struct lwp_avl_struct **nodeplace = &node_to_delete->avl_left;
  137. struct lwp_avl_struct *node;
  138. for (;;)
  139. {
  140. node = *nodeplace;
  141. if (node->avl_right == AVL_EMPTY)
  142. break;
  143. *stack_ptr++ = nodeplace;
  144. stack_count++;
  145. nodeplace = &node->avl_right;
  146. }
  147. *nodeplace = node->avl_left;
  148. node->avl_left = node_to_delete->avl_left;
  149. node->avl_right = node_to_delete->avl_right;
  150. node->avl_height = node_to_delete->avl_height;
  151. *nodeplace_to_delete = node;
  152. *stack_ptr_to_delete = &node->avl_left;
  153. }
  154. lwp_avl_rebalance(stack_ptr, stack_count);
  155. }
  156. /**
  157. * @brief Inserts a new node into an AVL tree while maintaining balance
  158. *
  159. * @param[in] new_node The new node to be inserted into the AVL tree
  160. * @param[in,out] ptree Pointer to the root node pointer of the AVL tree
  161. *
  162. * @note Uses a stack to track the insertion path for rebalancing
  163. */
  164. void lwp_avl_insert(struct lwp_avl_struct *new_node, struct lwp_avl_struct **ptree)
  165. {
  166. avl_key_t key = new_node->avl_key;
  167. struct lwp_avl_struct **nodeplace = ptree;
  168. struct lwp_avl_struct **stack[avl_maxheight];
  169. int stack_count = 0;
  170. struct lwp_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
  171. for (;;)
  172. {
  173. struct lwp_avl_struct *node = *nodeplace;
  174. if (node == AVL_EMPTY)
  175. break;
  176. *stack_ptr++ = nodeplace;
  177. stack_count++;
  178. if (key < node->avl_key)
  179. nodeplace = &node->avl_left;
  180. else
  181. nodeplace = &node->avl_right;
  182. }
  183. new_node->avl_left = AVL_EMPTY;
  184. new_node->avl_right = AVL_EMPTY;
  185. new_node->avl_height = 1;
  186. *nodeplace = new_node;
  187. lwp_avl_rebalance(stack_ptr, stack_count);
  188. }
  189. /**
  190. * @brief Finds a node in an AVL tree by key
  191. *
  192. * @param[in] key The key to search for in the AVL tree
  193. * @param[in] ptree Pointer to the root node of the AVL tree
  194. *
  195. * @return struct lwp_avl_struct* Pointer to the found node, or NULL if not found
  196. *
  197. * @note This function searches the AVL tree for a node with the specified key.
  198. * It performs a standard binary search by comparing keys and traversing
  199. * left or right subtrees accordingly.
  200. */
  201. struct lwp_avl_struct *lwp_avl_find(avl_key_t key, struct lwp_avl_struct *ptree)
  202. {
  203. for (;;)
  204. {
  205. if (ptree == AVL_EMPTY)
  206. {
  207. return (struct lwp_avl_struct *)0;
  208. }
  209. if (key == ptree->avl_key)
  210. break;
  211. if (key < ptree->avl_key)
  212. ptree = ptree->avl_left;
  213. else
  214. ptree = ptree->avl_right;
  215. }
  216. return ptree;
  217. }
  218. /**
  219. * @brief Recursively traverses an AVL tree and applies a function to each node
  220. *
  221. * @param[in] ptree Pointer to the root node of the AVL tree
  222. * @param[in] fun Callback function to apply to each node
  223. * @param[in,out] arg Additional argument passed to the callback function
  224. *
  225. * @return int Returns the last result from the callback function (0 if all nodes processed)
  226. *
  227. * @note This function performs an in-order traversal of the AVL tree, applying the
  228. * provided callback function to each node. The traversal can be stopped early
  229. * if the callback returns a non-zero value.
  230. */
  231. int lwp_avl_traversal(struct lwp_avl_struct *ptree, int (*fun)(struct lwp_avl_struct *, void *), void *arg)
  232. {
  233. int ret;
  234. if (!ptree)
  235. {
  236. return 0;
  237. }
  238. if (ptree->avl_left)
  239. {
  240. ret = lwp_avl_traversal(ptree->avl_left, fun, arg);
  241. if (ret != 0)
  242. {
  243. return ret;
  244. }
  245. }
  246. ret = (*fun)(ptree, arg);
  247. if (ret != 0)
  248. {
  249. return ret;
  250. }
  251. if (ptree->avl_right)
  252. {
  253. ret = lwp_avl_traversal(ptree->avl_right, fun, arg);
  254. if (ret != 0)
  255. {
  256. return ret;
  257. }
  258. }
  259. return ret;
  260. }
  261. /**
  262. * @brief Finds the first (leftmost) node in an AVL tree
  263. *
  264. * @param[in] ptree Pointer to the root node of the AVL tree
  265. *
  266. * @return struct lwp_avl_struct* Pointer to the leftmost node, or NULL if tree is empty
  267. *
  268. * @note This function traverses the AVL tree to find the leftmost node, which represents
  269. * the minimum element in the tree. It's commonly used for ordered traversal starting point.
  270. */
  271. rt_weak struct lwp_avl_struct* lwp_map_find_first(struct lwp_avl_struct* ptree)
  272. {
  273. if (ptree == AVL_EMPTY)
  274. {
  275. return (struct lwp_avl_struct *)0;
  276. }
  277. while (1)
  278. {
  279. if (!ptree->avl_left)
  280. {
  281. break;
  282. }
  283. ptree = ptree->avl_left;
  284. }
  285. return ptree;
  286. }