I have known that Huffman Tree is a kind of tree used for optimal prefix code, but is there any tree for optimal prefix code other than Huffman tree? If there are some that kind of trees, will their heights be the same? Many thanks!
2 Answers
Huffman trees are constructed recursively by taking the two currently lowest probability symbols and combining them.
If there are other symbols with the same low probability, then these symbols can be combined instead.
This means that the final tree is not uniquely defined and there are multiple optimal prefix codes with potentially different heights.
For example, consider the symbols and probabilities below:
A 1/3
B 1/3
C 1/6
D 1/6
Can be encoded as:
A 0
B 10
C 110
D 111
or
A 00
B 01
C 10
D 11
Both encodings have an expected number of bits per symbol equal to 2, but different heights.
However, all optimal prefix codes can be constructed by the Huffman algorithm for a suitable choice of ordering with respect to probability ties.

- 33,126
- 4
- 46
- 75
Within the constraints of the Huffman code problem, i.e. representation of each symbol by a prefix-unique sequence of bits, then there is exactly one optimal total number of bits that can be achieved, and the Huffman algorithm achieves that. There are other approaches that arrive at the same answer.
As Peter de Rivaz noted, in certain special cases the Huffman algorithm has more than one choice at some steps in which of two minimum probability set of codes to pick, which can result in different trees. So the tree height/depth you mention is not unique, but the total number of bits (sum of the bit lengths of each symbol weighted by its probability) is always the same.

- 101,978
- 13
- 118
- 158