1
0

avl.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2006-2020, 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. * 2022-11-14 WangXiaoyao Optimize for generic use case
  10. * and performance
  11. */
  12. #ifndef __UTIL_TREE_AVL_H__
  13. #define __UTIL_TREE_AVL_H__
  14. #include <rtdef.h>
  15. #include <stdint.h>
  16. struct util_avl_struct
  17. {
  18. struct util_avl_struct *avl_left;
  19. struct util_avl_struct *avl_right;
  20. struct util_avl_struct *parent;
  21. size_t height;
  22. };
  23. #define AVL_ROOT ((struct util_avl_struct *)0)
  24. struct util_avl_root
  25. {
  26. struct util_avl_struct *root_node;
  27. };
  28. void util_avl_rebalance(struct util_avl_struct *node,
  29. struct util_avl_root *root);
  30. void util_avl_remove(struct util_avl_struct *node, struct util_avl_root *root);
  31. static inline void util_avl_link(struct util_avl_struct *new_node,
  32. struct util_avl_struct *parent,
  33. struct util_avl_struct **nodeplace)
  34. {
  35. new_node->avl_left = AVL_ROOT;
  36. new_node->avl_right = AVL_ROOT;
  37. new_node->parent = parent;
  38. new_node->height = 1;
  39. *nodeplace = new_node;
  40. }
  41. static inline struct util_avl_struct *util_avl_next(
  42. struct util_avl_struct *node)
  43. {
  44. struct util_avl_struct *successor = 0;
  45. if (node)
  46. {
  47. if (node->avl_right)
  48. {
  49. node = node->avl_right;
  50. while (node->avl_left)
  51. node = node->avl_left;
  52. successor = node;
  53. }
  54. else
  55. {
  56. while ((successor = node->parent) && (node == successor->avl_right))
  57. node = successor;
  58. }
  59. }
  60. return successor;
  61. }
  62. static inline struct util_avl_struct *util_avl_prev(
  63. struct util_avl_struct *node)
  64. {
  65. struct util_avl_struct *predecessor = 0;
  66. if (node)
  67. {
  68. if (node->avl_left)
  69. {
  70. node = node->avl_left;
  71. while (node->avl_right)
  72. node = node->avl_right;
  73. predecessor = node;
  74. }
  75. else
  76. {
  77. while ((predecessor = node->parent) &&
  78. (node == predecessor->avl_left))
  79. node = predecessor;
  80. }
  81. }
  82. return predecessor;
  83. }
  84. static inline struct util_avl_struct *util_avl_first(struct util_avl_root *root)
  85. {
  86. struct util_avl_struct *first = root->root_node;
  87. if (first)
  88. {
  89. while (first->avl_left)
  90. first = first->avl_left;
  91. }
  92. return first;
  93. }
  94. static inline struct util_avl_struct *util_avl_last(struct util_avl_root *root)
  95. {
  96. struct util_avl_struct *last = root->root_node;
  97. if (last)
  98. {
  99. while (last->avl_right)
  100. last = last->avl_right;
  101. }
  102. return last;
  103. }
  104. #endif /* __UTIL_TREE_AVL_H__ */