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 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. 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

  1. Delete the node following normal BST delete rules.
  2. Walk back up from the point of deletion toward the root, updating heights.
  3. At each ancestor, compute the balance factor.
  4. 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

  1. Remove the leaf node as normal
  2. Walk upward, recalculating heights
  3. Find a node with balance factor -2
  4. Apply the correct rotation (e.g. left rotation) to restore balance
  5. Continue checking ancestors further up in case more rotations are needed

Output: Tree remains balanced after deletion and rotation(s)

Try your own input
AVL: Introduction
A self-balancing BST — after every insert or delete, it checks each node’s left/right height difference and rotates automatically if it exceeds 1.
500
300
200
400
700
600
800
Active Result Unbalanced
AVL: Introduction
Press ▶ Run to watch it step by step.
Python · AVL Trees in Code
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: delete
Input: Delete a leaf that leaves its parent unbalanced

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.

Delete Time & Space Complexity

TimeO(log n)
SpaceO(log n) recursive, O(1) extra per rotation

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

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.