In a nutshell
Huffman coding compresses text by giving frequent characters short bit-codes and rare ones longer codes. Starting from one leaf per character weighted by its frequency, it repeatedly merges the two lowest-frequency trees under a new parent until a single tree remains. Reading each character's path from the root (left = 0, right = 1) gives its code. Because the rarest symbols are merged in first, they end up deepest with the longest codes — and the result is provably the optimal prefix code. With a heap to pull the two smallest each round, it runs in O(n log n).