LOGBOOK

HELP

Quiz Entry - updated: 2026.11.26

Inserting the keys 4, 5, 3, 2, 1 in this order into an empty min-heap: what does the array (index 1 onwards) look like after each insert?

[4] → [4, 5] → [3, 5, 4] → [2, 3, 4, 5] → [1, 2, 4, 5, 3].

Five array states after inserting 4, 5, 3, 2 and 1, ending at 1, 2, 4, 5, 3

* Each new key is appended at the end and swapped towards the front while its parent is larger. *

Each insert appends at index n + 1 and then upheaps (swap with the parent at index ⌊i/2⌋ while the parent is larger):

  1. insert 4: [4]. A single node is a heap.
  2. insert 5: [4, 5]. Parent 4 ≤ 5, no swap.
  3. insert 3: placed at index 3 → [4, 5, 3]. Parent (index 1) is 4 > 3: swap → [3, 5, 4].
  4. insert 2: placed at index 4 → [3, 5, 4, 2]. Parent (index 2) is 5 > 2: swap → [3, 2, 4, 5]. Parent (index 1) is 3 > 2: swap → [2, 3, 4, 5].
  5. insert 1: placed at index 5 → [2, 3, 4, 5, 1]. Parent (index 2) is 3 > 1: swap → [2, 1, 4, 5, 3]. Parent (index 1) is 2 > 1: swap → [1, 2, 4, 5, 3].

As a tree, the final heap is 1 → (2, 4), 2 → (5, 3).

Notice that the result is not sorted (5 comes before 3), and that a different insertion order of the same keys would generally give a different heap. Both are expected: the heap only guarantees the order along each root-to-leaf path.

From Quiz: ADS / Priority Queues and Heaps | Updated: Nov 26, 2026