Skip to content
StackDecode
TreesAnimated

BST Insertion

Walk down as in search and attach a new leaf.

Time

O(h)

Extra space

O(1)

Use it for

Building ordered trees.

How it works

  1. Walk down exactly as in a search: smaller goes left, larger goes right.
  2. When you reach an empty spot, that is where the new key belongs.
  3. Attach it as a new leaf. No other node moves.
  4. Duplicates are ignored. O(height).

Watch it run

Loading animation…