2

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

Python has a min heap implemented in the heapq module. However, if one would want a max heap, would one have to build from scratch?

Community
  • 1
  • 1
coffee
  • 951
  • 1
  • 8
  • 5

2 Answers2

2

You could multiply your numbers by -1 and use the min heap.

James Thompson
  • 44,924
  • 17
  • 64
  • 82
0

No need to implement a max heap from scratch. You can easily employ a bit of math to turn your min heap into a max heap!

See this and this - but really this SO answer.

Community
  • 1
  • 1
Matt Ball
  • 344,413
  • 96
  • 627
  • 693