heap.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef HW_HEAP_H_
  2. #define HW_HEAP_H_
  3. #include <assert.h>
  4. #include "hdef.h"
  5. struct heap_node {
  6. struct heap_node* parent;
  7. struct heap_node* left;
  8. struct heap_node* right;
  9. };
  10. typedef int (*heap_compare_fn)(const struct heap_node* lhs, const struct heap_node* rhs);
  11. struct heap {
  12. struct heap_node* root;
  13. int nelts; // if compare is less_than, root is min of heap
  14. // if compare is larger_than, root is max of heap
  15. heap_compare_fn compare;
  16. };
  17. static inline void heap_init(struct heap* heap, heap_compare_fn fn) {
  18. heap->root = NULL;
  19. heap->nelts = 0;
  20. heap->compare = fn;
  21. }
  22. // replace s with r
  23. static inline void heap_replace(struct heap* heap, struct heap_node* s, struct heap_node* r) {
  24. // s->parent->child, s->left->parent, s->right->parent
  25. if (s->parent == NULL) heap->root = r;
  26. else if (s->parent->left == s) s->parent->left = r;
  27. else if (s->parent->right == s) s->parent->right = r;
  28. if (s->left) s->left->parent = r;
  29. if (s->right) s->right->parent = r;
  30. if (r) {
  31. //*r = *s;
  32. r->parent = s->parent;
  33. r->left = s->left;
  34. r->right = s->right;
  35. }
  36. }
  37. static inline void heap_swap(struct heap* heap, struct heap_node* parent, struct heap_node* child) {
  38. assert(child->parent == parent && (parent->left == child || parent->right == child));
  39. struct heap_node* pparent = parent->parent;
  40. struct heap_node* lchild = child->left;
  41. struct heap_node* rchild = child->right;
  42. struct heap_node* sibling = NULL;
  43. if (pparent == NULL) heap->root = child;
  44. else if (pparent->left == parent) pparent->left = child;
  45. else if (pparent->right == parent) pparent->right = child;
  46. if (lchild) lchild->parent = parent;
  47. if (rchild) rchild->parent = parent;
  48. child->parent = pparent;
  49. if (parent->left == child) {
  50. sibling = parent->right;
  51. child->left = parent;
  52. child->right = sibling;
  53. } else {
  54. sibling = parent->left;
  55. child->left = sibling;
  56. child->right = parent;
  57. }
  58. if (sibling) sibling->parent = child;
  59. parent->parent = child;
  60. parent->left = lchild;
  61. parent->right = rchild;
  62. }
  63. static inline void heap_insert(struct heap* heap, struct heap_node* node) {
  64. // get last => insert node => sift up
  65. // 0: left, 1: right
  66. int path = 0;
  67. int n,d;
  68. ++heap->nelts;
  69. // traverse from bottom to up, get path of last node
  70. for (d = 0, n = heap->nelts; n >= 2; ++d, n>>=1) {
  71. path = (path << 1) | (n & 1);
  72. }
  73. // get last->parent by path
  74. struct heap_node* parent = heap->root;
  75. while(d > 1) {
  76. parent = (path & 1) ? parent->right : parent->left;
  77. --d;
  78. path >>= 1;
  79. }
  80. // insert node
  81. node->parent = parent;
  82. if (parent == NULL) heap->root = node;
  83. else if (path & 1) parent->right = node;
  84. else parent->left = node;
  85. // sift up
  86. if (heap->compare) {
  87. while (node->parent && heap->compare(node, node->parent)) {
  88. heap_swap(heap, node->parent, node);
  89. }
  90. }
  91. }
  92. static inline void heap_remove(struct heap* heap, struct heap_node* node) {
  93. if (heap->nelts == 0) return;
  94. // get last => replace node with last => sift down and sift up
  95. // 0: left, 1: right
  96. int path = 0;
  97. int n,d;
  98. // traverse from bottom to up, get path of last node
  99. for (d = 0, n = heap->nelts; n >= 2; ++d, n>>=1) {
  100. path = (path << 1) | (n & 1);
  101. }
  102. --heap->nelts;
  103. // get last->parent by path
  104. struct heap_node* parent = heap->root;
  105. while(d > 1) {
  106. parent = (path & 1) ? parent->right : parent->left;
  107. --d;
  108. path >>= 1;
  109. }
  110. // replace node with last
  111. struct heap_node* last = NULL;
  112. if (parent == NULL) {
  113. return;
  114. }
  115. else if (path & 1) {
  116. last = parent->right;
  117. parent->right = NULL;
  118. }
  119. else {
  120. last = parent->left;
  121. parent->left = NULL;
  122. }
  123. if (last == NULL) {
  124. if (heap->root == node) {
  125. heap->root = NULL;
  126. }
  127. return;
  128. }
  129. heap_replace(heap, node, last);
  130. node->parent = node->left = node->right = NULL;
  131. if (!heap->compare) return;
  132. struct heap_node* v = last;
  133. struct heap_node* est = NULL;
  134. // sift down
  135. while (1) {
  136. est = v;
  137. if (v->left) est = heap->compare(est, v->left) ? est : v->left;
  138. if (v->right) est = heap->compare(est, v->right) ? est : v->right;
  139. if (est == v) break;
  140. heap_swap(heap, v, est);
  141. }
  142. // sift up
  143. while (v->parent && heap->compare(v, v->parent)) {
  144. heap_swap(heap, v->parent, v);
  145. }
  146. }
  147. static inline void heap_dequeue(struct heap* heap) {
  148. heap_remove(heap, heap->root);
  149. }
  150. #endif // HW_HEAP_H_