Skip to content

Commit

Permalink
heap: fix heap_remove()
Browse files Browse the repository at this point in the history
Remove should shuffle items in both directions, not just down. It is
required, because `max` node could be not the actual maximum value in
the tree.

fix #1267

Signed-off-by: Fedor Indutny <[email protected]>
  • Loading branch information
indutny committed May 23, 2014
1 parent 70c4256 commit e002340
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/heap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ HEAP_EXPORT(void heap_remove(struct heap* heap,
break;
heap_node_swap(heap, child, smallest);
}

/* Walk up the subtree and check that each parent is less than the node
* this is required, because `max` node is not guaranteed to be the
* actual maximum in tree
*/
while (child->parent != NULL && less_than(child, child->parent))
heap_node_swap(heap, child->parent, child);
}

HEAP_EXPORT(void heap_dequeue(struct heap* heap, heap_compare_fn less_than)) {
Expand Down

0 comments on commit e002340

Please sign in to comment.