Data Structures
Red-Black Trees
Balanced Trees
Binary Search Trees
Algorithms

Red-Black Trees

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Red-Black Trees are a type of self-balancing binary search tree. While binary search trees are efficient structures for storing ordered data and performing quick searches, insertions, and deletions, they can become unbalanced, degrading the performance to that of a linked list. Red-Black Trees maintain balance automatically, ensuring that operations like insertions and deletions remain efficient.

Properties of Red-Black Trees

A Red-Black Tree is governed by a set of properties that ensure the tree remains approximately balanced:

  1. Node Color: Each node is either red or black.
  2. Root Property: The root of the tree is always black.
  3. Leaf Property: Every leaf (NIL node) is black. Note: NIL nodes are considered as leaves.
  4. Red Property: If a node is red, then both its children must be black.
  5. Black Depth: For each node, any simple path from the node to its descendant leaves must have the same number of black nodes.

These properties collectively ensure that the longest path from the root to a leaf is no more than twice as long as the shortest path, maintaining a balanced tree and ensuring efficient operations.

Operations on Red-Black Trees

Insertion

Inserting a node in a Red-Black Tree is similar to insertion in a standard BST, followed by a series of fix-ups to maintain the Red-Black properties:

  1. Initial Insertion: Insert the node using standard BST insertion, color the new node red.
  2. Fix-Up: Resolve violations of Red-Black properties:
    • Case 1: If the parent node is black, do nothing.
    • Case 2: If the parent node and the uncle node are both red (leading to a violation of the red property), recolor the parent and uncle black, grandparent red, and move the fix-up procedure to the grandparent.
    • Case 3 & 4: If the uncle is black, perform rotations to balance the tree.

Deletion

Deletion in a Red-Black Tree involves standard BST deletion, followed by a series of fix-ups:

  1. Node Removal: Remove the node in question as in a standard BST.
  2. Fix-Up: If removing the node violates the Red-Black properties, especially the black depth property, proceed with:
    • Case 1 & 2: Recoloring nodes if necessary.
    • Case 3 & 4: Perform rotations and recoloring to achieve balance.

Rotations

Rotations are fundamental operations used in maintaining tree balance during insertions and deletions:

  • Left Rotation:
    • Pivot the current node and bring its right child up.
  • Right Rotation:
    • Pivot the current node and bring its left child up.

These rotations help maintain the structure and balance of the tree without violating the binary search tree properties.

Example

Consider the following insertion example:

  1. Insert node `7` into an empty tree; color it black (root property).
  2. Insert node `3`, color it red (no violation).
  3. Insert node `18`, color it red (no violation).
  4. Insert node `10`, color it red (requires a fix-up since parent `18` is red).

This example will help illustrate how nodes are inserted and rotated to retain Red-Black properties.

Complexity Analysis

The balancing operations ensure that Red-Black Trees have the following time complexities for common operations:

OperationBest CaseAverage CaseWorst Case
InsertionO(1)O(1)O(logn)O(\log n)O(logn)O(\log n)
DeletionO(1)O(1)O(logn)O(\log n)O(logn)O(\log n)
SearchO(1)O(1)O(logn)O(\log n)O(logn)O(\log n)

Here, nn is the number of nodes in the tree. The logarithmic time complexities are assured due to the tree's balanced nature.

Applications

Red-Black Trees are widely used in many computing applications:

  • Memory Management: Used in implementing associative arrays, e.g., in the C++ Standard Library's `map`.
  • Run-Loop Schedulers: Help in efficient scheduling due to balanced property.
  • Multicast Routing: Used to enhance the routing efficiency in network systems.

Conclusion

Red-Black Trees are a crucial data structure that efficiently manages sorted sets, ensuring operations remain effective through automatic balancing. Their application across various fields, from memory management to network routing, highlights their versatility and importance in computer science.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.