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
11 def __lt__(self, …Keep your momentum going
Create a free account to unlock the full lesson, all 60 days, and track your progress as you go.
- Full access to all 60 daily lessons
- Track completion and build a daily streak
- Get interview-ready, one algorithm at a time
Join 2,900+ learners · 4.8/5 average rating