trees and heaps
Chapters eleven, twelve, fourteen and fifteen cover trees, priority queues, binary search trees and balanced search trees Four chapters for one idea Now I understand why
A tree is what you get when hierarchy matters
The basic shape
A binary tree has nodes with at most two children The root is the top Leaves have no children Height is the longest path down
Traversal comes before the abstract data type section That ordering is right Learn to move through a tree before you build one
Binary search tree
#include <memory>
class BST {
public:
explicit BST(int v) : value(v) {}
void insert(int v) {
if (v < value) {
if (!left) left = std::make_unique<BST>(v);
else left->insert(v);
} else if (v > value) {
if (!right) right = std::make_unique<BST>(v);
else right->insert(v);
}
}
bool contains(int v) const {
if (v == value) return true;
if (v < value && left) return left->contains(v);
if (v > value && right) return right->contains(v);
return false;
}
private:
int value;
std::unique_ptr<BST> left, right;
};
Smaller goes left, larger goes right
Search follows the same path
Ownership is handled by unique_ptr, so a destructor is not a research project
The trap to watch for
A binary search tree can become skewed Insert sorted data and every node has one child The tree is now a linked list wearing a costume Search degrades from logarithmic to linear
That is why chapter fifteen covers AVL trees, red black trees, splay trees and B trees They are different ways to enforce balance
Height matters
Sections cover the height of BSTs and the height of AVL trees
Height is the cost of a search
A balanced tree keeps height close to log n
A skewed tree makes height n
Priority queue
#include <queue>
#include <vector>
#include <functional>
std::priority_queue<int, std::vector<int>, std::greater<int>> heap;
heap.push(3);
heap.push(1);
heap.push(6);
int smallest = heap.top();
Output value:
1
A heap gives the smallest item quickly It does not keep the whole collection sorted That distinction matters A heap that promises sorted order is like a terminal that promises silence
Real uses
- schedulers choosing the next job
- top k results from a stream
- Dijkstra style graph search
- event loops picking the next timer
The tournament tree chapter is the same instinct for repeated selection
Choosing
| Need | Structure |
|---|---|
| exact key lookup | unordered_map |
| ordered range queries | balanced tree |
| repeated minimum | heap |
| hierarchical data | tree |
Mistakes
- assuming a BST is balanced without checking insertion order
- using a heap when you need sorted output
- forgetting that duplicates need an explicit rule
- confusing tree height with node count
- owning nodes with raw pointers and hoping for the best
Exercises
- Insert
10, 5, 15, 12into a BST by hand, then verify with code - Insert
1, 2, 3, 4and draw the shape you get - Use a priority queue to find the three smallest ports in a list of twenty
- Explain why balanced search trees deserve four chapters
What to remember
Trees buy order at the cost of maintenance Heaps buy priority at the cost of full ordering Pick the one that matches the question
Posted