We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents a77d1b0 + 597f973 commit 5419a32Copy full SHA for 5419a32
Data Strucrures/heap.py
@@ -0,0 +1,27 @@
1
+# Python code to demonstrate working of
2
+# heapify(), heappush() and heappop()
3
+
4
+# importing "heapq" to implement heap queue
5
+import heapq
6
7
+# initializing list
8
+li = [5, 7, 9, 1, 3]
9
10
+# using heapify to convert list into heap
11
+heapq.heapify(li)
12
13
+# printing created heap
14
+print ("The created heap is : ",end="")
15
+print (list(li))
16
17
+# using heappush() to push elements into heap
18
+# pushes 4
19
+heapq.heappush(li,4)
20
21
+# printing modified heap
22
+print ("The modified heap after push is : ",end="")
23
24
25
+# using heappop() to pop smallest element
26
+print ("The popped and smallest element is : ",end="")
27
+print (heapq.heappop(li))
0 commit comments