Skip to the workspace
Pebellslearn by building
Balanced Trees  ›  Lesson

AVL and Red-Black Trees

AVL & Red-Black Trees 32 minA balance factor caught after every insert, fixed by one of four rotation recipes

dsa / AVL and Red-Black Trees

Explore each scene at your own pace.

01 · Keeping a leaderboard fast after a losing streak

Keeping a leaderboard fast after a losing streak

AVL and Red-Black Trees

Definition
An AVL tree is a binary search tree that keeps the heights of every node's two subtrees within 1 of each other, restoring this after each insert with one or two rotations. A red-black tree uses a looser colour-based rule that needs fewer rotations.
In plain words
It is a filing system that tidies up the moment one drawer gets noticeably fuller than its neighbour, so no search ever has to go very deep. A red-black tree allows a bit more untidiness in exchange for doing less rearranging.

In short: an AVL tree rebalances after every insert so that for every node, the heights of its two subtrees never differ by more than 1 — its balance factor (bf = height(left) - height(right)) always stays in {-1, 0, 1}. An insert can push several ancestors' bf to ±2 at once, but fixing the lowest unbalanced one with one of four rotation recipes — LL, LR, RR or RL — restores that subtree to its pre-insert height, which makes every ancestor above it balanced again too. A red-black tree relaxes the rule (a looser "never more than twice as deep on one side") and fixes violations with cheaper recolouring most of the time, trading a slightly taller tree for fewer rotations per insert.

Four complete before/after case cards — LL and RR as straight lines fixed with one rotation, LR and RL as zigzags fixed with two — each node badged with its balance factor

Keeping a leaderboard fast after a losing streak

A game's leaderboard indexes players by score. Scores update constantly, and a bad run of matches can insert a long streak of similar, nearly-sorted scores — exactly the pattern that degrades a plain BST. An AVL-backed index keeps lookups at O(log n) no matter how the scores arrive, by checking the balance factor after every single insert and fixing it immediately if it's broken.

python

bf(70) comes out -1 — its right child 90 makes that side one level taller — and that extra level echoes up to the root: bf(50) is -1 too, while bf(30), a leaf, is still 0. None of these cross ±2 yet, so no rotation fires. That's the check an AVL insert runs after every single node on its way back up, not just at the root.

Scene 1 of 10
Project context and further resources
You're building a piece ofQuarry — the index layer
This piece — avl_insert_sequence(): Inserts and rebalances so the whole index never grows past O(log n) deep.
Scenario Quarry's index layer gets keys in every order a real workload produces — sorted batches, random inserts, bursts of near-duplicates — and it has to guarantee O(log n) lookups regardless.
avl_insert_sequence.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
avl_insert_sequence(values) → dictIn-order sequence and final height after inserting every value into an AVL tree.
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.