#!/usr/bin/perl -w use strict; use lib qw(./); use Huffman qw(:All); use Data::Dumper; my $string = 'In computer science, Huffman coding is an entropy encoding algorithm used for data compression that finds the optimal system of encoding strings based on the relative frequency of each character. It was developed by David A. Huffman as a PhD student at MIT in 52, and published in A Method for the Construction of Minimum-Redundancy Codes. Huffman coding uses a specific method for choosing the representations for each symbol, resulting in a prefix-free code (that is, no bit string of any symbol is a prefix of the bit string of any other symbol) that expresses the most common characters in the shortest way possible. It has been proven that Huffman coding is the most effective compression method of this type: no other mapping of source symbols to strings of bits will produce a smaller output when the actual symbol frequencies agree with those used to create the code. For a set of symbols whose cardinality is a power of two and a uniform probability distribution, Huffman coding is equivalent to simple binary block encoding.'; # build tree my $tree = build_tree($string); # build encoding look up table my @paths = build_encodings($string); # test encoding subroutine - to encode, simply call this single function my $encoded_str = encode_str($string); print "Encoded:\n$encoded_str\n"; print "..any key to decode...\n"; my $x = ; # get huffman tree -> a tree structure is required by decode_str, unlike encode_str which just needs the init string my $decoded = decode_str($encoded_str,@paths); print "Decoded:\n".$decoded."\n"; print "Stats: \n"; print "Original chars: ".length($string)."\n"; print "Original size (in bits): ".(8*length($string))."\n"; print "Encoded size (in bits): ".length($encoded_str)."\n"; print "Size reduction: %".((1-(length($encoded_str)/(8*length($string))))*100)."\n"; # shows contents of tree #print Dumper($tree); # shows structure and contents of huffman tree # # Testing auxilary functions used by encode_str in Huffman.pm # # test frequency count subroutine my %char_frequency = freq($string); #print Dumper(%char_frequency); # shows structure and contents of char_frequency hash # test routine that builds encodings #print Dumper(@paths); # shows structure and contents of encodings that were derived from the huffman tree