LOGBOOK

HELP

Quiz Entry - updated: 2026.11.26

What are the steps of inserting a key into a heap?

Three steps: (1) find the insertion node z, the next free position after the last node; (2) store the new key in z, which becomes the new last node; (3) restore the heap-order property with upheap.

  1. Find the insertion node z. To keep the complete shape, the new node must go into the first free slot in level order: to the right of the current last node on the bottom level, or, if that level is full, as the leftmost node of a new level.
  2. Store the key k in z. The tree now has the correct shape again, and z is the new last node.
  3. Restore heap order. The new key may be smaller than its parent, which would break key(child) ≥ key(parent). Upheap repairs this by moving k up towards the root.

Example: in the heap with root 2, children 5 and 6, and 9, 7 below the 5, the next free slot is the left child of 6. Inserting key 1 there puts 1 below 6, which violates the order; upheap then moves it up to the root.

The order of steps matters: the shape is fixed first (step 1 can never break it) and only then the order (step 3 never changes the shape, it only swaps keys).

Go deeper:

  • doc Binary heap: insert — the same three steps with a worked example and the "up-heap / sift-up" naming.

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