Skip to content

Conversation

@amirsoroush
Copy link

@amirsoroush amirsoroush commented May 13, 2023

Hi,
I made two changes to the current implementation of min-heap:

  1. In both insertion(in .arrange() method) and deletion(in .sink() method) we can stop iterating as soon as we find out that there is no need to swap the values.
  2. In delete_at_location() method, we need to compare the newly inserted node with its parent. Here is an example that reveals the bug:
h = MinHeap()
for i in (1, 10, 2, 11, 12, 3, 4):
    h.insert(i)
print(h.heap)
n = h.delete_at_location(5)
print(h.heap)

After the deletion the output is [0, 1, 10, 2, 11, 4, 3] which is wrong. If we draw this min-heap we could see that index 5(which has the value of 4) is less smaller than index 2(which has the value of 10)

Thanks.

@amirsoroush amirsoroush changed the title Chapter07 - short circuiting sink and arrange method in min-heap Chapter07 - enhance min-heap implementation May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant