Binary Search Trees and Heaps
Lesson 15 of 20Watch 47 and 12 walk the compare path into a BST, then see 9 sift to the root of a min-heap — PriorityQueue included.
Why ordered trees
Two structures promise order without sorting: the BST keeps values navigable, the heap keeps the minimum on tap. Both are just binary trees with a rule.
The BST rule
Every node obeys left < node < right. One invariant, and inorder traversal prints the whole tree sorted.
Insert = a search that lands
Walk down comparing; the first null slot is the new home. Cost is O(h) — the height, not n.
Balance is money
Balanced height ≈ log₂ n: a million keys need about 20 levels. A skewed BST collapses into a linked list.
Heaps live in arrays
Children of index i sit at 2i + 1 and 2i + 2; the parent is (i − 1) / 2. No pointers needed at all.
Java ships both
TreeMap is a balanced BST under the hood; PriorityQueue is a min-heap — poll() always returns the smallest.
BST insert live
Two inserts, one rule: compare on the way down, attach at the first null slot.
This BST already holds 7 keys. Watch 47 find its home — the walk starts at the root, 60.
Insert flow
The whole BST insert is one loop: compare, branch, repeat — until the tree runs out of nodes.
Walkthrough
Recursive BST insert with the full compare path — then a min-heap sift-up on a plain array.
1public class BstInsert {2static class Node {3int val; Node left, right;4Node(int v) { val = v; }5}67static Node insert(Node n, int v) {8if (n == null) return new Node(v);9if (v < n.val) n.left = insert(n.left, v);10else n.right = insert(n.right, v);11return n;12}1314public static void main(String[] args) {15Node root = null;16int[] keys = {60, 34, 78, 22, 45, 47};17for (int k : keys) root = insert(root, k);18System.out.println("47 hangs under " + root.left.right.right.val);19}20}
root = null — the tree starts empty.
Heap sift-up live
The same tree as an array: append 9 at index 6, then bubble it up while it beats its parent.
A valid min-heap — every parent ≤ its children. About to append 9.
Predict the output
PriorityQueue defaults to a min-heap — and the parent formula never lies.
1import java.util.PriorityQueue;23public class Predict {4public static void main(String[] args) {5PriorityQueue<Integer> pq = new PriorityQueue<>();6pq.add(24);7pq.add(11);8pq.add(40);9System.out.println(pq.peek());10System.out.println(pq.poll());11System.out.println(pq.peek());12int[] heap = {9, 24, 11, 31, 27, 40, 18};13System.out.println("parent of 18: " + heap[(6 - 1) / 2]);14System.out.println("parent of 31: " + heap[(3 - 1) / 2]);15}16}
Hint: PriorityQueue<Integer> orders smallest-first by default. In the array, 18 sits at index 6 and 31 at index 3 — apply (i − 1) / 2.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
A BST keeps left < node < right, so inorder prints sorted data for free.
Insert and search walk one root-to-leaf path — O(h); balanced h ≈ log₂ n.
A skewed BST degrades into a linked list: O(n). Balance is everything.
A heap is a complete tree: append at the end, then sift up (or down).
Parent of i is (i − 1) / 2; children are 2i + 1 and 2i + 2 — a heap is an array in disguise.
PriorityQueue<Integer> is a min-heap by default — poll() returns the smallest.