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
  • 1,121
  • 2
  • 9
  • 5

2 Answers2

2

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

James Thompson
  • 46,512
  • 18
  • 65
  • 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
  • 354,903
  • 100
  • 647
  • 710