0

I am trying to create a Huffman tree and i am bit confused by reading several links on internet. Some add the greater(in terms of weight) child nodes in left or some on right.

So my question:

(1)Is it really a matter that where to add the nodes(in left or right) ?

(2) May i add node with greater weight in right or lower weight in Left ?

Thanks for the help.

Sss
  • 1,519
  • 8
  • 37
  • 67
  • 2
    You should try to follow standard link and articles like this http://en.wikipedia.org/wiki/Huffman_coding – Viral Shah Mar 12 '14 at 13:00

2 Answers2

1

It makes no difference, and it doesn't have to be consistent either. The currently accepted answer is wrong.

The reason is that:

  • The depth of each encoded character in the tree is not affected by this decision;
  • The Huffman decoding algorithm, gets the tree, so there is no meaning to "left" or "right"... at that moment it is only about distinctive symbol strings, consisting of symbols 0 or 1, which are implemented as bits, which uniquely define a character. There is no special meaning to the zero or to the one: the only importance is that they are distinct, and there is no ambiguity to which character a series defines.
trincot
  • 317,000
  • 35
  • 244
  • 286
0

As long as you're consistent, it makes no difference.

Either you put all the lower weights on left children and all the higher weights on right children, or vice-versa.

At the bottom line, left and right will only be variable names in your code, with no physical meaning whatsoever.

UPDATE:

If you are not consistent, then the resulting Huffman tree will not necessarily yield the best possible compression that can be achieved for the given input using the Huffman compression algorithm.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • thanks for the answer and if we add randomly the nodes in left or right (I mean some in left or some on right).Does it still don't matter ? Because the encoding will be different at last (i mean "0" will be replaced by "1" and vice versa) – Sss Mar 12 '14 at 13:00
  • If you add the nodes randomly, then the resulting Huffman tree will not necessarily yield the best possible compression (the best that can be achieved with the Huffman algorithm). – barak manos Mar 12 '14 at 13:02
  • so what do you suggest me to have best compression ? Greater weight in right and smaller in right Is it OK ?? – Sss Mar 12 '14 at 13:04
  • 1
    As I said in the answer: "Either you put **all** the lower weights on left children and **all** the higher weights on right children, or vice-versa". – barak manos Mar 12 '14 at 13:06
  • 1
    Wait, why does it matter whether you're consistent in putting the lower weight item left or right? Putting it left or right randomly doesn't influence the weight of the new node, or the height, so nothing really changes, right? – harold Mar 12 '14 at 15:05
  • @harold, to put it in simple words: you will end up with some rare symbols that have the same encoded-bit-sequence length as some frequent symbols. – barak manos Mar 12 '14 at 15:39
  • 1
    @barakmanos that's essentially what you said, yes. But how? Like I said, switching left and right changes neither the weight of the new node (and so shouldn't change the height of this node) nor the height of anything below. So it shouldn't change anything. – harold Mar 12 '14 at 15:41
  • @harold, what I meant is, if you choose to put the lower weight sometimes on the left child and sometimes on the right child, then the resulting Huffman tree wouldn't be the optimal (not to mention the fact that you will actually have to "work harder" on your code in order to accomplish that). – barak manos Mar 12 '14 at 15:50
  • 2
    @barakmanos I know what you mean, I just don't understand how it could be the case. – harold Mar 12 '14 at 15:56
  • 2
    This answer is wrong. The efficiency of compression is entirely unaffected by the arbitrary choices of one or zero on left or right. – Mark Adler Jul 30 '22 at 16:26