How would you iteratively build a huffman code? All solutions I have seen are recursive. I was curious if an iterative solution was possible.
Asked
Active
Viewed 396 times
-4
-
1Any recursive algorithm can be converted to iterative, and vice-versa. – Jonathon Reinhart Feb 27 '18 at 01:58
-
Could you provide pseudocode for that, please? I've been trying to figure it out. – nrajanee Feb 27 '18 at 02:18
1 Answers
0
The Huffman algorithm is not recursive to begin with, and I've never seen a recursive implementation.
You might be talking about traversing the Huffman tree once the algorithm has built it. That can be recursive, but a traversal, as well as the tree itself, is not necessary. All you need for each symbol is how many bits are in the code for that symbol. With that you can sequentially generate a canonical code, i.e. assignment of actual bit values, to each symbol. The number of bits can be a direct result of the Huffman algorithm implementation, as in Moffat's implementation.

Mark Adler
- 101,978
- 13
- 118
- 158