From 7c6abafae69e3b582dd2d47f47bf4908a9a58283 Mon Sep 17 00:00:00 2001 From: Mukesh Jha Date: Mon, 21 Oct 2019 16:15:09 +0530 Subject: [PATCH] Create Create Heaps --- Create Heaps | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Create Heaps diff --git a/Create Heaps b/Create Heaps new file mode 100644 index 0000000..602a084 --- /dev/null +++ b/Create Heaps @@ -0,0 +1,11 @@ +import heapq +a = [11,22,31,4] +heapq.heapify(a) +print(heapq.heappop(a)) #4 +print(heapq.heappop(a)) #11 +print(heapq.heappop(a)) #22 +print(heapq.heappop(a)) #31 +print(a) #Empty + +#heapq by default makes use of min heap concept. heapify builds heap, heappop pops smallest element from heap, heappush(a,4) pushes 4 in heap. +#For making use of max heap simply multiply all the elements with -1 and then use min heap concept and then change the sign of the value as an when required.