Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Huffman coding from huffman tree...

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Thanked 0 Times in 0 Posts

    Default Huffman coding from huffman tree...

    hi there, need some help with my huffman coding assignment...
    actually, i had done tree huffman tree part... now left the part which stresses me for days... can't really come out with a working code for generating the huffman code.
    all i need is to extract the code, which is (for example 01010101) to represent the last character at the leaf of the huffman tree...
    the way i define the nodes are as follows...
    [code : java]
    class Node
    public String item;
    public Node leftChild;
    public Node rightChild;
    public Node getLeft(){return leftChild;}
    public Node getRight(){return rightChild;}
    public String getKey(){return item;}
    i have a working tree which i check by listing the traversal of it, and now, can anyone help me out with generating the huffman bit code for the huffman tree? the huffman tree starts with the root, have the value of the frequency in the internode and the leaf are representing the character of the text file i was to encode.
    any suggestion or ideas are deeply appreciated.
    regards, tetkun.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 665 Times in 569 Posts

    Default Re: Huffman coding from huffman tree...

    Before you sit down to code, you have to think about exactly what you need to do. Pretend you have a really dumb friend who knows nothing about Huffman trees. Write instructions that the friend could follow to get the job done. Remember, he doesn't know anything, so the instructions have to be as exact as possible.

    When you have those written out (not in code, just plain language), then you'll have an algorithm that you can worry about writing code for.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Thanked 0 Times in 0 Posts

    Default Re: Huffman coding from huffman tree...

    hi kevin,
    thanks for the reply.
    i understand what you mean... actually, i'm more to a vb user, but learning java now for my master assignments... i previously work as a math teacher for 7 years and continues master in IT because of knowledge and programming experience i have in IT and on top of it, i'm interested to learn the language of many not fond of in my country. therefore with the lack of fundamental and experience in using java, internet have always been the source of searching understandable code as a reference. for the problem above, i didn't copy the whole code from other since my lecture say that's it's ok, but i try my own after understanding my first assignment on constructing a binary tree which i use the coding from CHINMAY LOKESH (acknowledgment was given). based on understanding towards the huffman coding... this is what i did for my 2nd assignment.
    1. read the text file
    2. rearrange the characters using quicksort
    3. count the frequency from the first till the end
    4. sort the frequency with corresponding character using bubble sort (just to utilize what i learn from lectures)
    5. create the huffman tree (using recursion with binary tree experience)
    6. generate the coding from the tree... (the last part of the assignment)

    i don't know why, it took me a whole day of thinking but i cannot solve it using recursion. alas.. when i woke up this morning, something new came up... i don't know how it happened but it was a logic solution apart from recursion. i manage to solve it. this is the code. do comment a bit about it and i would sure like to know other method of doing so. i will try to understand the coding.
    public void PrintCode()
            Queue<Node> level  = new LinkedList<>();
            Queue<String> code = new LinkedList<>();
            code.add(" ");
                Node node = level.poll();
                String str = code.poll();
                if(node.leftChild != null)
                	code.add(str + "1");
                if(node.rightChild != null)
                	code.add(str + "0");
                	System.out.println(node.item + " -" + str);
    any response or amendment are most welcome. thanks. nice day.
    Last edited by pbrockway2; April 23rd, 2013 at 08:31 PM. Reason: code tags edited

Similar Threads

  1. Huffman Algorithm Help
    By Chewybakas in forum Algorithms & Recursion
    Replies: 6
    Last Post: February 20th, 2013, 10:40 AM
  2. Need help with Huffman coding, please?
    By knguye in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2012, 08:36 AM
  3. Huffman tree help!
    By rmendoza in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 3rd, 2012, 06:36 PM
  4. Help Huffman Coding>>
    By cool_97 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: July 23rd, 2011, 05:27 PM
  5. Huffman Tree
    By Aberforth in forum Java Theory & Questions
    Replies: 2
    Last Post: November 9th, 2010, 05:49 AM

Tags for this Thread