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<>();
level.add(root);
code.add(" ");
while(!level.isEmpty()){
Node node = level.poll();
String str = code.poll();
if(node.leftChild != null)
{
level.add(node.leftChild);
code.add(str + "1");
}
if(node.rightChild != null)
{
level.add(node.rightChild);
code.add(str + "0");
}
else
{
System.out.println(node.item + " -" + str);
}
}
}
any response or amendment are most welcome. thanks. nice day.