0% found this document useful (0 votes)
8 views1 page

Lab 4 Q 1 B

Uploaded by

Milica Markovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Lab 4 Q 1 B

Uploaded by

Milica Markovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

procedure removeMin(H):

// Remove the minimum element from the heap

// Step 1: Check if the heap is not empty


if heapSize < 1:
print("Heap is empty")
return

// Step 2: Swap the root with the last element


swap(H[1], H[heapSize])

// Step 3: Decrease the heap size


heapSize = heapSize - 1

// Step 4: Restore the heap property by moving the new root down
i = 1
while true:
leftChild = 2 * i
rightChild = 2 * i + 1
smallest = i

// Find the smallest among the root, left child, and right child
if leftChild ≤ heapSize and H[leftChild] < H[smallest]:
smallest = leftChild

if rightChild ≤ heapSize and H[rightChild] < H[smallest]:


smallest = rightChild

// If the smallest is not the root, swap and continue heapifying


if smallest ≠ i:
swap(H[i], H[smallest])
i = smallest
else:
break

procedure swap(a, b):


// Swap the values of two elements
temp = a
a = b
b = temp

You might also like