~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
So I guess we should ask do you know how to create a binary tree and do you know how to traverse the nodes in that tree.
If not, it should all be documented on the web, since it's quite a popular resource for data retrieval.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I've got my tree printing correctly now, but I am now having a problem writing the code to traverse the tree and record the 1's and 0's to address each character in the leaf nodes. I know this should probably be a recursive function, but I cannot seem to figure it out. Any help with this would be greatly appreciated.
Thanks,
Nick
Ok assuming you have your tree set up correctly, you should have something like this:- http://www.cs.uidaho.edu/~karenv/cs213/cs213.useful.pages/huffman.html
So now all you want is to find out what the binary code is for each letter. Each letter will occupy a leaf ( by definition a leaf is a node with no children). Therefore all you need is a root-to-leaf path algorithm and you will have your binary encodings!
Luckily, the following link, problem 7. shows you what the algorithm is for a root-to-leaf traversal. Look near the bottom.
http://cslibrary.stanford.edu/110/BinaryTrees.html
Job done.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439