Binary Code Trees and Prefix-Free Bit-Stream Coding #
This module defines full binary code trees (CodeTree α), their weighted external path length
(CodeTree.weightedPathLength), their prefix-free binary codebook (CodeTree.codewords), and
total bit-stream encoding and decoding (CodeTree.encode, CodeTree.decode), connecting the tree
model to Mathlib's InformationTheory.IsPrefixFree and InformationTheory.IsUniquelyDecodable
([Huf52]; [CLRS09], Section 16.3; [CT06], Chapter 5).
Main definitions #
Cslib.Algorithms.Lean.Huffman.CodeTree: full binary tree with symbol-labeled leaves.CodeTree.leaves,CodeTree.leafDepths,CodeTree.maxDepth: leaf sequence,(symbol, depth)pairs, and maximum leaf depth.CodeTree.weight,CodeTree.weightedPathLength: total leaf weight and weighted external path length (defined recursively via subtree weight sums).CodeTree.codewords,CodeTree.codewordSet: canonicalfalse/trueprefix-free codebook.CodeTree.encodeSym,CodeTree.encode,CodeTree.decodeOne,CodeTree.decode: bit-stream encoder and tree-walk decoder.
Main results #
CodeTree.weightedPathLength_eq_sum_leafDepths,CodeTree.weightedPathLength_eq_sum_codewords: external-path-length identity ([CLRS09], Equation (16.4) and Exercise 16.3-4) equating the recursive internal-weight sum with∑ (a, d), d • w a.CodeTree.weightedPathLength_join: compositional path-length identity for grafting a forest of code trees onto a skeletonCodeTree (CodeTree α).CodeTree.isPrefixFree_codewordSet,CodeTree.isUniquelyDecodable_codewordSet,CodeTree.isUniquelyDecodable_codewordSet_iff: everyCodeTreecodebook isInformationTheory.IsPrefixFree, and aCodeTreecodebook isInformationTheory.IsUniquelyDecodableif and only if the tree has at least two leaves (a single leafleaf aassigns the empty codeword[]).CodeTree.decode_encode,CodeTree.length_encode_eq_weightedPathLength_count: lossless bit-stream round-trip and exact equality between compressed bit length and weighted external path length under empirical symbol counts.
References #
- [Huf52] David A. Huffman, A Method for the Construction of Minimum-Redundancy Codes, 1952.
- [CLRS09] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms (3rd ed.), Section 16.3, Equation (16.4) and Exercise 16.3-4, 2009.
- [CT06] Thomas M. Cover and Joy A. Thomas, Elements of Information Theory (2nd ed.), Chapter 5, 2006.
Left-to-right list of leaf symbols in T.
Maximum root-to-leaf depth of T.
Subtree weight and weighted external path length #
Weighted external path length of T, defined recursively via internal child-weight sums
([CLRS09], Exercise 16.3-4): 0 at a leaf, and
l.weightedPathLength w + r.weightedPathLength w + (l.weight w + r.weight w) at an internal
branch.
External-Path-Length Identity ([CLRS09], Equation (16.4) and Exercise 16.3-4): the
recursive internal-weight sum weightedPathLength w equals ∑ (a, d) ∈ T.leafDepths, d • w a.
Grafting subtrees onto a skeleton S : CodeTree (CodeTree α) decomposes weighted external path
length into the skeleton's weighted path length (under subtree weights) plus the sum of the
subtrees' own weighted path lengths.
Prefix-free codebook and bit-stream encoding/decoding #
Every CodeTree codebook satisfies Mathlib's InformationTheory.IsPrefixFree.
Every non-leaf CodeTree codebook satisfies Mathlib's
InformationTheory.IsUniquelyDecodable.
A CodeTree codebook is InformationTheory.IsUniquelyDecodable if and only if the tree has
at least two leaves: every branch l r assigns nonempty prefix-free codewords, whereas a single
leaf leaf a assigns the empty codeword [], violating
InformationTheory.IsUniquelyDecodable.epsilon_notMem.
Lookup the binary codeword of symbol a in T (first match in left-to-right order).
Encode a message msg : List α by concatenating symbol codewords.
Lossless Bit-Stream Round-Trip: for any non-leaf CodeTree (branch l r) and any message
whose encoded bit stream is bits, decode recovers msg exactly.
When T.leaves.Nodup, the bit length of T.encode msg equals the weighted external path length
of T under the empirical symbol frequency function fun a => msg.count a.