Skip to content

Binary Heap Push and Pop
Step by Step

A binary heap stores a complete tree in an array where every parent is at most its children, so the minimum always sits at index 0.

Step through every climb and sink in both directions, then share a link to the step where a parent broke.

What You Watch While Values Climb and Sink

The tree, its array and the moving value change on every step.

Every parent holds a value at most as big as its children, so the smallest value waits at the top.

CodeStory's visualiser shows the tree and its array side by side while a value climbs or sinks. Marked code lines follow every move, and you can step back, step forward or play.

Open Mixed values first. Then load Pop picks a child to see where the heap breaks.

How a Binary Heap Pushes and Pops

Steps below follow the array on your screen.

  1. 1Store the tree in an array, where a node at i has children at 2i+1 and 2i+2.
  2. 2On a push, place the value at the end and let it climb past bigger parents.
  3. 3On a pop, take index 0, move the last value to the top and let it sink.
  4. 4Index 0 always holds the smallest value, at log n per change.

Heap Mistakes the Visualiser Shows

A switch in the visualiser turns the mistake on, so you can watch the state go wrong.

  • Bug: sink into the bigger childSift-down swaps with the bigger child, so the smaller one ends up below a bigger parent. Load Pop picks a child to see it.

Binary Heap Inputs Worth Trying

Eight presets cover climbs, sinks, equal values and empty heaps.

  • Mixed valuesPops come out as 1, 2, 3, smallest first.
  • Pushed in sorted orderNo push climbs, since every new value is already at least its parent.
  • Pushed in reverse orderWorst case for push, because every new value climbs to the root.
  • Pop picks a childSwitch on the bigger child bug on this one.
  • Equal valuesEqual values are fine because a value stops next to its equals.
  • One valuePop the only value and the heap is empty.
  • Pop more than you pushedThe last two pops find an empty heap.
  • Push ten, pop noneCheck any index i and heap[i] ≥ heap[(i − 1) / 2].

Binary Heap Cost in Time and Space

Every push or pop moves a value along one branch of the tree.

Time
O(log n)counted from the climbs and sinks you step through
Space
O(n)the array that stores the tree
Pattern
HeapKnow the smallest value in log n per change.

Practise Heaps Beyond Push and Pop

Watching shows the climb and solving trains you to reach for a heap.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Binary Heap Visualiser FAQs

Open the visualiser and follow the heap step by step.

Open the visualiser
What does the binary heap visualiser show?

CodeStory's visualiser shows the tree and its array side by side while a value climbs or sinks, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of a binary heap?

O(log n) time per push or pop and O(n) space. Live counts show the climbs and sinks instead of a formula on a slide.

Why does my binary heap code fail on some inputs?

Sinking into the bigger child is a common cause. Switch the bug on in the visualiser and watch a smaller child end up below a bigger parent.

Can I use my own input?

Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Heaps? Get a Free Callback

Tell CodeStory which heap step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from heap basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.