MIN HEAP ARRAY BASED
class MinHeap:
def __init__(self):
[Link] = []
def insert(self, value):
[Link](value) # Add at the end
self._heapify_up(len([Link]) - 1) # Restore heap property
def _heapify_up(self, index):
parent_index = (index - 1) // 2
while index > 0 and [Link][index] < [Link][parent_index]:
[Link][index], [Link][parent_index] = [Link][parent_index],
[Link][index] # Swap
index = parent_index
parent_index = (index - 1) // 2
def get_heap(self):
return [Link]
# Test the Min Heap
values = [10, 7, 11, 5, 4, 13]
min_heap = MinHeap()
for v in values:
min_heap.insert(v)
# Print the heap as an array
print("Min Heap as array:", min_heap.get_heap())