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.
- 1Store the tree in an array, where a node at i has children at 2i+1 and 2i+2.
- 2On a push, place the value at the end and let it climb past bigger parents.
- 3On a pop, take index 0, move the last value to the top and let it sink.
- 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.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
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 visualiserWhat 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.
- 1CodeStory checks your level, from heap basics to interview ready.
- 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
- 3You decide. No pressure, no payment on the call.