• Post Reply Bookmark Topic Watch Topic
  • New Topic

Huffman's Code Question  RSS feed

 
Scott Grant
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, after checking my other forum that I browse concerning MUD coding, I was just flamed for posting a question considered too difficult on a newbie coder board, so I'm posting this on on newbie Java and intermediate Java in hopes that the same doesn't happen to me!
I need to decompress this Huffman Code block:
1100111001000100011110110
Here's an array I put together, would this work? (I only included the first 5 letters of the code to save space). My only issue is I haven't included a null option, because 0 is a part of the code. I know.. total newbie issues.

[ June 02, 2003: Message edited by: Scott Grant ]
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only have the briefest knowledge of the Huffman code, but here are some problems with your approach:
you are using (what appear to be) binary strings as your array indices. eg, code[01] = "a";. Java does not support this. In fact, any number that starts with a 0 is interpreted as octal (base 8). This will quickly cause you problems because, for instance, code[011] will be interpreted as the index of the array "code" at position (octal)011, which is (decimal)9.
args[0] is the first argument on the command line after the name of the class that is being run. so, if you typed in:
java HuffmanTest 1100111001000100011110110
then args[0] is equal to "1100111001000100011110110". You then try to parse an integer out of that. That is a valid *number*, but it is not a valid integer in java, since the maximum positive number in java is 2147483647 (=2^31-1). I'm not sure what parseInt() does in this case, but it won't be what you're expecting.
So, you need to first work out your indexing of your array (perhaps by translating binary to decimal). Better yet, build a Map using the binary strings representing each character as keys and the characters as values. Then, you need to work out a parsing algorithm. I'm not sure how the Huffman code works, but if each character is represented by a 3 digit binary number (which wouldn't be enough to cover the alphabet, but just for grins...) then you could break up the input string given in args[0] in to sections of 3 characters each (the String.substring() method would be helpful here), and put those substrings in to the Map as keys to get the associated character.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!