# 前缀码

前缀码（又称无前缀码或即时码）是一种变长码，其中任何码字都不是其他码字的前缀，因此由码字拼接而成的码流可以逐符号无歧义地解析，无需分隔符，也无需前瞻。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup> 前缀码是无损数据压缩的主力：DEFLATE、Zstandard、Brotli、JPEG 和 HPACK 都用霍夫曼前缀码来编码符号。<sup>[2](https://www.rfc-editor.org/info/rfc1951/)</sup><sup> • </sup><sup>[3](https://www.rfc-editor.org/rfc/rfc8878.html)</sup><sup> • </sup><sup>[4](https://www.rfc-editor.org/rfc/rfc7932.html)</sup>

| 关键事实 | 细节 |
|---|---|
| 定义性质 | 任何码字都不是其他码字的前缀；在码树中，每个码字都位于叶节点上，因此没有任何码字处在通往其他码字的路径上。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup> |
| 解码 | 解码器沿码树逐比特下行，一旦到达叶节点就立即输出该符号；每个码字在其最后一个比特到达的瞬间即被识别——这正是“即时”的含义。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup> |
| 克拉夫特—麦克米伦不等式 | 对每个唯一可译码，\( \sum_i 2^{-\ell_i} \le 1 \)（二进制）；反之，任何满足该不等式的整数码长都可由某个前缀码实现。<sup>[5](https://web.stanford.edu/class/ee376a/files/scribes/lecture8.pdf)</sup> |
| 熵界 | 任何唯一可译的 \( D \) 元码的期望长度至少为 \( H_D(P) \)，符号 \( i \) 的理想码长为 \( \ell_i^* = \log_D(1/p_i) \)。<sup>[6](https://www.cs.cmu.edu/~aarti/Class/10704/lec7-kraft.pdf)</sup> |
| 最优构造 | 霍夫曼 1952 年的算法构造出最小冗余前缀码，即在给定符号集下平均长度最低的码。<sup>[7](https://compression.ru/download/articles/huff/huffman_1952_minimum-redundancy-codes.pdf)</sup> |
| 实际限制 | Zstandard 将其霍夫曼前缀码的上限设为 11 比特；Brotli 和 DEFLATE 使用仅由码长定义的规范前缀码。<sup>[3](https://www.rfc-editor.org/rfc/rfc8878.html)</sup><sup> • </sup><sup>[4](https://www.rfc-editor.org/rfc/rfc7932.html)</sup> |

## 定义与解码

前缀条件只有一条：对任意两个不同的码字，任何一个都不是另一个的前缀。诸如 {00, 01, 10, 11} 这样的定长码天然满足该条件，因为长度相同的不同字符串不可能互为前缀，所以每个定长码都是前缀码。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup> 而像 {1, 01, 011, 010} 这样的码违反该条件，因为 01 是 011 和 010 的前缀，而一处违反就已足够：此时码流 011 有两种合法解析，即 C，或 B 后接 A。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup>

解析前缀码流既不需要分隔符，也不需要前瞻。解码器从码树根出发，每读一比特走一条分支，到达叶节点时输出该叶节点的符号并返回根。由于没有任何码字越过叶节点继续延伸，每个码字在其最后一个比特到达的那一刻即被确定。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup> 大卫·A·霍夫曼（David A. Huffman）当时是 IRE 的准会员，他在 1952 年的论文中将同一条件表述为限制 (b)：任何消息码都不得逐位地作为任何更长消息码的前一部分出现。<sup>[7](https://compression.ru/download/articles/huff/huffman_1952_minimum-redundancy-codes.pdf)</sup>

**无前缀性与唯一可译性。** 前缀条件是唯一可译的充分条件，但并非必要条件。逗号码 {0, 01, 011, 0111} 违反了前缀条件多达六次，但每个码字都以其中唯一的 0 开头，因此码流中每个 0 都标志着一个新码字的开始，解析结果因而唯一确定；代价是解码延迟——读到 01 之后，接收方必须等待下一个 0 才能知道该码字是否已经结束。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup> 仅仅凭观察来判断一个码是否唯一可译，可能极其困难，萨尔迪纳斯—帕特森算法（Sardinas–Patterson algorithm）正是为自动检验这一点而设计的；对码 {1, 011, 01110, 1110, 10011} 运行时，它发现两个导出集合共享一个码字，于是报告该码不具有唯一可译性。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup><sup> • </sup><sup>[8](https://www.cs.auckland.ac.nz/~cristian/314Assignment1-2012S.pdf)</sup>

## 克拉夫特—麦克米伦不等式

唯一可译性对码字长度施加了严格的限制：某些码字只有以加长其他码字为代价才能缩短。克拉夫特—麦克米伦不等式量化了这一平衡。对每个码长为 \( \ell(u) \) 的唯一可译二进制码，<sup>[5](https://web.stanford.edu/class/ee376a/files/scribes/lecture8.pdf)</sup>

\[ \sum_{u} 2^{-\ell(u)} \le 1, \]

而对 \( D \) 元码，相应的表述是 \( \sum_i D^{-\ell_i} \le 1 \)。<sup>[6](https://www.cs.cmu.edu/~aarti/Class/10704/lec7-kraft.pdf)</sup> 逆命题同样成立：任何满足该不等式的整数值长度函数，都是某个唯一可译码的长度函数，而且实际上可以是某个前缀码的长度函数，并可用霍夫曼码构造出来。<sup>[5](https://web.stanford.edu/class/ee376a/files/scribes/lecture8.pdf)</sup> 一个具体的反例展示了该界的作用：码长 (1, 1, 2, 2) 给出 \( 2^{-1} + 2^{-1} + 2^{-2} + 2^{-2} = 1.5 > 1 \)，因此不存在具有这些码长的唯一可译码。<sup>[9](https://citeseerx.ist.psu.edu/document?doi=a7861578b545dd6c231574a92d96e607af885743&repid=rep1&type=pdf)</sup>

该不等式有一个推论，使前缀限制不带来任何代价：既然每个唯一可译码都受制于与前缀码相同的码长约束，那么任何可由延迟解码码达到的期望长度，都可由具有相同码长且无解码延迟的前缀码达到。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup> 该不等式以莱昂·G·克拉夫特（Leon G. Kraft）和麦克米伦命名：克拉夫特 1949 年的 MIT 硕士论文《一种用于量化、分组和编码调幅脉冲的装置》（*A Device for Quantizing, Grouping, and Coding Amplitude-Modulated Pulses*）推导出了这一编码界，麦克米伦后来将其推广到所有唯一可译码。<sup>[10](https://dspace.mit.edu/entities/publication/77df9efd-cec6-4ae8-8b45-2a3a5e93d2ba)</sup><sup> • </sup><sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup>

## 熵界

该不等式给出了压缩的基本极限。对于从分布 P 中抽取的随机符号，任何在 D 元字母表上的唯一可译编码方案——包括所有前缀码——所达到的平均编码长度都不小于熵 H_D(P)。证明在约束 \( \sum_i D^{-\ell_i} \le 1 \) 下最小化 \( \sum_i p_i \ell_i \)；最优解满足 \( D^{-\ell_i^*} = p_i \)，因此符号 \( i \) 的理想码长为 \( \ell_i^* = \log_D(1/p_i) \)，即香农信息量，而最小期望长度等于 \( H_D(P) \)。<sup>[6](https://www.cs.cmu.edu/~aarti/Class/10704/lec7-kraft.pdf)</sup>

只有当每个概率都是 \( 1/2 \) 的幂、从而每个惊奇度 \( -\log_2 p_i \) 都是整数时，该下界才能被精确达到。概率为 \( (\tfrac12, \tfrac14, \tfrac18, \tfrac18) \) 的信源，其 \( H = 1.75 \) 比特，码长为 (1, 2, 3, 3) 的前缀码恰好达到 \( L = 1.75 \) 比特；而对于概率为 (0.5, 0.25, 0.15, 0.1) 的信源，惊奇度 2.737 和 3.322 不是整数，最佳前缀码要付出取整代价，约比约 1.743 比特的熵下界高出 0.007 比特。<sup>[1](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)</sup>

## 霍夫曼编码与最优性

霍夫曼 1952 年发表在《IRE 汇刊》（*Proceedings of the IRE*）上的论文《一种构造最小冗余码的方法》（*A Method for the Construction of Minimum-Redundancy Codes*）将最小冗余码定义为：对于有限消息集和给定数量的可用编码符号，产生尽可能低的平均消息长度的码，并给出了第一个构造此类码的确定程序。<sup>[7](https://compression.ru/download/articles/huff/huffman_1952_minimum-redundancy-codes.pdf)</sup> 霍夫曼指出，克劳德·E·香农（Claude E. Shannon）和罗伯特·M·法诺（Robert M. Fano）所发展的编码程序随着消息集的增大而趋近最优，但并非最优；而克拉夫特曾做过早期工作，朝着为有限消息集推导接近理想的编码方法的方向努力。<sup>[7](https://compression.ru/download/articles/huff/huffman_1952_minimum-redundancy-codes.pdf)</sup> 香农 1948 年发表在《贝尔系统技术期刊》（*Bell System Technical Journal*）上的论文《通信的数学理论》（*A Mathematical Theory of Communication*）提供了衡量这些编码的熵框架。<sup>[11](https://www.princeton.edu/~wbialek/rome/refs/shannon_48.pdf)</sup>

给定符号频率已知的字母表，霍夫曼算法构造出最优前缀码，即对该字母表而言所有可能前缀码中使用比特数最少的码。<sup>[3](https://www.rfc-editor.org/rfc/rfc8878.html)</sup> 在字符编码（每个字符一个码字）方面，霍夫曼码仍然是最优的，但算术编码更为紧凑，因为它能分配分数比特，代价是复杂度更高。<sup>[12](https://xlinux.nist.gov/dads/HTML/arithmeticCoding.html)</sup>

## 实践中的规范前缀码

压缩格式很少直接传输码树本身，而是使用规范前缀码，其定义有两条规则：给定比特长度的所有码，按其所代表符号的顺序具有字典序连续的值；较短的码在字典序上先于较长的码。在这两条规则下，仅比特长度序列就决定了实际的码，因此格式只需存储长度。<sup>[2](https://www.rfc-editor.org/info/rfc1951/)</sup><sup> • </sup><sup>[4](https://www.rfc-editor.org/rfc/rfc7932.html)</sup> 对于字母表 ABCDEFGH，比特长度为 (3, 3, 3, 3, 3, 2, 4, 4)，规范算法将两比特码 00 分配给 F，将四比特码 1110 和 1111 分配给 G 和 H。<sup>[4](https://www.rfc-editor.org/rfc/rfc7932.html)</sup>

各格式对码长设定了不同的限制。DEFLATE（RFC 1951）用 LZ77 加一对霍夫曼码树来压缩每个块，一棵树用于字面量和长度，另一棵独立用于距离，距离限制为 32K 字节，长度限制为 258 字节。<sup>[2](https://www.rfc-editor.org/info/rfc1951/)</sup> Zstandard（RFC 8878）对字面量使用霍夫曼前缀码，并将最大码长限制为 11 比特，指出更多比特能提高精度，但会产生更大的头部和更复杂的解码。<sup>[3](https://www.rfc-editor.org/rfc/rfc8878.html)</sup> Brotli（RFC 7932）对每条命令使用三类前缀码，分别用于插入—复制长度、字面量和距离，并且每类可以使用多个码，按上下文选择。<sup>[4](https://www.rfc-editor.org/rfc/rfc7932.html)</sup> JPEG 1（ITU-T T.81 | ISO/IEC 10918-1）规定了基于 DCT 的霍夫曼编码有损图像格式，同时也提供了算术编码选项。<sup>[13](https://jpeg.org/jpeg/)</sup> 在 HPACK（RFC 7541）中，带有 H = 1 标记的头部字符串字面量使用规范附录 B 中定义的固定霍夫曼码进行编码。<sup>[14](https://www.rfc-editor.org/rfc/rfc7541.html)</sup>

## References

1. [Prefix codes and code trees, distillcourse](https://distillcourse.com/courses/information-theory/lessons/prefix-codes)
2. [RFC 1951: DEFLATE Compressed Data Format Specification version 1.3, P. Deutsch, May 1996](https://www.rfc-editor.org/info/rfc1951/)
3. [RFC 8878: Zstandard Compression and the 'application/zstd' Media Type, Y. Collet and M. Kucherawy, February 2021](https://www.rfc-editor.org/rfc/rfc8878.html)
4. [RFC 7932: Brotli Compressed Data Format, J. Alakuijala and Z. Szabadka, July 2016](https://www.rfc-editor.org/rfc/rfc7932.html)
5. [EE376A Information Theory, Lecture 8 scribe notes, Stanford University](https://web.stanford.edu/class/ee376a/files/scribes/lecture8.pdf)
6. [Lecture 7: Prefix codes, Kraft-McMillan Inequality, 10-704 lecture notes, Carnegie Mellon University (A. Risteski)](https://www.cs.cmu.edu/~aarti/Class/10704/lec7-kraft.pdf)
7. [David A. Huffman (1952). A Method for the Construction of Minimum-Redundancy Codes. Proceedings of the IRE 40(9), 1098–1101.](https://compression.ru/download/articles/huff/huffman_1952_minimum-redundancy-codes.pdf)
8. [Assignment 1 solutions, COMPSCI 314, University of Auckland](https://www.cs.auckland.ac.nz/~cristian/314Assignment1-2012S.pdf)
9. [A Short Course in Universal Coding, CiteSeerX](https://citeseerx.ist.psu.edu/document?doi=a7861578b545dd6c231574a92d96e607af885743&repid=rep1&type=pdf)
10. [Leon Gordon Kraft (1949). A Device for Quantizing, Grouping, and Coding Amplitude-Modulated Pulses. M.S. thesis, MIT Department of Electrical Engineering.](https://dspace.mit.edu/entities/publication/77df9efd-cec6-4ae8-8b45-2a3a5e93d2ba)
11. [Claude E. Shannon (1948). A Mathematical Theory of Communication. The Bell System Technical Journal 27, 379–423, 623–656.](https://www.princeton.edu/~wbialek/rome/refs/shannon_48.pdf)
12. [Arithmetic coding, Dictionary of Algorithms and Data Structures, NIST](https://xlinux.nist.gov/dads/HTML/arithmeticCoding.html)
13. [JPEG 1 overview, JPEG Committee](https://jpeg.org/jpeg/)
14. [RFC 7541: HPACK – Header Compression for HTTP/2, R. Peon and H. Ruellan, May 2015](https://www.rfc-editor.org/rfc/rfc7541.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Combinatorics › Enumerative combinatorics › Combinatorics on words › Applications of combinatorics on words

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/prefix-code

English original: https://www.edgepedia.org/prefix-code
