Skip to content
Merged
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
22 changes: 15 additions & 7 deletions heapify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,42 @@ def heapify(arr, n, i):
left = 2 * i + 1
right = 2 * i + 2

if left < n and arr[left] > arr[smallest]:
if left < n and arr[left] < arr[smallest]:
smallest = left
if right < n and arr[right] > arr[smallest]:
if right < n and arr[right] < arr[smallest]:
smallest = right

if smallest != i:
arr[i], arr[smallest] = arr[smallest], arr[i]
heapify(arr, n, smallest)

def buildMinHeap(arr):

def build_min_heap(arr):
n = len(arr)
startIdx = n // 2 - 1
for i in range(startIdx, -1, -1):
heapify(arr, n, i)


def heapify_arrays_from_file(file_path):
with open(file_path, 'r') as file:
for line in file:
arr = line.strip()
# Convert the line to a list of integers
arr = ast.literal_eval(line.strip())

buildMinHeap(arr)
build_min_heap(arr)

print("Min Heap:", arr)


def main():
file = sys.argv[0]
heapify_arrays_from_file(file)
# Get the first command-line argument as the input file path
if len(sys.argv) > 1:
file_path = sys.argv[1]
heapify_arrays_from_file(file_path)
else:
print("Please provide a file path as an argument.")


if __name__ == "__main__":
main()