0% found this document useful (0 votes)
15 views

Huffman Coding Algorithm

Uploaded by

KhOa Lê
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Huffman Coding Algorithm

Uploaded by

KhOa Lê
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Huffman Coding is a compression algorithm that reduces the size of data by encoding

frequently occurring characters with shorter bit sequences. It begins by calculating the
frequency of each character in a string and then builds a binary tree, called the Huffman Tree,
where characters with lower frequencies are placed deeper in the tree and those with higher
frequencies closer to the root. Each character is assigned a unique binary code based on its
position in the tree—left branches represent a `0`, and right branches represent a `1`. This way,
frequently used characters have shorter codes, achieving efficient compression by minimizing
the total number of bits used to represent the string.
Here's a simple example of Huffman Coding:

Suppose we have the string: **"AABBCDDDD"**

1. **Calculate Frequencies:**
- A: 2
- B: 2
- C: 1
- D: 4

2. **Build the Huffman Tree:**


- Start by creating nodes for each character with their frequencies.
- Combine the two nodes with the smallest frequencies repeatedly until you have a single tree:
- Combine C (1) and A (2) to get a node with frequency 3.
- Combine B (2) and the previous node (3) to get a node with frequency 5.
- Combine D (4) and the previous node (5) to form the root node with frequency 9.

3. **Assign Codes:**
- Traverse the tree, assigning `0` for left branches and `1` for right branches.
- Example codes from the tree:
- A: `010`
- B: `011`
- C: `00`
- D: `1`

4. **Encode the String:**


- The original string "AABBCDDDD" becomes `01001001101100111111`.

By using Huffman Coding, we represent "AABBCDDDD" more compactly, reducing its storage
size.

You might also like