From fff587b4e300e9d91c3288143ec9ae353a6680d9 Mon Sep 17 00:00:00 2001 From: Abh3201 Date: Sat, 23 Oct 2021 12:09:05 +0530 Subject: [PATCH] Python Min Heap File added --- Python/MinHeap.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Python/MinHeap.py diff --git a/Python/MinHeap.py b/Python/MinHeap.py new file mode 100644 index 00000000..2b4acceb --- /dev/null +++ b/Python/MinHeap.py @@ -0,0 +1,21 @@ +#Min Heap +def heapify(li, x, n): + largest = x + left = 2 * x + 1 + right = 2 * x + 2 + if (left < n and li[left] < li[largest]) or (right < n and li[right] < li[largest]): + #print(li[x], x) + if left < n and li[left] < li[largest]: #cheking if left child is exist and its value is less than largest node or not + li[x], li[left] = li[left], li[x] + heapify(li, left, n) + elif right < n: #We just have to check right because if left is not exist then right cannot be exist. + li[x], li[right] = li[right], li[x] + heapify(li, right, n) + + +li = [3,9,2,1,4,5] +n = len(li) +for x in range((n // 2) - 1, -1, -1): + heapify(li, x, n) + +print(li)