What do I use for a max-heap implementation in Python?

0 votes
asked Mar 23, 2010 by douglas-mayle

Python includes the heapq module for min-heaps, but I need a max heap. What should I use for a max-heap implementation in Python?

7 Answers

0 votes
answered Mar 23, 2010 by daniel-stutzbach

The easiest way is to invert the value of the keys and use heapq. For example, turn 1000.0 into -1000.0 and 5.0 into -5.0.

0 votes
answered Mar 23, 2010 by rlotun

If you are inserting keys that are comparable but not int-like, you could potentially override the comparison operators on them (i.e. <= become > and > becomes <=). Otherwise, you can override heapq._siftup in the heapq module (it's all just Python code, in the end).

0 votes
answered Mar 13, 2014 by lijo-joseph

You can use

import heapq
listForTree = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]    
heapq.heapify(listForTree)             # for a min heap
heapq._heapify_max(listForTree)        # for a maxheap!!
0 votes
answered Mar 3, 2016 by zhe-he

I implemented a max heap version of heapq and submitted it to PyPI. (Very slight change of heapq module CPython code.)




pip install heapq_max


tl;dr: same as heapq module except adding ‘_max’ to all functions.

heap_max = []                           # creates an empty heap
heappush_max(heap_max, item)            # pushes a new item on the heap
item = heappop_max(heap_max)            # pops the largest item from the heap
item = heap_max[0]                      # largest item on the heap without popping it
heapify_max(x)                          # transforms list into a heap, in-place, in linear time
item = heapreplace_max(heap_max, item)  # pops and returns largest item, and
                                    # adds new item; the heap size is unchanged
0 votes
answered Mar 6, 2016 by isaac-turner

The solution is to negate your values when you store them in the heap, or invert your object comparison like so:

import heapq

class MaxHeapObj(object):
  def __init__(self,val): self.val = val
  def __lt__(self,other): return self.val > other.val
  def __eq__(self,other): return self.val == other.val
  def __str__(self): return str(self.val)

Example of a max-heap:

maxh = []
x = maxh[0].val # fetch max value
x = heapq.heappop(maxh).val # pop max value

But you have to remember to wrap and unwrap your values, which requires knowing if you are dealing with a min- or max-heap.

MinHeap, MaxHeap classes

Adding classes for MinHeap and MaxHeap objects can simplify your code:

class MinHeap(object):
  def __init__(self): self.h = []
  def heappush(self,x): heapq.heappush(self.h,x)
  def heappop(self): return heapq.heappop(self.h)
  def __getitem__(self,i): return self.h[i]
  def __len__(self): return len(self.h)

class MaxHeap(MinHeap):
  def heappush(self,x): heapq.heappush(self.h,MaxHeapObj(x))
  def heappop(self): return heapq.heappop(self.h).val
  def __getitem__(self,i): return self.h[i].val

Example usage:

minh = MinHeap()
maxh = MaxHeap()
# add some values
# fetch "top" values
print(minh[0],maxh[0]) # "4 12"
# fetch and remove "top" values
print(minh.heappop(),maxh.heappop()) # "4 12"
0 votes
answered Sep 15, 2017 by jasonleonhard

Allowing you to chose an arbitrary amount of largest or smallest items

import heapq
heap = [23, 7, -4, 18, 23, 42, 37, 2, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
print(heapq.nlargest(3, heap))  # [42, 42, 37]
print(heapq.nsmallest(3, heap)) # [-4, -4, 2]
0 votes
answered Sep 15, 2017 by sebastian-nielsen

Multiple the values with -1, and there you go. All the highest numbers are now the lowest and virca versa.

Just remember that when you pop an element to multiple it with -1 again, in order to get the original value again.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter