Pure Programmer
Blue Matrix


Cluster Map

Project: Huffman Coding

[[Huffman_coding|Huffman Coding]] is a particular type of code commonly used for lossless data compression. By using shorter codes for more common symbols in a file and longer codes for less common symbols, we can save space overall.

Write a program that reads in a file and writes out a shorter compressed file using a Huffman Code. You will need to process the file to compute the frequency counts for each symbol in the file before you construct the Huffman tree used to encode the file. You will also need to write out the Huffman tree at the beginning of the file so that it can be used to decompress the file.

The program should take three arguments on the command line: name of input file, name of output file, and C/D for compress or decompress.

Even though this is an "optimal" compression technique, it doesn't always result in a smaller file. Why is this? What is the worst case?

Output
$ swiftc HuffmanCoding.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation) $ swiftc HuffmanCoding.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation) $ ls -al output/testUnicodeTest.* ls: output/testUnicodeTest.*: No such file or directory $ diff ../../data/text/UnicodeTest.utf8 output/testUnicodeTest.txt diff: output/testUnicodeTest.txt: No such file or directory $ swiftc HuffmanCoding.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation) $ swiftc HuffmanCoding.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation) $ ls -al output/test100.* ls: output/test100.*: No such file or directory $ diff ../../data/text/100.txt output/test100.txt diff: output/test100.txt: No such file or directory

Solution