What is a Red-Black Tree?
A Red-Black Tree is a self-balancing Binary Search Tree (BST) where each node has an extra bit representing "color" (Red or Black). These colors are used to ensure the tree remains approximately balanced during insertions and deletions.
Unlike AVL trees which are strictly balanced, Red-Black trees provide a looser balance criterion but are often faster for heavy insertion/deletion workloads because they require fewer rotations to rebalance.
The 5 Properties
- 1. Every node is either Red or Black.
- 2. The Root is always Black.
- 3. Every Leaf (NIL) is Black.
- 4. If a node is Red, both its children must be Black (No Double Reds).
- 5. Every path from a node to any of its descendant NIL nodes has the same number of Black nodes.
Real-World Use Cases
- ✓Standard LibrariesJava's
TreeMapandTreeSet, and C++ STLstd::mapandstd::setare typically implemented using Red-Black Trees. - ✓Linux KernelUsed in the "Completely Fair Scheduler" (CFS) to manage process scheduling efficiently.