Abstract

Derived from the 2-4 Tree, the Red-Black Tree is a self-balancing BST that achieves a worst-case time complexity. Unlike the AVL Tree, which requires two passes (down and up) for updates, the Red-Black Tree is designed to maintain balance in a single pass through the tree.


1. The Four Properties

For a BST to be a valid Red-Black Tree, it must satisfy these strict coloring and structural rules:

  1. Node Color: Every node is either Red or Black.
  2. Root Property: The root of the tree is always Black.
  3. Red Property: Red nodes cannot have red children (no two red nodes in a row).
  4. Black Property: For any node, every path from that node to a null reference (leaf) must contain the same number of black nodes.

NOTE

Null References (empty children) are conceptually treated as Black nodes.


2. Mathematical Height Guarantee

We can prove that the height of a Red-Black Tree with internal nodes is .

  • Black Height : The number of black nodes from to a leaf (excluding ).
  • Internal Nodes: A subtree rooted at has at least internal nodes.
  • Height Relation: Since red nodes cannot be adjacent, at least half the nodes on any path (excluding the root) must be black. Thus, .
  • Conclusion: . The height is at most roughly twice the optimal BST height.

3. Insertion Algorithm: The Single-Pass Method

Nodes are always inserted as Red by default to preserve the Black Property (Property 4). We then fix any violations of the Red Property (Property 3) as we traverse.

Case 1: The Root

If the tree is empty, the new node becomes the root. Color it Black.

Case 2: Black Node with Two Red Children (Color Flip)

During your descent, if you encounter a black node with two red children:

  1. Recolor the parent Red.
  2. Recolor both children Black.
  3. If this creates a red-red violation with the grandparent, fix it using rotations (Cases 3 or 4).

Case 3: Red-Red Violation (Straight Line)

If the new red node and its red parent form a straight line (e.g., both are left children):

  1. Perform a Single AVL Rotation.
  2. Recolor: Set the new “top” node of the rotation to Black and its children to Red.

Case 4: Red-Red Violation (Kink)

If the nodes form a “kink” shape:

  1. Perform a Single Rotation to transform the kink into a Straight Line.
  2. Apply the Case 3 Fix.


4. Performance Trade-offs: AVL vs. Red-Black

FeatureAVL TreeRed-Black Tree
BalanceStricter (Factor of )Looser (Black height equality)
Height
FindFaster (shorter paths)Slower
Insert/RemoveSlower (2 passes + more rotations)Faster (1 pass + fewer rotations)
Use CaseRead-heavy datasetsWrite-heavy/General purpose

TIP

This is why Red-Black Trees are used for standard library implementations like std::map and std::set in C++.


5. Summary of BST Evolution

Data StructureBest CaseWorst CaseBalance Strategy
Basic BSTNone
Randomized BSTRandom Priorities (Treaps)
AVL TreeHeight-based rotations
Red-Black TreeSingle-pass color rules