Binary Search Trees
From the shape of a binary tree to the ordering rule that makes it searchable — then one node type and seven routines: three depth-first traversals, a node count, insertion and search.
- 01Cover
- 02Binary Tree
- 03Binary Search Tree
- 04Tree Terminology
- 05Node Structure
- 06Pre-order Traversal
- 07In-order Traversal
- 08Post-order Traversal
- 09Node Count
- 10BST Insertion
- 11BST Search
What is a Binary Tree?
A tree where every node holds at most two children — a left and a right — and exactly one path leads from the root to each node.
nullptr.left and right are both empty.Binary means at most two: 50 would need three pointers to hold 30, 70 and 90, and the struct only has left and right.
What is a Binary Search Tree?
A binary tree with one extra promise: everything left of a node is smaller, everything right of it is larger — at every node, not just the root.
The rule is checked at every node: 60 is right of 50 but left of 70, and both promises hold at the same time.
Binary, yes — searchable, no. 60 sits on 50's left, yet a search for it would compare 60 > 50 and turn right.
Tree Terminology
Six words that every later slide depends on — all of them pointing at the same seven-node tree.
Leaves: 20, 40, 60, 80 · siblings: 20 and 40 · 30 is their parent · height 2, size 7.
Node Structure
One value and two pointers. data carries the key, left and right point at the two subtrees — or at nothing at all.
intNode*Node*NodeNode* — the struct points at its own type, which is what lets a node hold a subtree.data50the key this node holdsleftnullptrno left subtree yetrightnullptrno right subtree yetPre-order: Root First
Print the node the moment you reach it, then recurse into its left subtree, then its right.
50 — the node itself, before anything below it.30 20 40 — the whole left subtree, same rule again.70 60 80 — the whole right subtree, last.In-order: Smallest First
Take the whole left subtree first, then print the node, then take the right. In a search tree that walks the keys in ascending order.
20 30 40 — everything smaller than the node, already sorted.50 — the node, once its left side is spent.60 70 80 — the larger keys, last.Post-order: Children First
Both subtrees first, the node last. Nothing is printed until everything beneath it has already been printed.
20 40 30 — the left subtree, itself walked children-first.60 80 70 — the right subtree, same rule again.50 — the node, only once both sides are spent.Node Count: One Plus Two
The size of a tree is one — for the node itself — plus the size of its left subtree and the size of its right subtree. The same question, asked of two smaller trees.
0 — the one case that needs no recursion.1 — every node counts itself first.+ left + right — both answers come back before the sum.Insert: Follow the Comparisons
Compare, turn, repeat: the first empty pointer the walk reaches is the slot.
Search: Halve, Then Halve Again
Equal means found; smaller goes left, larger goes right; nullptr means absent.
Value 90 NOT found.