Binary trees, visualized.
A node with two pointers instead of one. Keep smaller values on the left and bigger ones on the right, and in a well-shaped tree finding anything takes a few hops, not a long walk.
What is a binary tree?
A binary tree is made of nodes, like a linked list, but each node has two pointers: left and right. The node at the very top is the root. The nodes a node points at are its children. A node with no children is a leaf. A node’s depth is how many steps it is below the root.
A binary search tree (BST) adds one rule, everywhere in the tree: every value on a node’s left side is smaller than it, and every value on its right side is bigger. That rule tells you which way to go at every node, so in a balanced tree each step skips about half of what is left.
01 — one item first
Binary search tree insertion: start with one insert.
Build a tree one value at a time. At every node the new value asks one question: am I smaller (go left) or bigger (go right)?
Python
02 — why bother
Find 7: list vs tree.
The same fifteen numbers, twice. A sorted linked list can only walk from the front. A search tree throws away half of what is left at every step.
Sorted linked list
looks: 0
Binary search tree
looks: 0
Press a button to search both at once.
03 — your turn
Insert and search.
Type a number. The ghost starts at the root and compares at every node. Count the comparisons: that is the cost.
Pick a value and press insert or search.
Python
Python
04 — visit every node
Tree traversals: inorder, preorder, postorder and level order.
A list has one order. A tree has several, depending on when you print a node: before its children, between them, after them, or level by level.
Python
Python
Python
Python
05 — taking one out
Delete from a binary search tree, three ways.
Removing a node must keep the rule (smaller left, bigger right) true. A leaf just goes. A node with one child hands its place to that child. A node with two children needs a stand-in.
Pick a case. Each one starts from the same tree.
Python
06 — the classic bug
Sorted input breaks the tree.
Insert values that are already in order, and every new value is bigger than everything before it, so it always goes right. The “tree” is a linked list leaning sideways.
1, 2, 3, 4, 5, 6, 7
looks: –
4, 2, 6, 1, 3, 5, 7
looks: –
Same seven values, two insert orders. Press the button to build both.
07 — spot them in the wild
Tree or not?
A tree has one root, and every other item has exactly one parent. No loops, no item with two parents. Decide for each, then read why.
Folders and files on your computer
A company org chart
Friends on a social network
Autocomplete as you type a word
A database index that finds a row by its id
The undo history in a text editor
The HTML of a web page
A music playlist played in order
08 — the boss level
Validate a binary search tree.
A classic interview question. Checking each parent against its children is not enough: a value deep down must also respect every ancestor above it. The trick: pass an allowed range down the tree.
Python
09 — cheat sheet
Binary search tree time complexity.
Everything costs one step per level, so it all depends on the height. A balanced tree with n nodes is about log₂ n levels tall (20 levels for a million). A lopsided one can be n levels tall.
| Job | Balanced | Lopsided (sorted input) |
|---|---|---|
| Search, insert, delete | O(log n) | O(n)a walk down a list |
| Smallest / largest value | O(log n)keep going left / right | O(n) |
| All values in sorted order | O(n)in-order walk | O(n) |
| Memory | O(n)two pointers per node | O(n) |
| You want… | In Python |
|---|---|
| A sorted collection you add to and remove from | Python has no built-in BST. A popular choice is sortedcontainers.SortedList (pip install sortedcontainers): adds, removes and lookups in about O(log n). |
| Fast lookups in a list you keep sorted | bisect.bisect_left finds a spot in O(log n); bisect.insort inserts, but shifting the list makes that O(n). |
| A tree that stays balanced by itself | That is an AVL tree or a red-black tree. Java’s TreeMap and C++’s std::map are usually red-black trees. |