Created
January 2, 2021 00:34
-
-
Save goyuninfo/30149ec10383f901c5a831c8e70f2578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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