Skip to main content

Data Structures · Heaps, priority queues, and disjoint sets

`Bubble-Down`(A,i,n) is specified as follows on a 1-based array that is a complete…

Problem

`Bubble-Down`(\(A,i,n\)) is specified as follows on a 1-based array that is a complete binary tree. Let \(L\) and \(R\) be the left and right children of \(i\) (if they exist). If \(i\) has no child with a strictly smaller key than \(A[i]\), return. Otherwise let \(j\) be a child of \(i\) of minimum key (break ties toward the left child) and swap \(A[i]\) with \(A[j]\); then recurse (or iterate) at \(j\). Prove the following invariant: if every subtree rooted at a proper descendant of \(i\) is a min-heap, then after `Bubble-Down`(\(A,i,n\)) returns, the subtree rooted at \(i\) is a min-heap. Your proof must handle the cases of zero, one, and two children, and must argue that a swap cannot violate heap order at the parent of the original \(i\) (the parent is outside the subtree under consideration). Conclude that Floyd's `Build-Heap`, which calls `Bubble-Down` on nodes in decreasing index order, produces a min-heap.

Hint

The recursive step only fires when \(i\) is larger than a child; after the swap, \(i\) holds that smaller child and the hole has moved down.

Check your work

Work the problem yourself first. Then open it in Training to check your answer and read the full worked solution.

The answer check and full solution for this problem come with ProofAnvil Practice membership ($19 USD monthly). See membership. Or start with the free Data Structures sample problem: Try the free sample problem.

More Data Structures practice problems

Back to Data Structures

An original ProofAnvil practice problem, written for this course. ProofAnvil is a practice course, not a homework-answer service.