Red-Black Trees
Intro to
Presented by: Mobin Nesari
Overview
Introduction & Motivation
1.
Properties of Red-Black Trees
2.
Tree Rotations
3.
Insertion
4.
Deletion
5.
Examples
6.
Common Questions & Pitfalls
7.
Summary
8.
References
9.
Introduction &
Motivation
Why Balanced Trees?
LOOKS LIKE A LINKED LIST
HEIGHT = N
OPERATIONS: O(N)
HEIGHT ≈ LOG₂N
OPERATIONS: O(LOG N)
Balanced trees
ensure
consistently fast
operations.
Red-Black Trees: Smart BSTs with Color
BST + one extra bit: color (RED or BLACK)
Enforces 5 color-based properties
Ensures the tree is approximately balanced
Height is at most 2·log(n+1) ⇒ O(log n)
Tree Type Balance Strategy Worst-Case Height Pros & Cons
AVL Tree Strict height check O(log n)
✅Faster search
❌more rotations
Red-Black Tree Color rules (loose) ≤ 2·log(n+1) ✅Fewer rotations, good insert/delete
B-Tree Multi-way node degrees O(log n) ✅Great for disk-based systems
Why Use Red-Black Trees?
Red-Black Trees
strike a practical
balance between
strictness and
performance.
Red-Black Trees: Smart BSTs with Color
🔺BSTs can become unbalanced ⇒ O(n) operations
🧠We want logarithmic height to stay fast
🌈Red-Black Trees enforce 5 properties using node color
⏱️Guaranteed: O(log n) for SEARCH, INSERT, and DELETE
🎯Widely used in real-world systems (e.g., Java TreeMap, C++ STL map)
Properties of
Red-Black Trees
What Is a Red-Black Tree?
A RED-BLACK TREE IS A BINARY SEARCH TREE WHERE EACH NODE HAS AN
EXTRA ATTRIBUTE: COLOR ∈ {RED, BLACK}.
Definition
The 5 Red-Black Properties
Every node is either red or black.
1.
The root is black.
2.
Every leaf (NIL) is black.
3.
If a node is red, then both its children are black.
4.
For each node, all paths to descendant leaves
contain the same number of black nodes.
5.
Property 5 ensures
black-depth consistency
across subtrees
Together, these rules
guarantee balance —
but loosely
How Do These Properties Help?
Limit
Height
Restricting red
children prevents
long chains of red
nodes
Depth
Consistency
Balance is All
You Need !
Red-black trees allow more flexibility than AVL, with fewer rotations.
Black-Height: A Key Concept
The black-height of a node x is the number of black nodes (not
including x) on any path from x down to a leaf.
Notation: bh(x)
Black-height of tree = bh(root)
Definition
Black-Height: A Key Concept
Lemma 13.1 (Height Bound)
Lemma 13.1:
A red-black tree with n internal nodes has height ≤ 2·log(n + 1).
Lemma 13.1 (Height Bound)
Lemma 13.1 (Proof):
Show: Subtree rooted at x contains ≥ 2^bh(x) - 1 internal
nodes (proved by induction).
1.
Use Property 4:
2.
At least half the nodes on any root-to-leaf path must be
black ⇒ bh(root) ≥ h/2
Then:
3.
n ≥ 2^(h/2) - 1
⇒ log(n + 1) ≥ h/2
⇒ h ≤ 2·log(n + 1)
Consequence: Efficient Set Operations
Because red-black trees have height O(log n), the following
operations are guaranteed to be:
SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR
➜ O(log n) worst-case time
Note:
We’ll later see that even after insertion/deletion, red-black
properties can be restored efficiently.
Rotations in
Red-Black Trees
Fixing Tree Structure with
Rotations
Red-black tree invariants may be violated during insertion or deletion.
Rotations help restore balance without violating the binary search tree
property.
Two types: LEFT-ROTATE and RIGHT-ROTATE
LEFT-ROTATE(x)
Time complexity: O(1)
Updates only a few pointers
LEFT-ROTATE(x)
✅BST structure preserved
✅Height and black-height may change locally
RIGHT-ROTATE(x)
Time complexity: O(1)
Updates only a few pointers
RIGHT-ROTATE(x)
✅Again, BST structure is preserved
✅These operations are local but key to restoring red-black properties
Summary: Rotations
Simple pointer changes (no key swaps)
Used in RB-INSERT-FIXUP and RB-DELETE-FIXUP
Maintain BST invariants
Run in O(1) time
Only affect a small part of the tree
Insertion in
Red-Black Trees
How Insertion Works
First, insert the new node as in a normal BST.
Color it red (to avoid violating black-height property immediately).
Then call RB-INSERT-FIXUP to fix any red-black violations.
Note:
We color new nodes red to avoid increasing the black-height along any path.
RB-INSERT(T, z)
Note:
This part is just BST logic + coloring the new node red.
What Can Go Wrong After Insertion?
If z.p.color == BLACK: No problem.
If z.p.color == RED: Red-red violation! (violates Property 4)
This triggers RB-INSERT-FIXUP, which resolves these
problems.
RB-INSERT-FIXUP: Fixing Violations
Key Idea:
We look at the uncle y of the inserted node z:
Three cases depending on z’s uncle:
Case 1: Uncle y is red
Case 2: Uncle y is black and z is a right child
Case 3: Uncle y is black and z is a left child
Plus symmetric versions if z.p is a right child.
RB-INSERT-FIXUP(T, z)
Note:
This part is just BST logic + coloring the new node red.
Case-by-Case Fixup Visualization
Efficiency of Insertion
Insertion: O(log n) to find the location
Fixup: O(1) rotations per level, so total O(log n)
✅All red-black properties restored
✅Tree remains balanced
Summary: Insertion
Insert node like BST, color red
Fix red-red issues with RB-INSERT-FIXUP
Use rotations and recoloring as needed
Height remains O(log n)
Deletion in
Red-Black Trees
Deletion: The Hardest Operation
Just like in BSTs, we use the successor if the node has two children.
But deleting a node might violate red-black properties.
Especially problematic if we delete a black node → might change black-height.
RB-DELETE(T, z)
What Violations Can Occur?
Deleting a black node can violate:
Property 1: Root is black (if black root deleted)
Property 5: All paths must have same # of black nodes
RB-DELETE-FIXUP(T, x)
Fixup: The 4 Key Cases
Each based on color of sibling w and its children:
Case 1: Sibling w is red → rotate & convert to another
case
Case 2: Sibling w and both its children are black → recolor
and move up
Case 3: w is black, w.left red, w.right black → rotate to
convert to Case 4
Case 4: w is black, w.right is red → rotate and recolor, fix
complete
Fixup: The 4 Key Cases
Efficiency of Deletion
Like BST: O(log n) to locate node
Fixup: O(log n) (at most 3 rotations)
✅All red-black properties restored
✅Tree remains balanced
Summary: Deletion
Start with BST-style deletion
If deleted node is black → fix with RB-DELETE-FIXUP
Four main cases depending on sibling and its children
Tree height remains O(log n)
Properties and
Correctness Proofs
Property # Description
1 Every node is red or black
2 The root is black
3 Every leaf (NIL) is black
4 Red nodes have black children (no two reds in a row)
5 Every path from a node to its descendant NILs contains the same number of black nodes
Red-Black Tree Properties Recap
Lemma: Height is O(log n)
A red-black tree with n internal nodes has height at most
Lemma: Height is O(log n)
Lemma(Proof):
Show: Subtree rooted at x contains ≥ 2^bh(x) - 1 internal
nodes (proved by induction).
1.
Use Property 4:
2.
At least half the nodes on any root-to-leaf path must be
black ⇒ bh(root) ≥ h/2
Then:
3.
n ≥ 2^(h/2) - 1
⇒ log(n + 1) ≥ h/2
⇒ h ≤ 2·log(n + 1)
Operation Property 1 Property 2 Property 3 Property 4 Property 5
Insert-Fixup ✅ ✅ ✅ ✅(via rotations) ✅
Delete-Fixup ✅ ✅ ✅ ✅ ✅
Why Insertion and Deletion Maintain
Red-Black Properties
Insert and delete call fixup routines that:
Recolor
Rotate
Propagate problems up if needed
Summary: Properties and Proofs
Lemma: Height = O(log n)
All operations preserve 5 key properties
Fixups use only local changes
Proves red-black trees are reliable balanced BSTs
Application &
Summary
Motivation for Red-Black Trees in Practice
🌲Self-balancing: Guaranteed O(log n) for:
Search
Insert
Delete
⚖️Less rigid than AVL trees → fewer rotations
✅Used when balance and performance are needed
📦Real-world uses:
C++ STL map, set
Java TreeMap, TreeSet
Linux kernel scheduler
Feature Red-Black AVL Tree Splay Tree
Balance Factor Loose Strict Self-adjusting
Rotations Needed Fewer More Depends
Worst-case time O(log n) O(log n) O(log n)
Use Case General use Fast search Cache locality
Red-Black vs AVL vs Others
Red-Black Trees: Key Takeaways
A type of binary search tree with added color and balance
properties
Maintains 5 crucial invariants using:
Rotations
Recoloring
Insertions & deletions require fixups, but always keep height
= O(log n)
Supported by rigorous lemmas and proofs
Widely used in practice because of their efficiency and
reliability
Questions?
Thank's For
Watching

Red-Black Tree Presentation By Mobin Nesari.pdf

  • 1.
  • 2.
    Overview Introduction & Motivation 1. Propertiesof Red-Black Trees 2. Tree Rotations 3. Insertion 4. Deletion 5. Examples 6. Common Questions & Pitfalls 7. Summary 8. References 9.
  • 3.
  • 4.
    Why Balanced Trees? LOOKSLIKE A LINKED LIST HEIGHT = N OPERATIONS: O(N) HEIGHT ≈ LOG₂N OPERATIONS: O(LOG N) Balanced trees ensure consistently fast operations.
  • 5.
    Red-Black Trees: SmartBSTs with Color BST + one extra bit: color (RED or BLACK) Enforces 5 color-based properties Ensures the tree is approximately balanced Height is at most 2·log(n+1) ⇒ O(log n)
  • 6.
    Tree Type BalanceStrategy Worst-Case Height Pros & Cons AVL Tree Strict height check O(log n) ✅Faster search ❌more rotations Red-Black Tree Color rules (loose) ≤ 2·log(n+1) ✅Fewer rotations, good insert/delete B-Tree Multi-way node degrees O(log n) ✅Great for disk-based systems Why Use Red-Black Trees? Red-Black Trees strike a practical balance between strictness and performance.
  • 7.
    Red-Black Trees: SmartBSTs with Color 🔺BSTs can become unbalanced ⇒ O(n) operations 🧠We want logarithmic height to stay fast 🌈Red-Black Trees enforce 5 properties using node color ⏱️Guaranteed: O(log n) for SEARCH, INSERT, and DELETE 🎯Widely used in real-world systems (e.g., Java TreeMap, C++ STL map)
  • 8.
  • 9.
    What Is aRed-Black Tree? A RED-BLACK TREE IS A BINARY SEARCH TREE WHERE EACH NODE HAS AN EXTRA ATTRIBUTE: COLOR ∈ {RED, BLACK}. Definition
  • 10.
    The 5 Red-BlackProperties Every node is either red or black. 1. The root is black. 2. Every leaf (NIL) is black. 3. If a node is red, then both its children are black. 4. For each node, all paths to descendant leaves contain the same number of black nodes. 5.
  • 11.
    Property 5 ensures black-depthconsistency across subtrees Together, these rules guarantee balance — but loosely How Do These Properties Help? Limit Height Restricting red children prevents long chains of red nodes Depth Consistency Balance is All You Need ! Red-black trees allow more flexibility than AVL, with fewer rotations.
  • 12.
    Black-Height: A KeyConcept The black-height of a node x is the number of black nodes (not including x) on any path from x down to a leaf. Notation: bh(x) Black-height of tree = bh(root) Definition
  • 13.
  • 14.
    Lemma 13.1 (HeightBound) Lemma 13.1: A red-black tree with n internal nodes has height ≤ 2·log(n + 1).
  • 15.
    Lemma 13.1 (HeightBound) Lemma 13.1 (Proof): Show: Subtree rooted at x contains ≥ 2^bh(x) - 1 internal nodes (proved by induction). 1. Use Property 4: 2. At least half the nodes on any root-to-leaf path must be black ⇒ bh(root) ≥ h/2 Then: 3. n ≥ 2^(h/2) - 1 ⇒ log(n + 1) ≥ h/2 ⇒ h ≤ 2·log(n + 1)
  • 16.
    Consequence: Efficient SetOperations Because red-black trees have height O(log n), the following operations are guaranteed to be: SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR ➜ O(log n) worst-case time Note: We’ll later see that even after insertion/deletion, red-black properties can be restored efficiently.
  • 17.
  • 18.
    Fixing Tree Structurewith Rotations Red-black tree invariants may be violated during insertion or deletion. Rotations help restore balance without violating the binary search tree property. Two types: LEFT-ROTATE and RIGHT-ROTATE
  • 19.
  • 20.
    LEFT-ROTATE(x) ✅BST structure preserved ✅Heightand black-height may change locally
  • 21.
  • 22.
    RIGHT-ROTATE(x) ✅Again, BST structureis preserved ✅These operations are local but key to restoring red-black properties
  • 23.
    Summary: Rotations Simple pointerchanges (no key swaps) Used in RB-INSERT-FIXUP and RB-DELETE-FIXUP Maintain BST invariants Run in O(1) time Only affect a small part of the tree
  • 24.
  • 25.
    How Insertion Works First,insert the new node as in a normal BST. Color it red (to avoid violating black-height property immediately). Then call RB-INSERT-FIXUP to fix any red-black violations. Note: We color new nodes red to avoid increasing the black-height along any path.
  • 26.
    RB-INSERT(T, z) Note: This partis just BST logic + coloring the new node red.
  • 27.
    What Can GoWrong After Insertion? If z.p.color == BLACK: No problem. If z.p.color == RED: Red-red violation! (violates Property 4) This triggers RB-INSERT-FIXUP, which resolves these problems.
  • 28.
    RB-INSERT-FIXUP: Fixing Violations KeyIdea: We look at the uncle y of the inserted node z: Three cases depending on z’s uncle: Case 1: Uncle y is red Case 2: Uncle y is black and z is a right child Case 3: Uncle y is black and z is a left child Plus symmetric versions if z.p is a right child.
  • 29.
    RB-INSERT-FIXUP(T, z) Note: This partis just BST logic + coloring the new node red.
  • 30.
  • 31.
    Efficiency of Insertion Insertion:O(log n) to find the location Fixup: O(1) rotations per level, so total O(log n) ✅All red-black properties restored ✅Tree remains balanced
  • 32.
    Summary: Insertion Insert nodelike BST, color red Fix red-red issues with RB-INSERT-FIXUP Use rotations and recoloring as needed Height remains O(log n)
  • 33.
  • 34.
    Deletion: The HardestOperation Just like in BSTs, we use the successor if the node has two children. But deleting a node might violate red-black properties. Especially problematic if we delete a black node → might change black-height.
  • 35.
  • 36.
    What Violations CanOccur? Deleting a black node can violate: Property 1: Root is black (if black root deleted) Property 5: All paths must have same # of black nodes
  • 37.
  • 38.
    Fixup: The 4Key Cases Each based on color of sibling w and its children: Case 1: Sibling w is red → rotate & convert to another case Case 2: Sibling w and both its children are black → recolor and move up Case 3: w is black, w.left red, w.right black → rotate to convert to Case 4 Case 4: w is black, w.right is red → rotate and recolor, fix complete
  • 39.
    Fixup: The 4Key Cases
  • 40.
    Efficiency of Deletion LikeBST: O(log n) to locate node Fixup: O(log n) (at most 3 rotations) ✅All red-black properties restored ✅Tree remains balanced
  • 41.
    Summary: Deletion Start withBST-style deletion If deleted node is black → fix with RB-DELETE-FIXUP Four main cases depending on sibling and its children Tree height remains O(log n)
  • 42.
  • 43.
    Property # Description 1Every node is red or black 2 The root is black 3 Every leaf (NIL) is black 4 Red nodes have black children (no two reds in a row) 5 Every path from a node to its descendant NILs contains the same number of black nodes Red-Black Tree Properties Recap
  • 44.
    Lemma: Height isO(log n) A red-black tree with n internal nodes has height at most
  • 45.
    Lemma: Height isO(log n) Lemma(Proof): Show: Subtree rooted at x contains ≥ 2^bh(x) - 1 internal nodes (proved by induction). 1. Use Property 4: 2. At least half the nodes on any root-to-leaf path must be black ⇒ bh(root) ≥ h/2 Then: 3. n ≥ 2^(h/2) - 1 ⇒ log(n + 1) ≥ h/2 ⇒ h ≤ 2·log(n + 1)
  • 46.
    Operation Property 1Property 2 Property 3 Property 4 Property 5 Insert-Fixup ✅ ✅ ✅ ✅(via rotations) ✅ Delete-Fixup ✅ ✅ ✅ ✅ ✅ Why Insertion and Deletion Maintain Red-Black Properties Insert and delete call fixup routines that: Recolor Rotate Propagate problems up if needed
  • 47.
    Summary: Properties andProofs Lemma: Height = O(log n) All operations preserve 5 key properties Fixups use only local changes Proves red-black trees are reliable balanced BSTs
  • 48.
  • 49.
    Motivation for Red-BlackTrees in Practice 🌲Self-balancing: Guaranteed O(log n) for: Search Insert Delete ⚖️Less rigid than AVL trees → fewer rotations ✅Used when balance and performance are needed 📦Real-world uses: C++ STL map, set Java TreeMap, TreeSet Linux kernel scheduler
  • 50.
    Feature Red-Black AVLTree Splay Tree Balance Factor Loose Strict Self-adjusting Rotations Needed Fewer More Depends Worst-case time O(log n) O(log n) O(log n) Use Case General use Fast search Cache locality Red-Black vs AVL vs Others
  • 51.
    Red-Black Trees: KeyTakeaways A type of binary search tree with added color and balance properties Maintains 5 crucial invariants using: Rotations Recoloring Insertions & deletions require fixups, but always keep height = O(log n) Supported by rigorous lemmas and proofs Widely used in practice because of their efficiency and reliability
  • 52.
  • 53.