What do I use for a max-heap implementation in Python?
问题 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: 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. 回答2: 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!! If you then want to pop elements, use: heapq.heappop(minheap) #