VLVisual Learner

Red-Black Tree

Search / Insert / Delete: O(log n)Height ≤ 2 log₂(n + 1)Self-balancing BST
Insert
–
Pseudocode
1insert v like in a normal BST and colour it RED
2while v's parent is red:
3 uncle ← the parent's sibling
4 uncle red: parent and uncle → black, grandparent → red; v ← grandparent
5 v is an inner child: rotate at the parent so v becomes an outer child
6 rotate at the grandparent; swap the colours of parent and grandparent
7colour the root black
Comparisons
0
Rotations
0
Black height
0
unclerole in the current fix-upnode being checked

What is a Red-Black Tree?

A red-black tree is a binary search tree where every node is coloured red or black, and the colours follow a few rules: the root is black, a red node never has a red child, and every path down to an empty leaf has the same number of black nodes (the black height).

Those rules guarantee the longest path is at most twice the shortest, so the tree stays O(log n) tall. It is a little less strictly balanced than an AVL tree, but needs fewer rotations, which is why it powers std::map, Java's TreeMap and the Linux scheduler.

New nodes are red; fix any red-red pair by recolouring or rotating.

Using the Visualizer

  • • Insert a value: it arrives red. Tags (parent, uncle, grand) show the nodes the fix-up looks at.
  • • A red uncle means only recolouring; a black uncle means one or two rotations.
  • • Press Insert in order to watch 10, 20, …, 80 stay balanced.
  • • Delete a black node to see the four double-black cases.

How It Works

Insert: add a red leaf

Red doesn't change any black height, so only rule 2 (no red-red) can break.

Red uncle: recolour

Parent and uncle turn black, grandparent turns red; continue from the grandparent.

Black uncle: rotate

Inner child: rotate at the parent first. Then rotate at the grandparent and swap colours.

Delete: extra black

Removing a black node leaves a path short of one black; four sibling cases recolour and rotate to repair it.

Implementation

// Red-black tree insertion (left/right mirror cases written out once).
const RED = 1, BLACK = 0;
class Node {
  constructor(val, parent = null) {
    this.val = val; this.parent = parent;
    this.left = this.right = null; this.color = RED;   // new nodes are red
  }
}
const isRed = (n) => n !== null && n.color === RED;

class RedBlackTree {
  constructor() { this.root = null; }

  rotateLeft(x) {
    const y = x.right;
    x.right = y.left; if (y.left) y.left.parent = x;
    y.parent = x.parent;
    if (!x.parent) this.root = y;
    else if (x === x.parent.left) x.parent.left = y; else x.parent.right = y;
    y.left = x; x.parent = y;
  }
  rotateRight(x) { /* mirror of rotateLeft */ }

  insert(val) {
    let p = null, cur = this.root;
    while (cur) { p = cur; cur = val < cur.val ? cur.left : cur.right; }
    const z = new Node(val, p);
    if (!p) this.root = z; else if (val < p.val) p.left = z; else p.right = z;
    this.fixInsert(z);
  }

  fixInsert(z) {
    while (isRed(z.parent)) {
      const p = z.parent, g = p.parent;
      if (p === g.left) {
        const u = g.right;                         // uncle
        if (isRed(u)) {                            // case 1: recolour
          p.color = u.color = BLACK; g.color = RED; z = g;
        } else {
          if (z === p.right) { z = p; this.rotateLeft(z); }   // case 2
          z.parent.color = BLACK;                              // case 3
          z.parent.parent.color = RED;
          this.rotateRight(z.parent.parent);
        }
      } else { /* same with left and right swapped */ }
    }
    this.root.color = BLACK;
  }
}

Properties & Complexity

Time Complexity

  • • Search, insert, delete: O(log n) worst case
  • • Rotations: at most 2 per insert, 3 per delete

Space & Properties

  • • Space: O(n), one colour bit per node
  • • Height: at most 2·log₂(n + 1)

Where It Is Used

  • • C++ std::map / std::set, Java TreeMap / TreeSet
  • • Linux kernel: the CFS scheduler and memory areas
  • • Databases and in-memory ordered indexes

Limitations

  • • Complex delete with many cases
  • • Slightly taller than AVL trees, so lookups can be a bit slower