What happens when both nodes of a tree have the same weight while constructing a Huffman code?

126 Views Asked by At

In figure 1.21, e and n2 have same weight. Why do we put n1 on the right and g on the left of the tree.

enter image description here

1

There are 1 best solutions below

0
On

As mentioned in the comments, the choice is completely arbitrary. Whichever choice you make, the resulting code will be optimal. Any particular algorithm will have a specific kind of behavior. One can speculate why they made this particular choice here — perhaps they had a tie-breaking rule in mind, perhaps they wanted the most aesthetically pleasing tree — but it's not very important.