← anamaya.fyi 2021 · college

Every comparison halves the problem.

A binary search tree keeps everything smaller on the left and everything larger on the right, so finding a value means walking down and never back. Insert some numbers, then search for one and watch which nodes it actually had to look at.

Value
on the search path found missing
0
nodes
0
height
0
ideal height
comparisons, last search

In-order traversal

Left, self, right — which comes out sorted, every time, for free.

The failure mode

Press Insert 1…15 in order. Every value is larger than the last, so every one goes right, and the tree degenerates into a linked list — height 15 instead of 4, and a search that compares every node. A BST is only O(log n) while it stays balanced, which is the entire reason AVL and red-black trees exist. This was the thing I did not understand in 2021 and the reason the visualisation was worth building.