Core · Module 10 · Lesson 2 of 5
Binary search trees
A binary search tree keeps smaller keys to the left of each node and larger keys to the right, so a search discards a whole subtree at every step. Its speed depends entirely on its height.
By the end you can
- State the binary search tree property and check it correctly
- Search for a key, find the minimum and maximum, and find a key's successor
- Explain why every operation costs O(height), and how the order of insertions sets the height
Builds on Vocabulary and traversals, Binary search without off-by-one bugs.
01 · A question first
A sorted array finds a key in O(log n) but needs O(n) to insert one. A hash table inserts in O(1) but can't tell you the smallest key above 50. A binary search tree aims to do both kinds of work in O(log n). What does it store at each node to make that possible, and when does the promise fail?
Make a guess before you read on.
02 · The idea
Members only
Unlock the full lesson
This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.
What's inside
- The search tree property
- Searching
- Ordered operations
- Height is everything
- Storing more in each node
- BST or hash table?
- Pseudocode & Multi-Language Implementations (C++, Java, Python)
- Complexity Derivation, Edge Cases & Common Mistakes
03 · Practice
Asked in interviews
Something wrong with this lesson? Report a problem