Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save goyuninfo/30149ec10383f901c5a831c8e70f2578 to your computer and use it in GitHub Desktop.
Save goyuninfo/30149ec10383f901c5a831c8e70f2578 to your computer and use it in GitHub Desktop.
# Python example code of heap data structure
# importing "heapq" to implement heap queue
import heapq
# initializing list
li = [5, 6, 9, 2, 8]
# using heapify to convert list into heap
heapq.heapify(li)
# printing created heap
print ("The created heap is : ")
print (list(li))
# using heappush() to push elements into heap
# pushes 3
heapq.heappush(li,3)
# printing modified heap
print ("The modified heap after push is : ")
print (list(li))
# using heappop() to pop smallest element
print ("The popped and smallest element is : ")
print (heapq.heappop(li))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment