Data Structures · Visualization

Red-Black Tree: Insert

Red-Black Tree

Inserting into a red-black tree starts with a normal BST insert, coloring the new node red. Time: O(log n). Space: O(log n) recursive, O(1) extra work per fix-up step.

Big-O: Operations O(log n); height ≤ 2·log₂(n+1).

Red-Black Tree: Insert

Red-Black Tree

Inserting into a red-black tree starts with a normal BST insert, coloring the new node red. This might break a color rule (e.g. creating a red node with a red parent), so the tree then walks back up, fixing violations using a mix of 'recoloring' (flipping colors of nearby nodes) and rotations, depending on the color of the new node's 'uncle' (its parent's sibling).

How Insert Works

  1. Insert the new node as in a plain BST, and color it red.
  2. If the new node's parent is black, no rule is broken — done.
  3. If the parent is red, check the color of the 'uncle' node (the parent's sibling).
  4. If the uncle is red, recolor the parent, uncle, and grandparent, then continue checking further up.
  5. If the uncle is black (or missing), perform the appropriate rotation(s) and recoloring to fix the violation.

Insert Worked Example

Input: Insert a red node under a red parent whose uncle is also red

  1. New node is red, parent is red → violation
  2. Uncle is red → recolor parent and uncle to black, grandparent to red
  3. Continue checking from the grandparent upward, since it's now red and might conflict with its own parent

Output: Tree rules restored, possibly after further fixes up the tree

Try your own input
Red-Black: Introduction
A self-balancing BST that colors each node red or black and follows a few coloring rules — this keeps the tree "balanced enough" without needing exact height checks like AVL.
50
30
20
40
70
60
80
Red Black Active Result
Red-Black: Introduction
Press ▶ Run to watch it step by step.
Python · Red-Black Trees in Code
def left_rotate(T, x):
    y = x.right
    x.right = y.left
    if y.left: y.left.parent = x
    y.parent = x.parent
    if not x.parent: T.root = y
    elif x is x.parent.left: x.parent.left = y
    else: x.parent.right = y
    y.left = x
    x.parent = y

def insert_fixup(T, z):
    while z.parent and z.parent.color == RED:   # violation of invariant 4
        p = z.parent
        g = p.parent
        if p is g.left:                          # parent is a left child
            u = g.right                          # uncle
            if u and u.color == RED:             # case 1: recolor
                p.color = u.color = BLACK
                g.color = RED
                z = g
            else:
                if z is p.right:                 # case 2: LR -> left rotate
                    z = p
                    left_rotate(T, z)
                p.color = BLACK                  # case 3: RR -> right rotate
                g.color = RED
                right_rotate(T, g)
        else:                                    # mirror image for right parent
            u = g.left
            if u and u.color == RED:
                p.color = u.color = BLACK
                g.color = RED
                z = g
            else:
                if z is p.left:
                    z = p
                    right_rotate(T, z)
                p.color = BLACK
                g.color = RED
                left_rotate(T, g)
    T.root.color = BLACK
# example: insert
Input: Insert a red node under a red parent whose uncle is also red

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Insert Time & Space Complexity

TimeO(log n)
SpaceO(log n) recursive, O(1) extra work per fix-up step

When to Use Insert

  • Powering ordered map/set insert operations in standard libraries
  • Systems needing predictable insert performance under heavy, unordered input

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.