Browse curriculum

Red-Black Tree Visualization

A self-balancing binary search tree where each node is colored red or black, ensuring the tree remains approximately balanced during insertions and deletions. Step through it in the interactive Red-Black Tree visualization below.

READY

 

Insert values to watch the fix-up. A new node is always red; the rules then recolour and rotate until no red node has a red parent.

 

Value

Intuition

Standard BSTs can degrade into linked lists (O(n)) with sorted input. AVL trees are strictly balanced but require frequent rotations. Red-Black trees trade some balance for faster insertion/deletion (fewer rotations), guaranteeing O(log n) operations while maintaining a height at most 2 * log(n+1).

Concept

A Red-Black Tree is a BST with 5 properties:

  1. Nodes are Red or Black.
  2. Root is always Black.
  3. Leaves (NIL) are Black.
  4. If a node is Red, both children are Black (no double reds).
  5. Every path from a node to its descendant NIL nodes has the same number of Black nodes.

How Red-Black Tree Works

  • Insertion: Insert like BST (red node). Fix violations (recoloring or rotation) if parent is also red.
  • Deletion: Complex. If a black node is deleted, tree height reduces, violating black-height. Requires restructuring.
  • Rotations: Left and Right rotations are used to rebalance.
  • Recoloring: Changing node colors to fix parent-child conflicts.

Red-Black Tree Visualization, Step by Step

Insertion Logic:
  1. Insert node as RED.
  2. If parent is Black: Done.
  3. If parent is Red: Check uncle's color.
  4. Uncle Red: Recolor Parent+Uncle Black, Grandparent Red. Repeat for GP.
  5. Uncle Black: Rotate (LL, RR, LR, RL cases) and recolor.

When to Use

  • General purpose self-balancing tree (used in C++ std::map, Java TreeMap).
  • When insertions/deletions are frequent (faster than AVL).
  • Implementing associative arrays.

When NOT to Use

  • When lookups are the dominant operation (AVL is strictly balanced, so slightly faster lookups).
  • Simple scenarios where O(n) is acceptable.

How to Identify

Keywords: "Map Implementation", "Self-Balancing", "Associative Array", "Ordered Set".

Practice it on interview problems

LeetCode problems solved with the Depth-First Search pattern, each with a step-by-step visualizer:

All Depth-First Search problems

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description

Compare its cost with every other algorithm in the Big O cheat sheet.

Frequently Asked Questions

What does this red black tree visualization show?

Each insert, step by step: the new node goes in red as in a plain BST, then the fix-up either recolors the parent, uncle and grandparent or rotates the subtree, until no red node has a red child and the root is black.


What are the 5 properties of a Red-Black tree?

1. Every node is either red or black. 2. The root is always black. 3. All NIL leaves are black. 4. No two red nodes are adjacent (a red node cannot have a red child). 5. Every path from a node to descendant leaves contains the exact same number of black nodes (black-height).


How does Red-Black tree rebalancing work on insertion?

When a new red node creates a double-red violation: if the uncle is red, recolor parent and uncle to black and grandparent to red. If the uncle is black, perform tree rotations (LL, RR, LR, RL) and swap parent/grandparent colors.


Red-Black tree vs AVL tree: which is faster?

AVL trees are more strictly balanced, making lookups slightly faster. Red-Black trees require fewer rotations during insertions and deletions, making them faster for dynamic, write-heavy workloads.


Where are Red-Black trees used in real software?

They are used in standard library maps and sets across major languages: C++ STL (std::map, std::set), Java TreeMap / TreeSet, and Linux kernel process scheduling (Completely Fair Scheduler).