A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
28
Task/Huffman-coding/PHP/huffman-coding.php
Normal file
28
Task/Huffman-coding/PHP/huffman-coding.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
function encode($symb2freq) {
|
||||
$heap = new SplPriorityQueue;
|
||||
$heap->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
|
||||
foreach ($symb2freq as $sym => $wt)
|
||||
$heap->insert(array($sym => ''), -$wt);
|
||||
|
||||
while ($heap->count() > 1) {
|
||||
$lo = $heap->extract();
|
||||
$hi = $heap->extract();
|
||||
foreach ($lo['data'] as &$x)
|
||||
$x = '0'.$x;
|
||||
foreach ($hi['data'] as &$x)
|
||||
$x = '1'.$x;
|
||||
$heap->insert($lo['data'] + $hi['data'],
|
||||
$lo['priority'] + $hi['priority']);
|
||||
}
|
||||
$result = $heap->extract();
|
||||
return $result['data'];
|
||||
}
|
||||
|
||||
$txt = 'this is an example for huffman encoding';
|
||||
$symb2freq = array_count_values(str_split($txt));
|
||||
$huff = encode($symb2freq);
|
||||
echo "Symbol\tWeight\tHuffman Code\n";
|
||||
foreach ($huff as $sym => $code)
|
||||
echo "$sym\t$symb2freq[$sym]\t$code\n";
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue