마음만 바쁜 사람
[Python] min-hip을 이용한 Priority Queue
ALGORITHM/Data Structure 2022. 4. 22. 14:11

- 최소 힙(min-heap)의 구조 - 삽입/삭제: O(logN) import heapq로 사용 가능 push: heapq.heappush(hq, item) pop: heapq.heappop(hq) import heapq pq = [] heapq.heappush(pq, 6) heapq.heappush(pq, 12) heapq.heappop(pq) heapify - 리스트 x를 heap으로 변환 x = [4, 3, 1, 2, 5, 6] print(x) # [4, 3, 1, 2, 5, 6] heapq.heapify(x) print(x) # [1, 2, 4, 3, 5, 6] heapq를 사용하는 이유? from queue import PriorityQueue로 일반 우선순위 큐도 사용할 수 있다. 하지만 이..