Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. import heapq
  2. def heapExample():
  3. heap = []
  4. heapq.heapify(heap)
  5.  
  6. # push element into heap
  7. heapq.heappush(heap, 10)
  8. heapq.heappush(heap, 5)
  9. heapq.heappush(heap, 2)
  10. heapq.heappush(heap, 15)
  11. print(heap) # output [2,10,5,15]
  12.  
  13. #peek min element
  14. print(heap[0]) # output 2
  15.  
  16. # pop elements
  17. print(heapq.heappop(heap)) # output 2
  18. print(heapq.heappop(heap)) # output 5
  19. print(heapq.heappop(heap)) # output 10
  20.  
  21. heapExample()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement