Module 13 of 39

Red-black trees: balance, colors, and rotations

See how red-black trees use color rules and rotations to keep search paths short.

An ordinary binary search tree can turn into a long chain. If it does, the “discard half” intuition no longer applies: a search may visit nearly every node. A red-black tree is a binary search tree with extra color rules that prevent that shape from becoming too tall.

Background for this page. You need the search-tree rule and the idea of tree height: the number of edges on a longest downward path. Empty leaves participate in the red-black color rules. Related explanations: Binary search trees: search, insertion, and deletion · Augmenting search trees: subtree sizes and rank.

Each node is red or black. The root is black. A red node cannot have a red child. Every path from a node down to an empty leaf has the same number of black nodes. We treat the empty leaves as black when stating these rules. Together they ensure that no root-to-leaf path is more than twice as long as another. The tree’s height is O(log n), so search, insertion, and deletion remain O(log n).

Keys 2, 4, 6, 8 form a chain in insertion order, but a balanced tree can branch.

The colors do not change the order of the keys. Search still compares the target with a node and chooses left or right. Colors matter when an insertion or deletion would break a balancing rule. The repair may recolor nodes or rotate a small part of the tree.

A rotation changes links without changing the inorder sequence of keys. If 10 has right child 15, and 15 has left child 12, a left rotation at 10 makes 15 the local parent, 10 its left child, and 12 the right child of 10. The sorted order remains 10, 12, 15. This is the reason a rotation can repair shape while preserving search behavior.

Insertion first places a new key where an ordinary search tree would. It colors the new node red, then repairs any red-red conflict. Deletion is more involved because removing a black node can upset the number of black nodes along paths. The full case analysis matters when implementing the structure, but the central claim is clear: local repairs preserve the search-tree order and restore the color rules.

There is a cost to these guarantees. The code and its tests are more complex than for an unbalanced tree. Most applications should use a tested library implementation rather than write a red-black tree casually. The point of learning one is to see how a small number of invariants can guarantee a global performance bound.

A rotation preserves sorted order

Let x have right child y. Let A be x’s left subtree, B be y’s left subtree, and C be y’s right subtree. Their order is A, x, B, y, C. A left rotation makes y the local root, x its left child, and B the right subtree of x. The order remains A, x, B, y, C. Link changes can therefore repair shape without changing sorted order. They do not alone establish all color rules. Equal black counts and the ban on consecutive red nodes together bound height; the full proof also counts the minimum nodes for a given black height.

Practice. Why would coloring every node black satisfy the “no red parent with a red child” rule but still fail to make every arbitrary binary search tree a valid red-black tree? Why is the inorder order unchanged by a rotation?

Answer. An all-black chain violates the equal black-count rule: paths through different leaves contain different numbers of black nodes. A rotation changes parent-child links among nearby nodes but leaves the smaller keys before and larger keys after them.

Try another case. Where must B move during the left rotation?

Reasoning. B becomes x’s right subtree. Writing down all three subtrees before changing references helps prevent a lost link.

View the full course and choose another topic.

Image preview

Fit