HomeArticlesBinary Search Tree

Binary Search Trees: Insert, Search and AVL Balancing

Why the same search-tree invariant that gives O(log n) lookup can silently degrade to O(n), and how AVL rotations keep the tree balanced through every insert and delete.

mysimulator teamUpdated June 2026≈ 8 min read▶ Open the simulation

One invariant, three operations

A binary search tree (BST) stores keys under a single invariant: for every node, everything in its left subtree is smaller and everything in its right subtree is larger. That invariant alone is enough to make search, insert and delete all follow the same pattern — compare the target key to the current node and recurse left or right — turning what would otherwise be a linear scan into a path down the tree.

search(node, key):
  if node is null: return not found
  if key == node.key: return node
  if key < node.key: return search(node.left, key)
  else:                return search(node.right, key)
insert: search until you fall off the tree, attach a new leaf there
delete: 0 or 1 child -> splice node out; 2 children -> replace with in-order successor
live demo · inserting keys into a search tree● LIVE

Complexity depends entirely on the tree's shape

All three operations cost O(h), where h is the tree's height. For a perfectly balanced tree of n keys, h = O(log n), so search, insert and delete are all logarithmic. But a BST built by inserting keys already in sorted order degenerates into a straight chain, with h = n − 1: the tree becomes, structurally, a linked list, and every operation is O(n). The BST invariant guarantees correctness; it does not guarantee balance.

In-order traversal recovers the sorted order for free

Visiting a BST in-order (left subtree, node, right subtree, recursively) always yields the keys in ascending sorted order, a direct consequence of the search invariant. This is one reason to use a BST instead of a hash table when you need range queries or sorted iteration: a hash table gives O(1) average lookup but no ordering, while a balanced BST gives O(log n) lookup and sorted output as a byproduct.

AVL trees: balancing by rotation

An AVL tree (Adelson-Velsky and Landis, 1962) restores the logarithmic guarantee by enforcing a balance invariant after every insert and delete: for every node, the heights of its left and right subtrees differ by at most 1. When an operation violates that invariant, the tree performs a rotation — a local restructuring of a few pointers that restores balance without breaking the search-tree property.

right rotation around node y (fixes left-left imbalance):
      y                x
     / \                / \
    x   C     ->      A   y
   / \                    / \
  A   B                  B   C
(a left rotation is the mirror image)

Because a rotation only rearranges a constant number of pointers and at most one imbalance needs fixing per level on the path back to the root, each insert or delete costs O(log n) for the search plus O(log n) worst case for rebalancing. AVL trees keep height within about 1.44 log2(n), a tighter bound than a red-black tree, which trades slightly looser balance for fewer rotations per update.

Why balance is worth the bookkeeping

The whole appeal of a BST over a plain sorted array is O(log n) insert and delete instead of O(n) shifting. That appeal evaporates the moment the tree is allowed to degenerate. Self-balancing variants — AVL, red-black, splay trees — all exist to make the O(log n) guarantee hold for every sequence of operations, not just favourable ones, at the cost of a balance factor per node and occasional rotation work.

Frequently asked questions

Why can a binary search tree become as slow as a linked list?

The BST invariant only constrains relative ordering, not shape. Inserting keys in already-sorted order makes every new node attach as the rightmost child of the previous one, producing a straight chain of height n-1 instead of a balanced tree of height log n — every search then costs O(n).

What does an AVL rotation actually do?

It is a local, constant-time rearrangement of a few parent-child pointers around the point of imbalance that restores the height-balance invariant while preserving the BST ordering invariant. A chain of these rotations, at most O(log n) of them, fixes any imbalance introduced by one insert or delete.

Is in-order traversal always sorted, even in an unbalanced BST?

Yes — sortedness comes purely from the left-smaller, right-larger invariant, which holds regardless of the tree's height or balance. An unbalanced BST gives sorted output just as reliably as a balanced one; only the traversal time and search/insert/delete complexity depend on balance.

Try it live

Everything above runs in your browser — open Binary Search Tree and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.

▶ Open Binary Search Tree simulation

What did you find?

Add reproduction steps (optional)