Skip to the workspace
Pebellslearn by building
Balanced Trees  ›  Lesson

Rotations & Why a BST Degenerates

Rotations & Balance 24 minThree pointer moves fix a lopsided spot in a BST without changing what an in-order walk reads

dsa / Rotations & Why a BST Degenerates

Explore each scene at your own pace.

01 · Inserting ids in order

Inserting ids in order

Rotations & Why a BST Degenerates

Definition
A tree rotation is a local rearrangement of a parent and one child, done with a few pointer changes, that shifts height from a deeper side to a shallower one while keeping a binary search tree's left-to-right ordering intact.
In plain words
Think of a mobile hanging crooked because one side is too long. You lift a different piece to be the hanging point so the weight evens out. Everything keeps its left-to-right order, and only a few strings change where they are tied.

In short: a binary search tree is only fast if it's short. Insert keys in sorted order and a plain BST never branches — every new key goes right (or always left), and it degenerates into a linked list: search goes from O(log n) to O(n). A rotation fixes one lopsided spot in three pointer moves, without touching most of the tree and without breaking the BST ordering — everything still reads left-to-right the same way. It's the one move every self-balancing tree in this section builds on.

Two small trees side by side: a straight diagonal chain of 1, 2, 3 on the left labelled "before", and a balanced 3-node tree with 2 on top on the right labelled "after", with one curved arrow from the chain's root to the balanced root showing the pivot

Inserting ids in order

Quarry's index layer sometimes gets keys that arrive already sorted — auto-incrementing ids, timestamps, a bulk import from another system in order. A plain BST insert (less than: go left, else go right) handles this correctly, but not fast:

python

Height 8 for 8 keys — every key has exactly one child, a straight line. Search, insert and delete all become O(n). A balanced arrangement of the same 8 keys has height 4 (⌈log₂9⌉), and the gap only gets worse as n grows: 20 keys in order gives height 20 instead of about 5.

Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the index layer
This piece — single_rotation_report(): Fixes one lopsided spot in a BST with 3 pointer moves, keeping every key in the same order.
Scenario Quarry's index layer gets a batch of auto-incrementing ids inserted in order, and the plain BST underneath it is quietly turning into a linked list.
single_rotation_report.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
single_rotation_report(values, direction) → dictHeight before and after one rotation at the root, and the in-order sequence.
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.