Both structures put their keys in order — but a BST spreads them out for searching, while a heap crowds the minimum at the very top.
1 The BST rule
At every node: everything in the left subtree is smaller, everything right is bigger. One rule, applied recursively.
2 Search = descend
Compare and go left or right. A balanced 1,000-key BST finds any key in about 10 comparisons — one per level.
3 Insert = same walk
Follow the compares until you hit an empty slot, then link the new node in. Nothing else ever moves.
4 Heap = tree in a list
A complete binary tree stored flat in an array: children of slot i live at 2i+1 and 2i+2, the parent at (i-1)//2.
5 The heap deal
heap[0] is always the minimum. heapify costs O(n); push and pop cost O(log n) — priority-queue fuel.
6 heapq built in
Python ships heapq: heapify, heappush and heappop work on a plain list. One import, zero classes.
Two ordered cousins
The BST rule1At every node: everything in the left subtree is smaller, everything right is bigger. One rule, applied recursively. Search = descend2Compare and go left or right. A balanced 1,000-key BST finds any key in about 10 comparisons — one per level.
Insert = same walk3Follow the compares until you hit an empty slot, then link the new node in. Nothing else ever moves.
Heap = tree in a list4A complete binary tree stored flat in an array: children of slot i live at 2i+1 and 2i+2, the parent at (i-1)//2. The heap deal5heap[0] is always the minimum. heapify costs O(n); push and pop cost O(log n) — priority-queue fuel. heapq built in6Python ships heapq: heapify, heappush and heappop work on a plain list. One import, zero classes. 6 building blocks make up Two ordered cousins
Four keys join the tree, one compare at a time — every insert is a walk from the root to an empty slot.
1 / 10First key 50 arrives — it becomes the root. Every insert that follows starts comparing right here.
Inserting 40 into the tree from the viz — one comparison per level, left or right, then link it in.
Insert by compare-and-descend, then let heapq do the heap heavy lifting on a plain list.
moduleroot = Node(50) — the first key claims the root slot.
Memory
root= key=50, left=None, right=None
Console
output appears as the program runs…
Same numbers as the code: build-heap in O(n), then a heappop that sifts the last element down.
1 / 10A jumbled list [9, 5, 6, 2, 3] laid out as a heap array. Goal: a min-heap — every parent ≤ its children. Sub-labels are array slots.
This BST hides a party trick — one traversal prints its keys sorted.
Your predicted output — one line per console line
Hint: Inorder = left subtree, node, right subtree. On a BST that order is always ascending — the tree sorts itself.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?BST ruledrops into place
?inorder on BSTdrops into place
?O(h)drops into place
?2i + 1, 2i + 2drops into place
?(i - 1) // 2drops into place
?heap[0]drops into place
?heapifydrops into place
?heappopdrops into place
01BST rule: left < node < right — search and insert are the same compare-and-descend walk, O(h).
02Inorder traversal of a BST emits keys in sorted order — free sorting from structure.
03A heap is a complete tree in a plain list: kids of i at 2i+1 and 2i+2, parent at (i-1)//2.
04heap[0] is always the minimum — peek O(1), push/pop O(log n) after a O(n) heapify.
05heappop hands you the smallest item; heappush bubbles the newcomer up past bigger parents.
06BSTs win when you need any key fast; heaps win when every question is “what is the smallest?”.