Skip to the workspace
Pebellslearn by building
Balanced Trees  ›  Lesson

Balance by Coin Flip

Treaps & Skip Lists 28 minA treap keeps the BST shape but bubbles the highest random priority to the top with the same rotations as lesson 1; a skip list reaches the same O(log n) search with shorter express lanes built by a coin flip at insert time

dsa / Balance by Coin Flip

Explore each scene at your own pace.

01 · A matchmaking queue sorted by rating

A matchmaking queue sorted by rating

Balance by Coin Flip

Definition
A treap is a binary search tree in which each node also gets a random priority and the highest priority sits at the top of every subtree. A skip list adds randomly promoted shortcut layers of linked lists. Both stay short on average.
In plain words
Instead of carefully measuring balance, you let coin flips decide. A treap lets lucky nodes rise towards the top, like a lottery for who sits highest. A skip list is a train line with a few express stops above the local stops, where chance picks which stations get one.

In short: the last two lessons kept a tree balanced by bookkeeping — a balance factor, four rotation cases, a parent-child colour rule. A treap gets O(log n) height a different way: give every node a random priority when it's inserted, and keep the highest priority at the top of every subtree, like a heap — using the exact same rotations from lesson 1, just triggered by a coin flip instead of a balance check. A skip list reaches the same O(log n) search cost by building shorter "express lane" linked lists above the full list, one promotion coin flip per insert, so a search drops down a level every time it would overshoot. Both trade bookkeeping for randomness, and both guarantee O(log n) on average, not in the worst case — an unlucky run of coin flips can still produce a chain.

Three stacked horizontal lanes, the top with 3 stops, the middle with 6, the bottom with every item — one path drops from the top lane to the middle lane, then to the bottom lane, each drop happening where the next stop would overshoot the target

A matchmaking queue sorted by rating

A matchmaking service indexes waiting players by skill rating so it can quickly find "closest rating available." Players queue and leave in an order nobody controls — sometimes a burst of similarly-rated players joins at once, which is exactly the pattern that degrades a plain BST. A treap keeps the index short without checking a balance factor on every insert:

python

Height 5 for 8 ratings, inserted in the exact ascending order that gave lesson 1's plain BST a height of 8. Nothing checked a balance factor — the shape came entirely from which node happened to draw the highest priority. A genuinely unlucky sequence of coin flips could still produce a chain, but across many inserts that's as unlikely as flipping 8 heads in a row.

Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the index layer
This piece — treap_insert_sequence(): Balances by random priority instead of bookkeeping, with the same O(log n) guarantee on average.
Scenario Quarry's index layer wants O(log n) inserts without tracking a balance factor on every node — a treap gets there by paying a coin flip instead.
treap_insert_sequence.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
treap_insert_sequence(values, seed) → dictThe in-order sequence, final height, and the random priorities drawn for each insert, of a treap built from values.
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.