Skip to content

Binary heap push & pop
one step at a time.

A complete tree stored in an array where every parent is at most its children, so the minimum is always at index 0.

Free to use. Step forward and back at your pace, and share a link to the exact step you are stuck on.

What you actually watch.

The part a dry run on paper loses: the state, at every step.

A complete tree stored in an array where every parent is at most its children, so the minimum is always at index 0. That sentence is easy to agree with and hard to hold in your head on step nine, which is where most people lose the thread.

So the visualiser keeps it in front of you: the tree and its array side by side, as a value sifts up or down. The line of code being executed is highlighted as the picture changes, so the drawing and the code never drift apart. Step forward, step back, or let it play.

Put your own input in. Binary heap push & pop on a clean example teaches you the shape; binary heap push & pop on an empty input, a single element, or one with duplicates teaches you the thing an interviewer asks about.

How it runs, step by step.

The same order the visualiser moves in, so the list and the picture match.

  1. 1Store the tree in an array: a node at i has children at 2i+1 and 2i+2.
  2. 2On a push, put 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. 4The smallest value is always at index 0, in log n per change.

Where people get this one wrong.

These are not made up. Each one is a switch in the tool: turn it on, run the same input, and 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. Pop on the "Pop picks a child" preset.

Inputs worth trying.

The cases that teach something. Each one is a preset in the tool, with the reason it is there.

  • Mixed valuesPops come out 1, 2, 3: smallest first.
  • Pushed in sorted orderNo push ever climbs: each new value is already ≥ its parent.
  • Pushed in reverse orderThe worst case for push: every new value climbs to the root.
  • Pop picks a childRun the bigger-child bug on this one.
  • Equal valuesEqual is fine: ≤ means 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 the heap empty.
  • Push ten, pop noneCheck any index i: heap[i] ≥ heap[(i − 1) / 2].

What it costs.

The two numbers an interviewer will ask for, and what they mean here.

Time
O(log n)counted as real operations while it runs, not quoted from a table
Space
O(n)what the algorithm holds besides the input itself
Pattern
HeapAlways know the smallest, in log n per change.

Once you can see it, go and use it.

Watching is the first ten minutes. The pattern sticks when you solve with it.

Everything in the app, in one place.

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

Free to start: 100 AI credits when you sign up and 100 every month. Start free in the app

About binary heap push & pop.

Open it in the app and the answers are easier to see than to read.

Open the visualiser
What does the binary heap push & pop visualiser show?

Every step of the algorithm on input you choose: the tree and its array side by side, as a value sifts up or down, with the line of code being executed highlighted beside it. You can step forward, step back, or let it play. Spelled visualizer or visualiser, it is the same tool.

What is the time complexity of binary heap push & pop?

O(log n) time and O(n) space. The visualiser counts the real operations as it runs, so you can watch the number grow instead of taking the formula on trust.

Why does my binary heap push & pop code fail on some inputs?

Usually one of the known traps. The visualiser ships the real ones for this algorithm (sink into the bigger child), so you can switch the bug on, run it, and watch exactly where the state goes wrong.

Can I use my own input?

Yes. Type your own array, string or graph and the algorithm runs on that, including the edge cases that break a solution in an interview.

Is it free?

Yes. The app is free to start, with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.