Math Fundamentals18 sections · 814 units
Open in Course

Tree Height Example (Another log n pattern)

Balanced binary trees

A balanced binary tree with nn nodes has height O(logn)O(\log n). Why? Each level doubles the number of nodes.

Level 00 has 11 node (the root). Level 11 has 22 nodes. Level 22 has 44 nodes. Level kk has 2k2^k nodes. If the tree has nn total nodes, then 2h+11n2^{h+1} - 1 \approx n, so hlog2(n)h \approx \log_2(n).

This is why operations on balanced trees (like AVL trees or red-black trees) take O(logn)O(\log n) time. You traverse from root to leaf, which is O(h)=O(logn)O(h) = O(\log n) steps.