Data Structures · Visualization
AVL Tree: Insert
AVL Tree
Inserting into an AVL tree first does a normal BST insert, then walks back up the path from the new node to the root, checking the balance factor at each ancestor. Time: O(log n). Space: O(log n) recursive, O(1) extra for the rotation itself.
Big-O: Every operation stays O(log n) thanks to rotations.
AVL Tree: Insert
AVL TreeInserting into an AVL tree first does a normal BST insert, then walks back up the path from the new node to the root, checking the balance factor at each ancestor. As soon as an unbalanced node (balance factor +2 or -2) is found, one of four rotations is applied to restore balance before continuing upward.
How Insert Works
- Insert the new value following normal BST rules (smaller left, larger right).
- Walk back up from the inserted node toward the root, updating each ancestor's height.
- At each ancestor, compute the balance factor.
- If a node's balance factor becomes +2 or -2, identify the case (left-left, right-right, left-right, or right-left) and apply the matching rotation.
Insert Worked Example
Input: Insert 25 into tree root(20) -> right(30)
- 25 > 20, go right; 25 < 30, becomes left child of 30
- Check balance at 30: left height 1, right height 0 → factor +1, fine
- Check balance at 20: left height 0, right height 2 → factor -2 → unbalanced!
- This is a right-left case: rotate right around 30, then left around 20
Output: 25 / \ 20 30
Press ▶ Run to watch it step by step.
def rotate_left(y):
x = y.right
y.right = x.left
x.left = y
y.height = 1 + max(height(y.left), height(y.right))
x.height = 1 + max(height(x.left), height(x.right))
return x
def rotate_right(x):
y = x.left
x.left = y.right
y.right = x
x.height = 1 + max(height(x.left), height(x.right))
y.height = 1 + max(height(y.left), height(y.right))
return y
def insert(node, key):
if not node: return AVLNode(key)
if key < node.key: node.left = insert(node.left, key)
elif key > node.key: node.right = insert(node.right, key)
else: return node # duplicates not allowed
node.height = 1 + max(height(node.left), height(node.right))
b = balance(node) # rebalance the 4 cases
if b > 1 and key < node.left.key: return rotate_right(node) # LL
if b < -1 and key > node.right.key: return rotate_left(node) # RR
if b > 1 and key > node.left.key: # LR
node.left = rotate_left(node.left); return rotate_right(node)
if b < -1 and key < node.right.key: # RL
node.right = rotate_right(node.right); return rotate_left(node)
return node
# example: insert 25Behind 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
- Maintaining a self-balancing sorted structure as new data streams in
- Database indexes that need consistent insert performance
- Any application inserting data in an order that could otherwise unbalance a BST (e.g. already-sorted 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.