Rooted tree; edge labels are characters; root-to-node path spells a prefix
Prefix code
Codeword set where no codeword is a prefix of another (⟺ symbols at leaves)
Huffman coding
Greedy build of an optimal prefix-code tree by repeatedly merging the 2 lowest frequencies
Key theorems
Theorem
Statement
Proof engine
Characterizations (31.1)
Tree ⟺ unique path between every pair ⟺ minimally connected ⟺ maximally acyclic
cycle of implications
Leaf existence (31.3)
Tree with $\ge 2$ vertices has a degree-1 vertex
take the longest path; its endpoint has degree 1
Edge count (31.2)
Tree on $n$ vertices has exactly $n-1$ edges
induction: strip a leaf, recurse
Converse
Connected graph with $n-1$ edges is a tree
too few edges to also have a cycle
Huffman optimality (31.4)
Huffman minimizes $\sum_s f(s)\,d(s)$ over all prefix codes
greedy exchange argument + induction
Memory hook:connected + acyclic ⇒ exactly $n-1$ edges. Spend every edge on connectivity, none
on redundancy. Remove any edge → disconnects; add any edge → makes a cycle.
Traversals — pick by when the root is visited
Traversal
Visit order
Yields
Use it to…
Preorder
root, $L$, $R$
prefix / Polish
serialize/copy a tree, top-down listing
Inorder
$L$, root, $R$
infix
read a BST in sorted order
Postorder
$L$, $R$, root
postfix / RPN
evaluate expression tree; free a tree
Level-order
by increasing depth
—
BFS on a tree (Ch. 28)
Sample tree 1(2(4,5),3): pre $=[1,2,4,5,3]$; in $=[4,2,5,1,3]$; post $=[4,5,2,3,1]$.
Preorder + inorder uniquely reconstruct a binary tree.