Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import heapq
- def heapExample():
- heap = []
- heapq.heapify(heap)
- # push element into heap
- heapq.heappush(heap, 10)
- heapq.heappush(heap, 5)
- heapq.heappush(heap, 2)
- heapq.heappush(heap, 15)
- print(heap) # output [2,10,5,15]
- #peek min element
- print(heap[0]) # output 2
- # pop elements
- print(heapq.heappop(heap)) # output 2
- print(heapq.heappop(heap)) # output 5
- print(heapq.heappop(heap)) # output 10
- heapExample()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement