Huffman, a graduate student at MIT in 1952, published this paper to outline what became known as Huffman coding, the most efficient binary code. Fano, his teacher, worked with Shannon to create Shannon-Fano coding, which used a top-down binary tree to create their own encoding. Huffman coding uses bottom-up encoding, which is provably optimal.
from collections import Counter
from heapq import heapify, heappop, heappush
from dataclasses import dataclass
from typing import Optional
@dataclass
class Node:
str]
ch: Optional[int
freq: 'Node'] = None
left: Optional['Node'] = None
right: Optional[
def __lt__(self, other):
return self.freq < other.freq
def get_huffman_tree(text):
if not text:
return
= Counter(text)
freq = [Node(k, v) for k, v in freq.items()]
pq
heapify(pq)while len(pq) > 1:
= heappop(pq), heappop(pq)
left, right = left.freq + right.freq
new_freq None, new_freq, left, right))
heappush(pq, Node(return pq[0]
print(get_huffman_tree('AADDDDDBBBE'))