Black root
The root is always black. The simulator enforces this after every insertion and deletion.
A Red-Black Tree is a self-balancing Binary Search Tree. Node colors and local rotations keep every root-to-leaf path within a constant factor of the shortest path, guaranteeing logarithmic search, insertion, and deletion.
Example Red-Black Tree loaded. Choose an operation.
if right link is red:
rotate left
if node and left child are red:
rotate right
if both children are red:
flip colors
during deletion:
move a red link toward the target
delete using BST order
restore the invariants on returnThe root is always black. The simulator enforces this after every insertion and deletion.
A red node cannot have a red child. Rotations and color flips remove consecutive red links.
Every path from a node to a missing child contains the same number of black nodes.
This implementation represents 3-nodes using left-leaning red links, a compact form equivalent to a 2–3 tree.
| Operation | Purpose |
|---|---|
| Rotate left | Convert a right-leaning red link into a left-leaning link. |
| Rotate right | Repair two consecutive left red links. |
| Flip colors | Split or combine a temporary 4-node. |
| Move red left/right | Prepare a minimally colored subtree for safe deletion. |