Red-Black Tree

CS400 Data Structures and Algorithms

What is a Red-Black Tree?

A Red-Black Tree is a self-balancing binary search tree that maintains balance by adding a color attribute (red or black) to each node. Red-Black Trees ensure that basic operations (insertion, deletion, search) have O(log n) time complexity in the worst case.

Red-Black Tree Properties

A Red-Black Tree must satisfy the following five important properties:

  1. Color Property: Every node is either red or black
  2. Root Property: The root node must be black
  3. Leaf Property: All leaf nodes (NIL nodes) are black
  4. Red Property: Both children of every red node are black (no two consecutive red nodes)
  5. Path Property: Every path from any node to all of its descendant leaf nodes contains the same number of black nodes

Red-Black Tree Structure

Red-Black Tree Structure Diagram

Red-Black Tree Structure Diagram - Shows node color distribution and balancing properties

Main Operations

Red-Black Trees support the following basic operations, all with O(log n) time complexity:

Advantages and Applications

Main advantages of Red-Black Trees over regular binary search trees:

  • Guarantees O(log n) time complexity in the worst case
  • Insertion and deletion operations are relatively simple with fewer rotations
  • Widely used in various data structure and algorithm libraries

Red-Black Trees are extremely important in practical applications. They are used in many programming language standard libraries, such as Java's TreeMap and TreeSet, C++'s map and set, etc.