문제

I am trying to create a Huffman code from some frequencies. I know how to do that but i have only one confusion that in which side (left or right ?) we will put which element.

I mean what i have in mind for Huffman tree is- (1) First we sort in decreasing order all the frequencies. (2) take the smallest two and merge them. ** But i don't understand which of the two frequencies will go in right and which will go in left** and i know that in right side we have '0' and right side we have '1'. but which frequency is to be kept in right or left taht i don't know. On what basis we do that ?

도움이 되었습니까?

해결책

You can have the child node in left or right thatis immaterial of the overall solution.we will see the length only not these values 0 & 1.

다른 팁

Since I don't exactly understand your question I'll give you a quick example of huffman encoding:

A: 0.5
B: 0.1
C: 0.2
D: 0.15
E: 0.05

Now in descending order of frequency:

A: 0.5
C: 0.2
D: 0.15
B: 0.1
E: 0.05

Now we combine the bottom two:

A: 0.5
C: 0.2
D: 0.15
B+E: 0.15

We continue to do this until we have a completely binary tree. In this example: A = 0, C = 10, D = 110, B = 1110, E = 11110 It doesn't matter if we choose to continue the tree with a 0 or 1 as long as your decompressor knows how to read it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top