Huffman compression
Encoding and decoding files with variable-length prefix codes.
Counting the input
Compression reads the input text and counts each character in a hash map. It turns those counts into leaf nodes, then repeatedly merges the two smallest trees from a priority queue until one code tree remains.
Assigning prefix codes
A recursive traversal assigns 0 on left branches and 1 on right branches. In the captured input, A appears six times and receives the one-bit code 0, while the less frequent characters receive longer codes.
Writing and reading bits
The compressor writes each code through BufferedBitWriter and the decoder walks the same tree with BufferedBitReader. The demonstration compresses a 14-byte single-line input to 5 encoded bytes and verifies that the decoded text matches. Because the implementation reads text with readLine, it does not preserve a terminal newline from a source file.