Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Implementing Huffman class into another class

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello in the Javaranch.
I almost have a working Huffman algorithm but I'm having trouble figuring our how to implement it / or make it work together with the Code class
when you look at the generateHuffmanCode method.


My Huffman class




and my Code class

 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need one of the following:-
  • 1: An instance of the Huffman class: new HuffmanEncoder("CodeRanch Rocks!");
  • 2: Make the Huffman class a utility class: HuffmanEncoder.encode("CodeRanch Rocks!");
  • The two are mutually exclusive because you never instantiate a utility class and an ordinary concrete class often doesn't declare any static methods. You design test and produce your Huffman class and get it working correctly. Then you use it as shwon above. You appear to have taken the approach of No 2, so you shouldn't instantiate your Huffman class, so it should have a private constructor for reasons explained here in the JLS (=Java┬« Language specification).
    Why are you  using an Integer for frequency (line 14) when you previously defined it as a double?
    Why are you passing a Map and a Node to the encode() method? That doesn't look correct. Surely you should have worked out the frequencies beforehand and you should be creating the node in that method. Consider a private method which will receive a partially encoded text and such nodes. If you make the Node class a top‑level class, make sure to keep it hidden inside that package and don't let any code using it access it. So use the Huffman class only from outside that package. If anybody passes null, don't wait for something to go wrong: throw an exception immeidately you detect the null.
     
    Steven Madsen
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay yes that was my bad. so will make the Integer freq --> double freq.
    In my encode method I'm trying to Traverse the Huffman tree and store Huffman codes in map

    and then in my decode method to Traverse the Huffman Tree and decode the encoded String.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic