Log In Sign Up
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 #

  1. Calculate the frequency of each character in the input.
  2. Create a leaf node for each character and add it to a priority queue.
  3. 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.
  4. The remaining node is the root of the Huffman tree.
  5. 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 …