-1

Most people, when they implement study Huffman algorithm, prefer to store frequency array, not Huffman tree. But sometimes I would like to store Huffman tree. For example, It allows immediately decode data.

huffman tree

Unfortunately, I do not know, how to restore frequency array from Huffman tree. Furthermore, I do not know, it is possible or not.

Max
  • 1,803
  • 3
  • 25
  • 39
  • 1
    there is no need for restoring frequency array if you have tree. It is not possible get the exact frequency of symbols unless it is stored the tree nodes. To get approximate frequencies divide the parent frequency by 2 and assign it to child till you reach leaves. The frequency of root is 1 – Vikram Bhat Nov 27 '14 at 05:28

2 Answers2

2

It is not clear why you want to restore the frequency array. As you said, the tree is all you need to decode. (You don't even need to send the tree — you can just send the number of bits for each symbol, and generate a canonical Huffman code from that.)

You do not have enough information to recover the original frequency array. However you can easily construct a frequency array that would reproduce that tree. Make the longest codes frequency 1, codes with one bit less frequency 2, two bits less frequency 4, and so on.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
1

IMO when you have the frequency you can simply reconstruct the tree with the same function, for example greater or less.

Micromega
  • 12,486
  • 7
  • 35
  • 72