Day 42
Day 42: Huffman Coding
42/60 Days
Huffman Coding #
Welcome to Day 42 of our 60 Days of Coding Algorithm Challenge! Today, we’ll explore Huffman Coding, a greedy algorithm used for lossless data compression.
What is Huffman Coding? #
Huffman Coding is an efficient method for data compression. It assigns variable-length codes to characters based on their frequencies. More frequent characters get shorter codes, while less frequent characters get longer codes.
How Huffman Coding Works #
- Calculate the frequency of each character in the input.
- Create a leaf node for each character and add it to a priority queue.
- While there’s more than one node in the queue:
- Remove the two nodes with the lowest frequency.
- Create a new internal node with these two nodes as children.
- Add the new node to the queue.
- The remaining node is the root of the Huffman tree.
- Traverse the tree to assign codes to characters.
Implementation #
Let’s implement Huffman Coding in Python:
1import heapq
2from collections import defaultdict
3
4class Node:
5 def __init__(self, char, freq):
6 self.char = char
7 self.freq = freq
8 self.left = None
9 self.right = None
10 …
Continue Reading
Sign up or log in to access the full lesson and all 60 days of algorithm content.