From 597f973c71d68ea33a6824c7ab080026b48d7de6 Mon Sep 17 00:00:00 2001 From: Chandan Kumar Saha <70543351+chandankumar1307@users.noreply.github.com> Date: Fri, 1 Oct 2021 01:10:55 +0530 Subject: [PATCH] Heap Program in Python Python code to demonstrate the working of heapify(), heappush() and heappop() --- Data Strucrures/heap.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Data Strucrures/heap.py 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))