Skip to the workspace
Pebellslearn by building
Trees, Heaps & Tries  ›  Lesson

Heaps & Live Charts

Heaps 25 minA tree in a list that always knows its smallest item

dsa / Heaps & Live Charts

Explore each scene at your own pace.

01 · Keep the live chart's leader always one step away

Keep the live chart's leader always one step away

Heaps & Live Charts

Definition
A heap is a complete binary tree, stored in a plain list, in which every parent is no larger than its children (a min-heap), so the smallest item is always at the front and each push or pop costs O(log n).
In plain words
Think of a tournament ladder where the winner always sits at the top. You do not need everyone sorted, only the best one in view, and when it leaves, the next best rises with just a few swaps.

In short: a heap is a binary tree with two rules. Shape: it's complete — every level is full except perhaps the last, which fills from the left, so it needs no pointers at all: just a plain list, with parent(i) = (i-1)//2, left(i) = 2i+1, right(i) = 2i+2. Order: in a min-heap, every parent is ≤ its children, so the smallest item always sits at index 0 — a far weaker promise than "sorted", and much cheaper to keep. Pushing sifts the new item up while it beats its parent; popping moves the last item to the root and sifts it down while a child beats it. Each move travels one level at a time, so both cost O(log n).

A node climbing two levels up a small heap during a push, beside the same heap with its root sinking two levels down during a pop — every swap crosses exactly one level

Keep the live chart's leader always one step away

Tunebox's live chart shows the most-played tracks right now, and plays arrive by the thousand every minute. Re-sorting after every play costs O(n log n) each time; a heap absorbs each change in O(log n). Because the shape has no gaps, parent and child links become arithmetic on a list:

watch it move · array

The heap is just this list, and every parent and child link is an index calculation on it.

Index 0 is the root, so the smallest value always sits at the front of the list. 6 cells. At index 0.

1 / 3
watch it move · tree

The same six values drawn as a complete tree, filled level by level from the left with no gaps.

The root is index 0, and it beats both of its children, 8 and 5. Binary tree, 6 nodes, height 3. At node 3.

1 / 3
What does this print?
heap = [3, 8, 5, 12, 9, 7]
i = 2
print(heap[(i - 1) // 2], heap[2 * i + 1])

❓ Cross-question — "So a heap is just a sorted list?" No, and that's the point. [3, 8, 5, 12, 9, 7] isn't sorted — 8 comes before 5. A heap only promises each parent beats its own children; that weaker promise is much cheaper to keep, and it still puts the winner at index 0.

Scene 1 of 10
Project context and further resources
You're building a piece ofTunebox — discovery
This piece — top_charts(): Keeps the most-played tracks on top as plays roll in.
Scenario Plays stream into Tunebox all day, and the live chart must always show the most-played tracks. Each refresh counts the plays and pulls the top k off a heap.
top_charts.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
top_charts(plays, k) → listThe k most-played titles, most plays first, ties alphabetical.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.