Binary Search Trees
A binary search tree keeps values sorted with one simple rule โ smaller left, larger right โ which makes searching fast and reading them back in order effortless. A foundational structure.
The binary search tree (BST) is one of the most fundamental structures in computing, and it rests on a single elegant rule. Values are arranged in a tree so that, at every node, smaller values go to the left and larger to the right. That one rule makes searching fast, keeps the data sorted, and lets you read it back in order effortlessly. This session is about how a BST works and why that simple rule is so powerful.
One rule: smaller left, larger right
Every node in a BST follows the same rule, and that's what makes it work: everything in a node's left branch is smaller, everything in its right branch is larger.
5
/ \
3 8
/ \ / \
1 4 7 9
# at every node: left branch < node < right branchWhat a BST gives you
A BST shines when you need both fast search and sorted order as data changes - something a plain list or dict struggles to do together.
| Operation | In a BST |
|---|---|
| Search for a value | Fast - halve the tree each step |
| Insert a value | Fast - follow the path to an empty spot |
| Read in sorted order | Free - an in-order traversal |
| Find min / max | Follow all-left / all-right |
The stack's <a href='/stack/larztree'>larztree</a> implements exactly this - insert/contains/remove, min/max, and the in-order traversal that yields sorted values. Try inserting numbers in random order and calling <code>inorder()</code>: they come out sorted, because the structure maintained order all along. That combination of fast search and effortless sorted reading is the BST's superpower.
Fast search, sorted data, one rule
- The rule: at every node, left branch < node < right branch.
- Search is fast - each comparison discards half the tree (like binary search).
- Sorted reading is free - an in-order traversal (left, node, right).
- larztree is a readable BST - insert, search, remove, min/max, traversals.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- A binary search tree follows one rule: at every node, smaller values go left, larger go right.
- That rule makes search fast - each comparison discards half the tree, like binary search (O(log n) balanced).
- Reading a BST in order (left, node, right) gives sorted values for free.
- A BST uniquely offers fast search AND sorted order as data changes - see larztree.
Frequently asked
A BST is fast 'when balanced' - what happens when it isn't, and does it matter?
This is an important real-world caveat: a binary search tree's fast (O(log n)) performance depends on it being reasonably balanced, and when it becomes unbalanced, performance degrades - in the worst case, all the way to slow (O(n)). Here's what happens. The 'halve the tree each step' speed relies on the tree being bushy - roughly balanced, so its height is small (about log n) and each search step meaningfully cuts down the remaining nodes. But a BST's shape depends on the order values are inserted. If you insert values in already-sorted order (1, 2, 3, 4, 5...), each new value is larger than all before it, so it goes right every time, and the tree degenerates into essentially a linked list - a long thin chain with no branching. Now searching means walking down that chain one node at a time, which is O(n) - no faster than scanning a list, defeating the point of the tree. Unbalanced trees (from sorted or nearly-sorted insertion, or unlucky patterns) lose the logarithmic speed. Does it matter? It can, in practice, which is why it's important to know. If you're inserting data in random order, a plain BST tends to stay reasonably balanced on average and performs well, so for many uses a simple BST is fine. But if your data might arrive sorted or in patterns that unbalance the tree, a plain BST can silently become slow. The solution the field developed is self-balancing binary search trees - variants (with names like AVL trees and red-black trees) that automatically rearrange themselves during insertion and deletion to stay balanced, guaranteeing O(log n) performance regardless of insertion order. These are more complex to implement (they add balancing logic), which is why a learning-focused BST like larztree is a plain (unbalanced) one - perfect for understanding the core idea, while real-world libraries and languages use self-balancing versions under the hood for their sorted-map/set types. So the practical takeaways: a plain BST is great for learning and fine when data arrives in random-ish order, but be aware it can degrade to O(n) if data comes sorted or adversarially; and for production use where you need guaranteed performance, you'd use a self-balancing BST (or a library structure that is one). The core BST idea you're learning is the foundation; balancing is the refinement that makes it robust in all cases. Understanding this caveat is part of understanding BSTs properly - the simple version teaches the concept, and knowing about balancing tells you when the simple version is enough and when you'd want the more sophisticated one. It's a good example of how a beautiful simple idea (the BST rule) meets a real-world complication (balance) that has a known solution (self-balancing variants).
How is a binary search tree different from the 'binary search' algorithm from DS-101?
They're closely related - a binary search tree is essentially the binary search idea built into a data structure - but they apply to different things and have different trade-offs, so it's worth distinguishing them. Binary search (the algorithm from DS-101) is a way to search a sorted array/list quickly: you look at the middle element, decide whether your target is in the left or right half, and repeat, halving the search space each step - achieving O(log n) search. But it requires the data to be in a sorted array, and here's the catch: keeping an array sorted as you insert new items is slow, because inserting into the middle of an array means shifting everything after it (O(n) per insertion). So binary search on an array is great when the data is static (you sort once, then search many times), but poor when data changes frequently. A binary search tree takes the same halving-search idea but structures the data as a tree instead of an array, which solves the insertion problem. In a BST, the smaller-left/larger-right arrangement lets you search by halving (like binary search - go left or right at each node, discarding half), but inserting a new value is also fast (O(log n) balanced) because you just follow the path to an empty spot and add a node - no shifting required. So a BST gives you both fast search AND fast insertion on changing data, where a sorted array gives fast search but slow insertion. The essence: binary search is an algorithm for searching a static sorted array quickly; a binary search tree is a data structure that applies the same halving-search principle but supports efficient insertion and deletion, making it suitable for data that changes. They share the core insight (halve the search space using order), but the BST embeds it in a structure that stays efficient under modification. Another difference: reading a sorted array in order is trivial (it's already sorted), while reading a BST in sorted order requires an in-order traversal (though that's easy and fast). And a sorted array is more memory-compact and cache-friendly, while a tree uses pointers and more memory but handles dynamic data better. So which to use depends on your situation: a sorted array with binary search for static data you search a lot; a BST (or balanced BST) for data that changes and needs both fast search and fast updates while staying ordered. Understanding both, and their relationship, gives you the judgement to pick the right one. The BST is, in a real sense, the dynamic, insertable cousin of binary-search-on-an-array - same searching principle, different structure to handle change efficiently. This connection is a nice example of how an algorithm (binary search) and a data structure (BST) embody the same core idea for different circumstances.
Related lessons
Beyond Lists & Dictionaries
Lists and dictionaries take you a long way โ but some problems need more. When you need daโฆ
Trees, Heaps & TriesVisiting Every Node: Traversals
Once you have a tree, you often need to visit every node โ but in what order? The four traโฆ
Data Structures & AlgorithmsAlgorithms & Big-O Made Simple
An algorithm is just a recipe for solving a problem โ and Big-O is a simple way to talk abโฆ