Red-Black Tree Overview | Generated by AI

Home 2025.10

What is a Red-Black Tree?

A red-black tree is a type of self-balancing binary search tree (BST) used in computer science to store and retrieve data efficiently. It maintains balance by ensuring the tree remains roughly balanced after insertions and deletions, guaranteeing O(log n) time complexity for search, insert, and delete operations, where n is the number of nodes.

Key Properties

Red-black trees enforce five rules to stay balanced:

  1. Node Colors: Every node is either red or black.
  2. Root Node: The root is always black.
  3. Leaf Nodes: All NIL (null) leaves are black.
  4. Red Child Rule: A red node cannot have red children (no two reds in a row).
  5. Black Height Rule: Every path from a node to its descendant leaves has the same number of black nodes (ensures balance).

These rules prevent the tree from becoming skewed (like a linked list), which could degrade performance to O(n).

How It Works

Rotations are local operations that preserve the BST property while fixing balance.

Visual Example

Here’s a simple Markdown representation of a balanced red-black tree for keys 10, 20, 30, 40, 50 (R = red, B = black):

       B(30)
      /     \
   B(20)    B(40)
  /   \       \
R(10) R(25)  R(50)

Advantages

For deeper dives, check implementations in languages like Python (via sortedcontainers library) or C++ (std::set). If you need code examples or specifics, let me know!


Back

x-ai/grok-4-fast

Donate