Sorted input makes a chain
transcript
A search tree is fast. About twenty looks for a million values. Until you insert sorted data. Real data often arrives in order. Ids and timestamps usually do. Then it is as slow as a list. See why.
Insert ten. It becomes the root. Twenty is larger, so it goes right. Thirty is larger than both. Right again. Every new value is the largest so far. So every one goes right.
The tree has no left side. It is a chain of six levels. Search for sixty and look at every node. A balanced tree needs only three. For a million values, twenty becomes a million.
Shuffle the values before you insert them. Random order is usually fairly balanced. Or use a self-balancing tree from a library. It repairs its own shape. Either way, the tree stays short and wide.
Search cost is the height. A chain has height n. Check the order of your inserts. Build one in the sandbox. Full breakdown in the lecture.