liveBook · Manning

Bubble up

As explained in Advanced Algorithms and Data Structures

Bubble up is a heap-maintenance procedure that moves an element toward the root while its priority violates the parent ordering condition.

Bubble up restores a heap after an inserted or updated element violates parent ordering. In a max-heap, a higher-priority child moves toward the root until its parent has equal or greater priority. section 2.6.1 section 2.6.3

Implement the procedure by repeatedly comparing the element with its parent:

  • Start at the element's array index, commonly the newly appended final position. section 2.6.1 section 2.6.3
  • Compute the parent index and compare priorities. section 2.6.1
  • Swap when the current element has higher priority; otherwise stop because the heap ordering is restored. section 2.6.1
  • Repeat until the element reaches the root or no longer outranks its parent. section 2.6.1

Priority 9 moves above priority 8

In a ternary max-heap, the element at index 7 has priority 9 while its parent at index 2 has priority 8. Swap them, then compare the moved element with its new parent at index 0, whose priority 10 stops further bubbling. section 2.6.1

Listing 2.3 An optimization of the bubbleUp method
function bubbleUp(pairs, index=|pairs|-1)                     #1
  current ← pairs[index]                                      #2
  while index > 0 do                                          #3
    parentIndex ← getParentIndex(index)                       #4
    if pairs[parentIndex].priority < current.priority then    #5
      pairs[index] ← pairs[parentIndex]                       #6
      index ← parentIndex                                     #7
    else                                                      #8
      break
  pairs[index] ← current                                      #9

Use the optimized pseudocode to see how one saved element moves upward while parent entries shift downward, reducing assignments. section 2.6.1

How Advanced Algorithms and Data Structures covers it

How deep it goes
Explains parent traversal, stopping conditions, swaps, and a one-assignment optimization, but not formal correctness proofs.
Examples
Uses a task-management priority example and a ternary max-heap containing priorities 9, 8, and 10.
What it assumes
Assumes familiarity with heap ordering, parent-child relationships, array-backed heaps, and max-heap priorities.
What you can do after
Build it from scratch with parent-index calculations, conditional swaps, or the optimized single-assignment variant.
How it connects
Connects heap invariants, priority queues, d-way heaps, insertion, updates, and heap height.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.