Data Structures19 sections · 729 units
Open in Course

Intro

Why BSTs matter

Arrays give you O(1)O(1) random access but O(n)O(n) search (unless sorted, then O(logn)O(\log n) with binary search, but insertion becomes O(n)O(n)).

Linked lists give you O(1)O(1) insertion but O(n)O(n) search. Binary Search Trees solve this tradeoff.

The BST property ensures that at each node, you know which half contains your target. Search, insert, and delete all take O(h)O(h) time, where hh is the tree height.

The catch: height depends on insertion order. Insert [1,2,3,4,5][1, 2, 3, 4, 5] and you get a stick with h=n1h = n - 1. Insert in balanced order and h=O(logn)h = O(\log n).

In this section, I'll teach you BST operations and the inorder property. You'll see why self-balancing trees (AVL, Red-Black) exist. And when simpler solutions work fine.