• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

encoding Huffman

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello in the JavaRanch forum.
I have this problem bugging me for quite some time now.
I have been trying to generate the encoding part of Huffman for my Huffman class, I am pretty sure that I am on the right path. but how do I implement the result into my  so when I call the method in my main method it should generate the Huffman part?


I am aware that the Huffman tree takes the two lowest frequencies and makes them into a tree with the sum of their frequency as a parent.

So to give you the full information about my task:
I have a package called Code and in that package, I have 2 classes called Code.java & Huffman.java.
I am not allowed to change anything in the class "Code.java" and also the method in itself(so I can't make it nonstatic or something like that):
public static void generateHuffmanCode(TCode c) {} Huffman.java


So the problem I'm facing at the moment is in my method buildHuffmanTree. My for loop:

I understand that I can't use a non-static in a static method. but when I make the buildHuffmanTree non-static then I get a few errors somewhere else in my code.
So am I approaching it the wrong way? or is there a different way to make it work.
Also want to point out that I'm still very new to java programming, so I'm very sorry if I'm being too much of a rookie here.




Any help is much appreciated thanks in advance.


Huffman class:





Code class where no edit is allowed:




And my Main method:

 
Marshal
Posts: 72406
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two places where you could have an encodeHuffman() method:-
  • 1: In a utility class, where you pass the message as an argument, and the method would be static.
  • 2: In a Message class, where it encodes its own message, in which case it must be an instance method.
  • I would prefer No 2 as more object‑oriented, but No 1 is also acceptable.
    Don't call that method from the main() method. Use main() for starting your application and nothing else.
    Afraid you are approaching the problem the wrong way. You are trying to explain your code. Please start by explaining the algorithm you are using. Refine that and it will give you the code.
    I can see some good code, but you have made the compareTo() method much more complicated than it need be.
     
    Steven Madsen
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay algorithm of the Huffman concept.

    so we wanna create a binary tree with the two lowest frequencies and the add their values together to create the parent, and from there you can move your way up.
    so in my case the lowest are


    at this point i will get




    then I have two more nodes with the frequencies lower than 0.25 we create another binary tree with (B & E)

    then the result parent of this tree will then joining the result parent of the previous tree



    Lastly we join (C) and we get (1.00)



    The binary tree should look something like this:



    each symbol has weights and we assign bit'0' represents the left child and bit'1' represents the right child
    and that's how we get the code word:

     
    Campbell Ritchie
    Marshal
    Posts: 72406
    315
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Steven Madsen wrote:. . . create a binary tree

    That isn't ths first part of the algorithm. You want to start with, “find the least frequent element,” or similar. How you find the least frequent element is an implementation detail; that is a later stage of the process.
    As an alternative you can say to order the elements in order of frequency.

    with the two lowest frequencies and the add their values together to create the parent,  . . .

    At tis point things are beginning to fall in place. You remove the two lowest‑frequency nodes, put them together to make a tree and put that into your collection as a new Node.
    But how are you sorting the nodes? It is difficult to follow because you have so many methods with simmilar names. You are also not following object‑oriented principles; if you didn't make everythiing static, you could use fields for yout Map, etc.
    I can see an advantage which NetBeans would have over Eclipse. NetBeans has a different default for an unimplemented method from Eclipse, so when you try to run an unimplemented method you find out about it. Not like line 123 where an incorrect “default” implementation goes unnoticed Sorting that out might make your program work better.
    Why are you passing Strings with the encoding? Why don't you create the encoding from the structure of the tree?
     
    I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
    SKIP - a book about connecting industrious people with elderly land owners
    https://coderanch.com/t/skip-book
    reply
      Bookmark Topic Watch Topic
    • New Topic