Ask Data Structure Expert

  • Huffman Codes

You will turn in one file: HuffmanCodes.java, which can encode and decode files using Huffman codes. The program has the following command-line interface:

$ javaHuffmanCodes --help
Usage: java HuffmanCodes OPTIONS IN OUT
Encodes and decodes files using Huffman's technique

-e, --encode encodes IN to OUT
-d, --decode decodes IN to OUT
--show-frequency show the frequencies of each byte
--show-codes show the codes for each byte
--show-binary show the encoded sequence in binary
-h, --help display this help and exit

• When the encode option is used, the program will read in the file specified by the IN operand and create the file specified by the OUT operand. Depending on the nature of the input file, the output file could be smaller than the original input.

• When the decode option is used, the program will read in the file specified by the IN operand and interpret it as the output from the encode operation. If the given file represents a valid encoding, the original encoding for the file will be restored in a new file, specified by the OUT operand.

  • Frequency Counting

The show-frequency option assists debugging. When it is set, the frequency table computed from the input file is output during the encoding process. For example, if the input file is the characters "mississippi" (without a newline), then the output would be:
$ echo -n "mississippi" > mi.txt
$ javaHuffmanCodes --encode mi.txt mi.encoded --show-frequency

FREQUENCY TABLE
'm': 1
'p': 2
'i': 4
's': 4

input: 11 bytes [88 bits]

output: 12 bytes [header: 71 bits; encoding: 21 bits]
output/input size: 109.0909%

The first column is each byte of the input file displayed in the format of a Java character literal (e.g., the newline character would be represented as '\n'). The second column is the count (in decimal) for the number of times that byte's value is present in the input file.
The table is ordered from least frequent to most frequent, with ties broken by each byte's value.

  • Generating Prefix-Free Codes

Like the show-frequency option, the show-codes option also assists debugging. It displays the variable-length Huffman codes for each value, using a sequence of "0" and "1" characters.
$ javaHuffmanCodes --encode mi.txt mi.encoded --show-codes
CODES
"0" -> 's'
"11" -> 'i'
"100" -> 'm'
"101" -> 'p'

input: 11 bytes [88 bits]

output: 12 bytes [header: 71 bits; encoding: 21 bits]
output/input size: 109.0909%

  • Encoding the Sequence

Although you should also be using the xxd command to help you view your file contents in binary, it is also helpful to have an explicit show-binary option to describe the sequence:
$ javaHuffmanCodes --encode mi.txt mi.encoded --show-binary

  • ENCODED SEQUENCE

100110011001110110111
input: 11 bytes [88 bits]
output: 12 bytes [header: 71 bits; encoding: 21 bits]
output/input size: 109.0909%

  • Decoding

The program will also decode sequences:
$ xxd -b mi.encoded
0000000: 00000000 00000000 00000000 00001011 01011100 11001011 ....\.
0000006: 01101101 11000010 11010011 00110011 00111011 01110000 m..3;p
$ javaHuffmanCodes --decode mi.encodedmi.restored --show-codes

  • CODES

"0" -> 's'
"11" -> 'i'
"100" -> 'm'
"101" -> 'p'
original size: 11
$ catmi.restored
mississippi

Data Structure, Computer Science

  • Category:- Data Structure
  • Reference No.:- M91331520
  • Price:- $35

Guranteed 24 Hours Delivery, In Price:- $35

Have any Question?


Related Questions in Data Structure

Data Communication Delivering Information anywhere

Topic: Data Communication Delivering Information anywhere. Write a 9-12 pages paper in which you: Present an overview of the origin and history of the concept. Describe the current use of and attitude toward the concept. ...

Problem regarding the management program

Problem: Looks like its just adding a save and load feature to the same file you sent me for python 3.5 Until now, you have had to leave your team management program running on your computer indefinitely since you did no ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As