← Course home

Lookup and priority · Lesson 31 of 52

Balanced search trees

Explain how balancing keeps search-tree height logarithmic through rotations.

Full lessonAbout 10 min

You will learn to explain why balance bounds search-tree height and relate local rotations to preserved key order.

Recommended first: Binary search tree operations.

On this page 6 sections

The idea

Inserting 1, then 2, then 3 leaves 2 at the root. Inorder is still 1, 2, 3.

A plain binary search tree becomes a chain when keys arrive in sorted order. Every operation then walks n nodes. A balanced tree keeps the height logarithmic by local rotations. The library's left-leaning red-black tree encodes a 2-3 tree. A red link to a left child means two keys share a node. Right-leaning red links are rotated away. Insert 1, then 2, then 3. The inorder walk is 1, 2, 3, so the search-tree order survives. The root key is 2, not 3, so the tree did not become a chain. select(1) returns 2, the second smallest key. rank(2) returns 1, the number of keys strictly smaller than 2. Those order statistics come from subtree sizes stored on the nodes.

A worked trace

StepStateWhat changed
Insertroot = 1After inserting 1, the root is 1. Inorder is [1].
Insertroot = 2After inserting 2, the root is 2. Inorder is [1, 2].
Insertroot = 2After inserting 3, the root is 2. Inorder is [1, 2, 3].

Why it is correct

A rotation changes which node is the parent and which is the child. It does not change inorder. The keys that were left of a node stay left, and the keys that were right stay right. Only the shape moves. The red-black rules limit how many black links appear on a path from the root to a leaf. A red link cannot simply stack on another red link. That bound is why the height stays logarithmic after the sorted inserts. select walks by subtree size. If the left subtree holds more than k keys, the answer is in the left subtree. Otherwise it is the node itself or a key in the right subtree.

A common mistake

A rotation changes the parent. It does not reverse inorder.

What it costs

Insert, delete, rank, and select are logarithmic because the height is logarithmic. A rotation is a constant number of pointer writes. Extra memory is one node per key, plus the color and the size. The plain search tree uses the same memory and loses the height bound. Use the balanced tree when the input order is not under your control and later lookups must stay fast. Insert is Θ(log n) time, and the nodes use Θ(n) extra memory.

Run it

python
from bitsandbytes.trees.llrb import LeftLeaningRedBlackTreetree = LeftLeaningRedBlackTree()for value in (1, 2, 3):    tree.insert(value)print(tree.inorder())# prints: [1, 2, 3] 

Practice and next step

Open Heap operations next. Heap operations expose the extreme key without sorting the whole array.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.trees.llrb

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Sorted inserts stay balanced

Inserting 1, then 2, then 3 into a left-leaning red-black tree leaves 2 at the root. Inorder is still 1, 2, 3.

root
1

State

  1. 01chosen

After inserting 1, the root is 1. Inorder is [1].

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1Keys 1, 2, 3 arrive in order. Where does a left-leaning red-black tree put the root?
2A rotation changes the parent of a node. What stays true?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.