algo·visual

Binary Search Tree

Data Structures
avgO(log n)worstO(n)spaceO(n)
50307020406080

A BST with 7 keys, rooted at 50

1 / 38
Starting contents7 values
Operations7 / 40
  1. 1. insert(35)
  2. 2. insert(65)
  3. 3. search(60)
  4. 4. delete(20)
  5. 5. delete(70)
  6. 6. delete(30)
  7. 7. search(45)

How it works

Every key in a node's left subtree is smaller and every key in its right subtree is larger, so search, insert and delete each follow one root-to-leaf path. Deleting a node with two children swaps in its in-order successor. It is fast when balanced, but inserting keys in sorted order turns it into a linked list.

Complexity

Best
O(log n)
Average
O(log n)
Worst
O(n)
Space
O(n)

Search algorithms

Jump to any algorithm or page