Talk:Huffman coding: Difference between revisions
Content added Content deleted
(→IS This OK?: Change sort order, larger example) |
|||
Line 114: | Line 114: | ||
if tutor: print " PRODUCING:", lohi, '\n' |
if tutor: print " PRODUCING:", lohi, '\n' |
||
heappush(heap, lohi) |
heappush(heap, lohi) |
||
return sorted(heappop(heap)[1:], key=lambda x: |
return sorted(heappop(heap)[1:], key=lambda x: (len(x[-1]), x)) |
||
#readin = "B 25 C 2.5 D 12.5 A 5 \n" |
#readin = "B 25 C 2.5 D 12.5 A 5 \n" |
||
Line 149: | Line 149: | ||
C 12.5 110 |
C 12.5 110 |
||
D 12.5 111</pre> |
D 12.5 111</pre> |
||
With the following lines inserted into the program: |
|||
<lang python>astring = "this is an example of a huffman tree" |
|||
symbol2weights = dict((ch, astring.count(ch)) for ch in set(astring)) |
|||
</lang> |
|||
I get the following codes: |
|||
<pre>SYMBOL WEIGHT HUFFMAN CODE |
|||
7 111 |
|||
a 4 000 |
|||
e 4 001 |
|||
f 3 1101 |
|||
h 2 0100 |
|||
i 2 0101 |
|||
m 2 0111 |
|||
n 2 1000 |
|||
s 2 1010 |
|||
t 2 1011 |
|||
l 1 01100 |
|||
o 1 01101 |
|||
p 1 10010 |
|||
r 1 10011 |
|||
u 1 11000 |
|||
x 1 11001</pre> |