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.
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:
- Node Color: Each node is either red or black.
- Root Property: The root of the tree is always black.
- Leaf Property: Every leaf (NIL node) is black. Note: NIL nodes are considered as leaves.
- Red Property: If a node is red, then both its children must be black.
- 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:
- Initial Insertion: Insert the node using standard BST insertion, color the new node red.
- 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:
- Node Removal: Remove the node in question as in a standard BST.
- 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:
- Insert node `7` into an empty tree; color it black (root property).
- Insert node `3`, color it red (no violation).
- Insert node `18`, color it red (no violation).
- 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:
| Operation | Best Case | Average Case | Worst Case |
| Insertion | |||
| Deletion | |||
| Search |
Here, 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
- Red eye reduction algorithm
- Reducing the time complexity of this algorithm
- Redundancy algorithm for reading noisy bitstream
- Refactor recursive algorithm into an iterative one?
- Reduce array to set in Swift
- Referencing a string in a string array resource with xml
- regexp-like library for matrix pattern search
- Register SPI dynamically at runtime

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 courseTrack 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.