Huffman coding: Difference between revisions

Content added Content deleted
No edit summary
Line 3,431: Line 3,431:
a$="this is an example for huffman encoding"
a$="this is an example for huffman encoding"
inventory freq
inventory queue freq
For i=1 to len(a$) {
For i=1 to len(a$) {
b$=mid$(a$,i,1)
b$=mid$(a$,i,1)
if exist(freq, b$) then Return freq, b$:=freq(b$)+1 : continue
if exist(freq, b$) then Return freq, b$:=freq(b$)+1 : continue
append freq, b$:=1
append freq, b$:=1
}
}
sort ascending freq
b=stack
b=stack
K=each(freq)
K=each(freq)
Line 3,479: Line 3,480:
local b=array(a,1)
local b=array(a,1)
if type$(b)="mArray" Else {
if type$(b)="mArray" Else {
' Print @(10); quote$(array$(a, 1));" "; a$,@(20),array(a)
Print @(10); quote$(array$(a, 1));" "; a$,@(20),array(a)
Append decode, a$ :=array$(a, 1)
Append decode, a$ :=array$(a, 1)
Append encode, array$(a, 1):=a$
Append encode, array$(a, 1):=a$
Line 3,493: Line 3,494:
{{out}}
{{out}}
<pre >
<pre >
"r" 00000 0,0256
"p" 00000 0,0256
"l" 00001 0,0256
"l" 00001 0,0256
"c" 00010 0,0256
"t" 00010 0,0256
"u" 00011 0,0256
"r" 00011 0,0256
"g" 00100 0,0256
"x" 00100 0,0256
"d" 00101 0,0256
"u" 00101 0,0256
"o" 0011 0,0513
"s" 0011 0,0513
"m" 0100 0,0513
"o" 0100 0,0513
"s" 0101 0,0513
"m" 0101 0,0513
"n" 011 0,1026
"n" 011 0,1026
"h" 1000 0,0513
"h" 1000 0,0513
"t" 10010 0,0256
"c" 10010 0,0256
"p" 100110 0,0256
"g" 100110 0,0256
"x" 100111 0,0256
"d" 100111 0,0256
"a" 1010 0,0769
"e" 1010 0,0769
"i" 1011 0,0769
"a" 1011 0,0769
"f" 1100 0,0769
"i" 1100 0,0769
"e" 1101 0,0769
"f" 1101 0,0769
" " 111 0,1538
" " 111 0,1538
0001010001100001111111000011111101101111110100010010110101000000000110101111101010000011111100000101110111010101101101111110100111001001001001111100011100110
1001010001011010111110110101111101001111111011001111010010010011000001110111111000011000001111000000111100110001001010011111110101100010001100101101101100100
this is an example for huffman encoding
this is an example for huffman encoding
157 bits Encoding/decoding worked
157 bits Encoding/decoding worked

</pre >
</pre >