Huffman coding: Difference between revisions

Content added Content deleted
(Added Kotlin implementation)
Line 5,796: Line 5,796:
This code was adapted from Perl, Python and most of the other examples.
This code was adapted from Perl, Python and most of the other examples.
<lang zkl>fcn buildHuffman(text){ //-->(encode dictionary, decode dictionary)
<lang zkl>fcn buildHuffman(text){ //-->(encode dictionary, decode dictionary)
ft:=D();
ft:=Dictionary();
foreach c in (text){ ft[c]=ft.find(c,0)+1 } // leafs w/count
foreach c in (text){ ft[c]=ft.find(c,0)+1 } // leafs w/count


Line 5,815: Line 5,815:
if(T.isType(i))return(Void.Void,i,self.fcn); i });
if(T.isType(i))return(Void.Void,i,self.fcn); i });
encodeTable:=tree.toDictionary(); // symbol:Huffman code
encodeTable:=tree.toDictionary(); // symbol:Huffman code
decodeTable:=encodeTable.pump(D(),"reverse"); // code:symbol
decodeTable:=encodeTable.pump(Dictionary(),"reverse"); // code:symbol
return(encodeTable,decodeTable);
return(encodeTable,decodeTable);
}</lang>
}</lang>