원소들에게 우선순위를 매겨서 넣을 때의 순서와 상관없이 뺄 때에는 우선순위가 높은 원소부터 빼내는 것
파이썬의 리스트를 마치 힙처럼 다룰 수 있게 도와준다.
from heapq import heappush, heappop
heap = []
heappush(heap,3)
heappush(heap,8)
heappush(heap,2)
print(heap)
>>>[2, 8, 3]
heappop(heap)
>>> 2
print(heapq)
>>>[8, 3]
원소가 들어있는 리스트를 힙으로 만들어주는 함수
from heapq import heapify
heap = [2, 8, 3, 1, 5]
heapify(heap)
print(heap)
>>> [1, 2, 3, 8, 5]