Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (47.6k points)

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?

1 Answer

0 votes
by (106k points)

The max-heap can be used for as follows:-

import heapq 

listForTree = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

heapq.heapify(listForTree) 

heapq._heapify_max(listForTree) 

If you then want to pop elements, use:

heapq.heappop(minheap) 

heapq._heappop_max(maxheap) 

Related questions

0 votes
1 answer
asked Aug 29, 2019 in Java by Nigam (4k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 11, 2019 in Java by Krishna (2.6k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.6k answers

500 comments

108k users

Browse Categories

...