Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Min Heap File added #717

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Python/MinHeap.py
Original file line number Diff line number Diff line change
@@ -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)