Data Structures · Visualization
AVL Tree: Delete
AVL Tree
Deleting from an AVL tree starts with a normal BST delete (handling the leaf, one-child, and two-children cases), then walks back up from the affected node to the root, checking balance factors just like insertion. Time: O(log n). Space: O(log n) recursive, O(1) extra per rotation.
Big-O: Every operation stays O(log n) thanks to rotations.
AVL Tree: Delete
AVL TreeDeleting from an AVL tree starts with a normal BST delete (handling the leaf, one-child, and two-children cases), then walks back up from the affected node to the root, checking balance factors just like insertion. Unlike insertion, a delete can cause imbalance at multiple levels up the tree, so rotations may need to happen more than once on the way up.
How Delete Works
- Delete the node following normal BST delete rules.
- Walk back up from the point of deletion toward the root, updating heights.
- At each ancestor, compute the balance factor.
- Whenever a node's balance factor becomes +2 or -2, apply the matching rotation, then continue checking further up the tree.
Delete Worked Example
Input: Delete a leaf that leaves its parent unbalanced
- Remove the leaf node as normal
- Walk upward, recalculating heights
- Find a node with balance factor -2
- Apply the correct rotation (e.g. left rotation) to restore balance
- Continue checking ancestors further up in case more rotations are needed
Output: Tree remains balanced after deletion and rotation(s)
Press ▶ Run to watch it step by step.
def min_node(node):
while node.left: node = node.left
return node
def delete(node, key):
if not node: return None
if key < node.key: node.left = delete(node.left, key)
elif key > node.key: node.right = delete(node.right, key)
else: # node to delete
if not node.left: return node.right
if not node.right: return node.left
succ = min_node(node.right) # in-order successor
node.key = succ.key
node.right = delete(node.right, succ.key)
node.height = 1 + max(height(node.left), height(node.right))
b = balance(node)
if b > 1 and balance(node.left) >= 0: return rotate_right(node) # LL
if b > 1 and balance(node.left) < 0: # LR
node.left = rotate_left(node.left); return rotate_right(node)
if b < -1 and balance(node.right) <= 0: return rotate_left(node) # RR
if b < -1 and balance(node.right) > 0: # RL
node.right = rotate_right(node.right); return rotate_left(node)
return node
# example: deleteBehind 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.
Delete Time & Space Complexity
When to Use Delete
- Removing entries from a database index without degrading future performance
- Any long-running system doing repeated insert/delete cycles where balance must be preserved
- Priority-based systems needing consistently fast removal
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.