Red-Black Trees

Summary

Red black trees are a subset of binary search trees that implement a coloring system for each node in the tree. Each node is colored red or black, and the tree must adhere to the following 4 rules:

  1. The root node must always be black.
  2. Each node must be colored either red or black.
  3. No parent-child pairs of nodes can both be red.
  4. The tree's black height (the number of black nodes from a node to the root node) must be the same for each leaf node.

Insertion and Deletion of Nodes

Insertion and deletion operations have additional steps in a red-black tree to handle any red-black tree rule violations.

Nodes are inserted into a tree as red nodes by default, but changes to the tree may be necessary to ensure there are no red parent-child pairs.

Deletions can also cause a few different violations:

  1. If a black node is removed from the tree, the black height equality rule may be violated.
  2. If the root is deleted, it could cause the root to be red.
  3. Red parent-child pairs are also possible if a black node is removed.

Here are some diagrams of insertion and deletion resolution processes:

Insertion Resolution

Deletion Resolution

For further reading, see this red-black tree guide