diff --git a/Data Strucrures/heap.py b/Data Strucrures/heap.py new file mode 100644 index 0000000..235ed26 --- /dev/null +++ b/Data Strucrures/heap.py @@ -0,0 +1,27 @@ +# Python code to demonstrate working of +# heapify(), heappush() and heappop() + +# importing "heapq" to implement heap queue +import heapq + +# initializing list +li = [5, 7, 9, 1, 3] + +# using heapify to convert list into heap +heapq.heapify(li) + +# printing created heap +print ("The created heap is : ",end="") +print (list(li)) + +# using heappush() to push elements into heap +# pushes 4 +heapq.heappush(li,4) + +# printing modified heap +print ("The modified heap after push is : ",end="") +print (list(li)) + +# using heappop() to pop smallest element +print ("The popped and smallest element is : ",end="") +print (heapq.heappop(li))