Back

AVL Tree Operations

AVL Tree Selection

Choose from predefined examples or create your own AVL tree

Operation

Value between 1 and 999

Playback Controls

Empty AVL Tree - Insert a value to begin
Empty tree
No nodes to display
Current Operation

Current AVL Tree State

Values: []
Node Count: 0
Tree Type: Always Balanced (AVL)

Reference implementation of AVL Tree Operations. Select a language tab to view and copy the code.

def avl_insert(root, val):
    if root is None:
        return AVLNode(val)             # create leaf
    if val < root.val:
        root.left  = avl_insert(root.left,  val)  # go left
    elif val > root.val:
        root.right = avl_insert(root.right, val)  # go right

    # Update height of current node
    root.height = 1 + max(height(root.left), height(root.right))

    # Balance factor: left_height - right_height
    balance = height(root.left) - height(root.right)

    # LL case — right rotation
    if balance > 1 and val < root.left.val:
        return rotate_right(root)
    # RR case — left rotation
    if balance < -1 and val > root.right.val:
        return rotate_left(root)
    # LR case — left then right rotation
    if balance > 1 and val > root.left.val:
        root.left = rotate_left(root.left)
        return rotate_right(root)
    # RL case — right then left rotation
    if balance < -1 and val < root.right.val:
        root.right = rotate_right(root.right)
        return rotate_left(root)
    return root

def avl_search(root, val):
    if root is None:   return None      # not found
    if val == root.val: return root     # found
    if val < root.val:
        return avl_search(root.left,  val)
    return avl_search(root.right, val)

Python — Code Walkthrough

  1. Standard BST insert first

    if val < root.val: root.left = avl_insert(root.left, val)

    AVL insert starts exactly like BST insert: recurse left if smaller, right if larger. Balancing only happens on the way back up the recursion.

  2. Height update after recursion

    root.height = 1 + max(height(root.left), height(root.right))

    Height is recomputed bottom-up as the recursion unwinds. height() returns -1 for None so a single node gets height 0.

  3. Balance factor

    balance = height(root.left) - height(root.right)

    Balance factor = left height - right height. Valid AVL range is [-1, 0, 1]. A factor outside this range triggers a rotation.

  4. Four rotation cases

    LL: rotate_right | RR: rotate_left | LR: rotate_left then rotate_right | RL: rotate_right then rotate_left

    LL and RR are single rotations. LR and RL are double rotations — first fix the child, then fix the root — because the imbalance is "zigzag".