> For the complete documentation index, see [llms.txt](https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/38.-compression-and-complexity/38.2-prefix-free-codes.md).

# 38.2 Prefix-free Codes

Consider the representation of English text in Java. We represent text as a sequence of characters, each taking 8 bits of memory.

One easy way to compress, then, is to simply use less than 8 bits per character. To do this, we have to decide which **codewords** (bit sequences) go with each **symbol** (character).

## Mapping Alphanumeric Symbols

### Morse Code

As an introductory example, consider the Morse code alphabet. Looking at the alphabet below, what does the sequence – – • – – • represent? It’s ambiguous! The same sequence of symbols can represent either MEME, or GG, depending on what you choose – – • to represent

<figure><img src="https://2512475561-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBGDh1mPw1Aw5u4IlOFCn%2Fuploads%2Fgit-blob-af93148ad84887e9e2cafe5f1ae92b36e8d038b5%2FScreen%20Shot%202023-04-24%20at%205.44.37%20PM.png?alt=media" alt=""><figcaption><p>Ambiguity in morse code</p></figcaption></figure>

In real usage, operators must pause between codewords to indicate a break. The pause acts as an implicit third symbol, but we can't encode this real-time information into our code.

### Prefix-free Codes

An alternate strategy to avoid the need for real-time is to use **prefix-free codes**. In a prefix-free code, no codeword is a prefix of any other. In the Morse Code example, there would be no confusion whether the – – in the pattern – – • – – • is supposed to represent M, or the start of G.

Let's represent Morse code as a tree of codewords leading to symbols. As we can see from the tree, several symbols have representations that are prefixes of other symbols.

<figure><img src="https://2512475561-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBGDh1mPw1Aw5u4IlOFCn%2Fuploads%2Fgit-blob-612bd6178a092a08a4fccda4f6b2020327e4a60f%2Fimage%20(4).png?alt=media" alt=""><figcaption><p>Morse code is not prefix-free.</p></figcaption></figure>

As an example of an (arbitrary) prefix-free code, consider the following encoding:

<figure><img src="https://2512475561-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBGDh1mPw1Aw5u4IlOFCn%2Fuploads%2Fgit-blob-49f9a5f1629fe26330be18e5d3eec07e842348b5%2Fimage%20(64).png?alt=media" alt=""><figcaption><p>One prefix-free code.</p></figcaption></figure>

The following code is also prefix-free:

<figure><img src="https://2512475561-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBGDh1mPw1Aw5u4IlOFCn%2Fuploads%2Fgit-blob-53a8b741c41d2997ae979686acad43fc98c622d8%2Fimage%20(60).png?alt=media" alt=""><figcaption><p>Another prefix-free code.</p></figcaption></figure>

Note that some codes are more efficient for certain strings than others: in the first representation, `I ATE` uses less bits than the second code. However, this is highly dependent on what string we're trying to encode.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/38.-compression-and-complexity/38.2-prefix-free-codes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
