Huffman Coding Algorithm
Huffman Coding Algorithm
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:
1. **Calculate Frequencies:**
- A: 2
- B: 2
- C: 1
- D: 4
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`
By using Huffman Coding, we represent "AABBCDDDD" more compactly, reducing its storage
size.