algoritmauzmanı
Sign in
TREN
7.4Other Balanced Trees
Lesson 7.4.1

Red-black trees: the five rules

The keeps its balance strict and rotates often to do so. The red-black tree takes another path: it gives every node a colour and keeps a few rules about the colours. The rules do not keep the tree perfectly balanced, but they never let one path grow more than twice as long as another. This lesson explains the rules and why they work.

What you will learn
  • The five rules of a red-black tree
  • Black height and why the height is at most 2·log₂(n+1)
  • How the red-black tree differs from the AVL tree
7 min

1Five rules

A is a that keeps one more bit in every node: a colour. Empty children (NULL) count as imaginary black leaves; books call them NIL. The tree always keeps these five rules:

  1. Every node is either red or black.
  2. The root is black.
  3. All NIL leaves are black.
  4. Both children of a red node are black: two reds never come one after the other on a path.
  5. Every path from a node down to the NIL leaves below it has the same number of black nodes.
1B3B2R5B8R7B6R4B
A valid red-black tree. Nodes drawn in orange are red; the letter below gives the colour (R red, B black). Every path from the root to a NIL has 2 black nodes (counting the root).

The number in the fifth rule is called the . Some books count it without the starting node and with the NIL, others the other way round. Whichever definition you use, the rule is the same: the count must be equal on every path.

2Why do the rules keep the tree balanced?

The shortest path from the root can consist only of black nodes; its length is the black height, call it b. The longest path has the same number of blacks, but because of the fourth rule at most one red can sit next to each black. So the longest path is at most 2b long. No path can be more than twice as long as the shortest one.

A tree with black height b has at least 2^b - 1 inner nodes, because the black nodes alone must be as full as a perfect tree. Together these two observations give a height of at most 2·log₂(n + 1) for a red-black tree with n nodes. That is why search, insertion and deletion take .

3A tree that breaks a rule

10B22B27B25R40B30R20B
Invalid: 30 is red and its left child 25 is red too. The fourth rule is broken.

In this tree 30 and 25 are red one after the other. Try to fix it by changing a single colour: if you make 30 black, the paths through 30 gain one black and the fifth rule breaks; if you make 25 black, the same happens on the paths to 22 and 27. Fixes in a red-black tree always balance between these two rules like this: when recolouring is not enough, a rotation is done. The next lesson shows how insertion does it.

4Compared with AVL

AVL treeRed-black tree
Balance ruleHeight difference at most 1 at every nodeColour rules: the longest path is at most twice the shortest
Worst heightAbout 1.44·log₂ nAt most 2·log₂(n + 1)
Extra data per nodeThe height (an integer)The colour (one bit)
Rotations on insertAt most one single or doubleAt most two
Rotations on deleteCan be O(log n)At most three
SuitsData that is searched a lotData with frequent inserts and deletes

Red-black trees are used in common libraries. Java's java.util.TreeMap and TreeSet classes are red-black trees, and since Java 8 HashMap turns a bucket that grows too long into a red-black tree. C++ compilers' implementations of std::map and the Linux kernel's process scheduler also use this tree.

A red-black tree is a B-tree with 1 to 3 keys per node (a 2-3-4 tree) written as a binary tree: a black node together with its red children stands for one B-tree node. If you wonder where the rules come from, this link is a good start.

5Try it

Which cannot happen in a red-black tree?

Show the answer

The answer is B. By the fourth rule the children of a red node are black; two reds never come one after the other on a path. A black node's children can be either colour.

The shortest path from the root to a NIL has 3 black nodes. At most how many nodes can the longest path have?

Show the answer

The answer is B. Every path has the same number of blacks, 3. Reds cannot come one after another, so at most one red follows each black: at most 3 + 3 = 6 nodes.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.