heap.h 4.7 KB

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