SHARE
TWEET

Untitled

a guest Sep 19th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top