I'm trying to use the Python (2.0) built-in min-heap data structure from the heapq module (https://docs.python.org/3/library/heapq.html) to build a max-heap. To do that I simply use the negative of the numbers I need to push into my heap.
Using this (max-heap version):
import heapq
h=[]
for i in xrange(10):
heapq.heappush(h,-i)
print h
I get something which doesn't look correct:
[0]
[-1, 0]
[-2, 0, -1]
[-3, -2, -1, 0]
[-4, -3, -1, 0, -2]
[-5, -3, -4, 0, -2, -1]
[-6, -3, -5, 0, -2, -1, -4]
[-7, -6, -5, -3, -2, -1, -4, 0]
[-8, -7, -5, -6, -2, -1, -4, 0, -3]
[-9, -8, -5, -6, -7, -1, -4, 0, -3, -2]
The min-heap version instead looks fine:
import heapq
h=[]
for i in xrange(10):
heapq.heappush(h,i)
print h
As you can see:
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
What am I missing?
I've checked other SE questions/answers (e.g., python topN max heap, use heapq or self implement?, What do I use for a max-heap implementation in Python?, etc.) but they don't mention this issue.