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

  1. Insert the new value following normal BST rules (smaller left, larger right).
  2. Walk back up from the inserted node toward the root, updating each ancestor's height.
  3. At each ancestor, compute the balance factor.
  4. 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)

  1. 25 > 20, go right; 25 < 30, becomes left child of 30
  2. Check balance at 30: left height 1, right height 0 → factor +1, fine
  3. Check balance at 20: left height 0, right height 2 → factor -2 → unbalanced!
  4. This is a right-left case: rotate right around 30, then left around 20

Output: 25 / \ 20 30

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 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 25
Input: Insert 25 into tree root(20) -> right(30)

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 for the rotation itself

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

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.