I am writing a code that takes a word such as “abracadabra” and make it into a huffman tree. I understand the principles of the Huffman tree, but what I am snagged up on right now is how I am going to first implement abracadabra into it.
The approach that my teacher has told us to go is to have two separate queues/arrays. The first stores the amount for each letter, and the other stores the letters in order of the amount (highest to lowest) and when letters have the same value they are sorted alphabetically.
So it would result in: 5,2,2,1,1 and a,b,r,c,d
I am fairly sure he wants us to use a queue, but I do not know how to approach this simple part of the code..
Any help would be most grateful.
I can’t think why you’re being asked to write it in quite that form, but off the top of my head, I’d do this:
When you’re done, you have a queue in the right order for building your tree.
Feels like an abuse of a queue to me, but if that’s what you were asked to do…
Edit: Here’s how I’d probably actually write it, without queues etc.