Skip to content

Commit 5419a32

Browse files
authored
Merge pull request #67 from chandankumar1307/patch-5
Heap Program in Python
2 parents a77d1b0 + 597f973 commit 5419a32

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Data Strucrures/heap.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
print (list(li))
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

Comments
 (0)