avltree.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef __AVL_TREE_H__
  2. #define __AVL_TREE_H__
  3. //------------------------------------------------------------------
  4. #ifndef offsetof
  5. #define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER) // 获取"MEMBER成员"在"结构体TYPE"中的位置偏移
  6. #endif
  7. #ifndef container_of
  8. #if 1
  9. // 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针
  10. #define container_of(ptr, type, member) ((type *)((char *)ptr - offsetof(type, member)))
  11. // 此宏定义原文为 GNU C 所写,如下,有些编译器只支持 ANSI C /C99 的,所以作以上修改
  12. #else
  13. #define container_of(ptr, type, member) ({ \
  14. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  15. (type *)( (char *)__mptr - offsetof(type,member) ); })
  16. #endif
  17. #endif
  18. //------------------------------------------------------------------
  19. struct avl_node
  20. {
  21. unsigned long avl_parent;
  22. struct avl_node *avl_left;
  23. struct avl_node *avl_right;
  24. };
  25. struct avl_root
  26. {
  27. struct avl_node *avl_node;
  28. };
  29. #define avl_parent(r) ((struct avl_node *)((r)->avl_parent & (~3)))
  30. //------------------------------------------------------------------
  31. // 获取 avl 节点的平衡因子,值如下
  32. #define avl_scale(r) ((r)->avl_parent & 3)
  33. #define AVL_BALANCED 0 // 节点平衡
  34. #define AVL_TILT_RIGHT 1 // 节点右边比左边高,用 0b01 表示,右倾
  35. #define AVL_TILT_LEFT 2 // 节点左边比右边高,用 0b10 表示,左倾
  36. //------------------------------------------------------------------
  37. // 设置 avl 节点的平衡因子
  38. // 设置 avl 节点为平衡节点
  39. #define avl_set_balanced(r) \
  40. do \
  41. { \
  42. ((r)->avl_parent) &= (~3); \
  43. } while (0)
  44. // 设置 avl 节点为右倾节点
  45. #define avl_set_tilt_right(r) \
  46. do \
  47. { \
  48. (r)->avl_parent = (((r)->avl_parent & ~3) | AVL_TILT_RIGHT); \
  49. } while (0)
  50. // 设置 avl 节点为左倾节点
  51. #define avl_set_tilt_left(r) \
  52. do \
  53. { \
  54. (r)->avl_parent = (((r)->avl_parent & ~3) | AVL_TILT_LEFT); \
  55. } while (0)
  56. //------------------------------------------------------------------
  57. #define avl_entry(ptr, type, member) container_of(ptr, type, member)
  58. // 设置 avl 节点的父节点为 p
  59. static inline void avl_set_parent(struct avl_node *avl, struct avl_node *p)
  60. {
  61. avl->avl_parent = (avl->avl_parent & 3) | (unsigned long)p;
  62. }
  63. void avl_insert(
  64. struct avl_root *root,
  65. struct avl_node *insertnode,
  66. struct avl_node *insertparent,
  67. struct avl_node **avl_link);
  68. void avl_delete(struct avl_root *root, struct avl_node *node);
  69. /* Find logical next and previous nodes in a tree */
  70. extern struct avl_node *avl_next(const struct avl_node *);
  71. extern struct avl_node *avl_prev(const struct avl_node *);
  72. extern struct avl_node *avl_first(const struct avl_root *);
  73. extern struct avl_node *avl_last(const struct avl_root *);
  74. void avl_replace_node(struct avl_node *victim, struct avl_node *new,
  75. struct avl_root *root);
  76. #endif