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 TreeInserting 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
- Insert the new node as in a plain BST, and color it red.
- If the new node's parent is black, no rule is broken — done.
- If the parent is red, check the color of the 'uncle' node (the parent's sibling).
- If the uncle is red, recolor the parent, uncle, and grandparent, then continue checking further up.
- 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
- New node is red, parent is red → violation
- Uncle is red → recolor parent and uncle to black, grandparent to red
- 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
Press ▶ Run to watch it step by step.
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: insertBehind 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
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
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.