Huffman coding: Difference between revisions

m
syntax highlighting fixup automation
m (Python: pretty print Huffman table)
m (syntax highlighting fixup automation)
Line 33:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">T Element
Int weight
[(Char, String)] symbols
Line 70:
print("Symbol\tWeight\tHuffman Code")
L(p) huff
print("#.\t#.\t#.".format(p[0], symb2freq[p[0]], p[1]))</langsyntaxhighlight>
 
{{out}}
Line 100:
 
huffman.ads:
<langsyntaxhighlight Adalang="ada">with Ada.Containers.Indefinite_Ordered_Maps;
with Ada.Containers.Ordered_Maps;
with Ada.Finalization;
Line 182:
-- free memory after finalization
overriding procedure Finalize (Object : in out Huffman_Tree);
end Huffman;</langsyntaxhighlight>
 
huffman.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
with Ada.Containers.Vectors;
Line 428:
end loop;
end Dump_Encoding;
end Huffman;</langsyntaxhighlight>
 
example main.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Huffman;
procedure Main is
Line 479:
(Char_Natural_Huffman_Tree.Decode (Tree => Tree, Code => Code));
end;
end Main;</langsyntaxhighlight>
 
{{out}}
Line 507:
{{incorrect|BBC BASIC|Huffman code can not contain another code as a prefix}}
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTSALIB"
SortUp% = FN_sortSAinit(0,0) : REM Ascending
SortDn% = FN_sortSAinit(1,0) : REM Descending
Line 559:
ENDIF
NEXT
END</langsyntaxhighlight>
{{out}}
<pre>
Line 584:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( "this is an example for huffman encoding":?S
& 0:?chars
& 0:?p
Line 639:
| !arg
)
& out$("decoded:" str$(decode$(str$!encoded)));</langsyntaxhighlight>
{{out}}
<pre>(L=
Line 669:
This code lacks a lot of needed checkings, especially for memory allocation.
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 842:
 
return 0;
}</langsyntaxhighlight>
===Alternative===
Using a simple heap-based priority queue. Heap is an array, while ndoe tree is done by binary links.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 967:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>' ': 000
Line 992:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 1,305:
}
}
}</langsyntaxhighlight>
[[File:CSharpHuffman.jpg]]
 
Line 1,312:
This code builds a tree to generate huffman codes, then prints the codes.
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <queue>
#include <map>
Line 1,426:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,451:
=={{header|Clojure}}==
(Updated to 1.6 & includes pretty-printing). Uses Java PriorityQueue
<langsyntaxhighlight lang="clojure">(require '[clojure.pprint :refer :all])
 
(defn probs [s]
Line 1,484:
(->> s huffman-encode (sort-by :weight >) print-table))
 
(display-huffman-encode "this is an example for huffman encoding")</langsyntaxhighlight>
 
{{out}}
Line 1,510:
===Alternate Version===
Uses c.d.priority-map. Creates a more shallow tree but appears to meet the requirements.
<langsyntaxhighlight lang="clojure">(require '[clojure.data.priority-map :refer [priority-map-keyfn-by]])
(require '[clojure.pprint :refer [print-table]])
 
Line 1,547:
(->> s huffman-encode (sort-by :weight >) print-table))
 
(display-huffman-encode "this is an example for huffman encoding")</langsyntaxhighlight>
{{out}}
<pre>| :sym | :weight | :code |
Line 1,572:
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
huffman_encoding_table = (counts) ->
# counts is a hash where keys are characters and
Line 1,658:
console.log "#{rpad(code, 5)}: #{c} (#{counts[c]})"
console.log()
</langsyntaxhighlight>
 
{{out}}
Line 1,705:
(For a more efficient implementation of a priority queue, see the [[Heapsort]] task.)
 
<langsyntaxhighlight lang="lisp">(defstruct huffman-node
(weight 0 :type number)
(element nil :type t)
Line 1,763:
(huffman-node-element node)
(huffman-node-weight node)
(huffman-node-encoding node))))</langsyntaxhighlight>
 
Example:
Line 1,791:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.typecons, std.container, std.array;
 
auto encode(alias eq, R)(Group!(eq, R) sf) /*pure nothrow @safe*/ {
Line 1,811:
foreach (const p; s.dup.sort().group.encode)
writefln("'%s' %s", p[]);
}</langsyntaxhighlight>
{{out}}
<pre>' ' 101
Line 1,835:
=={{header|Eiffel}}==
Adapted C# solution.
<langsyntaxhighlight lang="eiffel">
class HUFFMAN_NODE[T -> COMPARABLE]
inherit
Line 2,024:
end
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,050:
=={{header|Erlang}}==
The main part of the code used here is extracted from [https://gist.github.com/rmies/2828351 Michel Rijnders' GitHubGist]. See also [http://codingpraxis.com/erlang/2012/10/23/huffman-coding-in-erlang.html his blog], for a complete description of the original module.
<langsyntaxhighlight lang="erlang">-module(huffman).
-export([encode/1, decode/2, main/0]).
Line 2,112:
print_bits(<<Bit:1, Rest/bitstring>>) ->
io:format("~w", [Bit]),
print_bits(Rest).</langsyntaxhighlight>
{{out}}
<pre> : 111
Line 2,138:
=={{header|F_Sharp|F#}}==
{{Trans|OCaml}}
<langsyntaxhighlight lang="fsharp">type 'a HuffmanTree =
| Leaf of int * 'a
| Node of int * 'a HuffmanTree * 'a HuffmanTree
Line 2,172:
let tree = charFreqs |> Map.toList |> buildTree
printfn "Symbol\tWeight\tHuffman code";
printTree ([], tree)</langsyntaxhighlight>
{{out}}
<pre>Symbol Weight Huffman code
Line 2,196:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">
USING: kernel sequences combinators accessors assocs math hashtables math.order
sorting.slots classes formatting prettyprint ;
Line 2,329:
! u 1 { 1 0 0 1 1 }
! x 1 { 1 0 0 1 0 }
</syntaxhighlight>
</lang>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Node
{
Line 2,431:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,460:
=={{header|Fortran}}==
 
<langsyntaxhighlight lang="fortran">! output:
! d-> 00000, t-> 00001, h-> 0001, s-> 0010,
! c-> 00110, x-> 00111, m-> 0100, o-> 0101,
Line 2,608:
print *
end program
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">type block
freq as uinteger
chars as string
Line 2,696:
print "'"+codelist(i).char+"'", codelist(i).code
next i
</syntaxhighlight>
</lang>
{{out}}
<pre>' ' 111
Line 2,720:
=={{header|Go}}==
{{trans|Java}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,816:
fmt.Println("SYMBOL\tWEIGHT\tHUFFMAN CODE")
printCodes(tree, []byte{})
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,841:
</pre>
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,905:
fmt.Printf(" %c %d %s\n", c.sym, sym2freq[c.sym], c.code)
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Line 2,911:
Implemented and tested with Groovy 2.3.
 
<langsyntaxhighlight lang="groovy">
import groovy.transform.*
 
Line 2,962:
: decoded
}
</syntaxhighlight>
</lang>
Usage:
<langsyntaxhighlight lang="groovy">
def message = "this is an example for huffman encoding"
 
Line 2,975:
def decoded = decode(encoded, codeTable)
println decoded
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,004:
=={{header|Haskell}}==
Credits go to [http://www.haskell.org/haskellwiki/99_questions/46_to_50#Problem_50 huffman] where you'll also find a non-tree solution. Uses sorted list as a priority queue.
<langsyntaxhighlight lang="haskell">import Data.List (group, insertBy, sort, sortBy)
import Control.Arrow ((&&&), second)
import Data.Ord (comparing)
Line 3,043:
 
main :: IO ()
main = test "this is an example for huffman encoding"</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="haskell">'p' : 00000
'r' : 00001
'g' : 00010
Line 3,063:
'a' : 1100
'e' : 1101
' ' : 111</langsyntaxhighlight>
 
===Using <code>Set</code> as a priority queue===
(might be worth it for bigger alphabets):
<langsyntaxhighlight lang="haskell">import qualified Data.Set as S
 
htree :: (Ord t, Num t, Ord a) => S.Set (t, HTree a) -> HTree a
Line 3,078:
huffmanTree :: (Ord w, Num w, Ord a) => [(w, a)] -> HTree a
huffmanTree = htree . S.fromList . map (second Leaf)</langsyntaxhighlight>
 
===A non-tree version===
This produces the output required without building the Huffman tree at all,
by building all the trace strings directly while reducing the histogram:
<langsyntaxhighlight lang="haskell">import Data.List (sortBy, insertBy, sort, group)
import Control.Arrow (second, (&&&))
import Data.Ord (comparing)
Line 3,099:
 
main = do
test "this is an example for huffman encoding"</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">record huffnode(l,r,n,c) # internal and leaf nodes
record huffcode(c,n,b,i) # encoding table char, freq, bitstring, bits (int)
 
Line 3,161:
}
return T
end</langsyntaxhighlight>
 
{{out}}
Line 3,190:
and implements the algorithm given in the problem description.
The program produces Huffman codes based on each line of input.
<langsyntaxhighlight Uniconlang="unicon">import Collections
 
procedure main(A)
Line 3,219:
else codeMap[node[1]] := prefix
return codeMap
end</langsyntaxhighlight>
 
A sample run:
Line 3,265:
'''Solution''' (drawn from [[J:Essays/Huffman_Coding|the J wiki]]):
 
<langsyntaxhighlight lang="j">hc=: 4 : 0
if. 1=#x do. y
else. ((i{x),+/j{x) hc (i{y),<j{y [ i=. (i.#x) -. j=. 2{./:x end.
Line 3,278:
t=. 0 {:: x hc w NB. minimal weight binary tree
((< S: 0 t) i. w) { <@(1&=)@; S: 1 {:: t
)</langsyntaxhighlight>
 
'''Example''':<langsyntaxhighlight lang="j"> ;"1":L:0(#/.~ (],.(<' '),.hcodes) ,&.>@~.)'this is an example for huffman encoding'
t 0 1 0 1 0
h 1 1 1 1 1
Line 3,299:
c 1 0 0 0 0
d 1 0 0 0 1
g 1 1 1 1 0</langsyntaxhighlight>
 
=={{header|Java}}==
This implementation creates an actual tree structure, and then traverses the tree to recover the code.
<langsyntaxhighlight lang="java">import java.util.*;
 
abstract class HuffmanTree implements Comparable<HuffmanTree> {
Line 3,397:
printCodes(tree, new StringBuffer());
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,429:
 
The Huffman encoder
<langsyntaxhighlight lang="javascript">function HuffmanEncoding(str) {
this.str = str;
 
Line 3,491:
}
return decoded;
}</langsyntaxhighlight>
 
And, using the Huffman encoder
<langsyntaxhighlight lang="javascript">var s = "this is an example for huffman encoding";
print(s);
 
Line 3,506:
print(t);
 
print("is decoded string same as original? " + (s==t));</langsyntaxhighlight>
 
{{out}}
Line 3,534:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
abstract type HuffmanTree end
 
Line 3,588:
 
printencoding(huffmantree(makefreqdict(msg)), "")
</langsyntaxhighlight>{{output}}<pre>
Char Freq Huffman code
p 1 00000
Line 3,614:
{{trans|Java}}
This implementation creates an actual tree structure, and then traverses the tree to recover the code.
<langsyntaxhighlight lang="kotlin">import java.util.*
 
abstract class HuffmanTree(var freq: Int) : Comparable<HuffmanTree> {
Line 3,667:
println("SYMBOL\tWEIGHT\tHUFFMAN CODE")
printCodes(tree, StringBuffer())
}</langsyntaxhighlight>
 
{{out}}
Line 3,695:
construct the Huffman tree, and finally fold the tree into the codes
while outputting them.
<langsyntaxhighlight lang="lua">local build_freqtable = function (data)
local freq = { }
 
Line 3,769:
return huffcode "this is an example for huffman encoding"
 
</syntaxhighlight>
</lang>
<pre>
frequency word huffman code
Line 3,794:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Huffman {
comp=lambda (a, b) ->{
Line 3,879:
}
Huffman
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,911:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<langsyntaxhighlight lang="mathematica">huffman[s_String] := huffman[Characters[s]];
huffman[l_List] := Module[{merge, structure, rules},
Line 3,923:
rules = (# -> Flatten[Position[structure, #] - 1]) & /@ DeleteDuplicates[l];
 
{Flatten[l /. rules], rules}];</langsyntaxhighlight>
 
=={{header|Nim}}==
 
<langsyntaxhighlight lang="nim">import tables, sequtils
 
type
Line 4,022:
 
for (key, value) in sortedByIt(toSeq(huffCodes.pairs), it[1]):
echo &"'{key}' → {value}"</langsyntaxhighlight>
 
{{out}}
Line 4,047:
=={{header|Oberon-2}}==
{{works with|oo2c}}
<langsyntaxhighlight lang="oberon2">
MODULE HuffmanEncoding;
IMPORT
Line 4,139:
 
END HuffmanEncoding.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,167:
 
This is not purely Objective-C. It uses Apple's Core Foundation library for its binary heap, which admittedly is very ugly. Thus, this only builds on Mac OS X, not GNUstep.
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
 
Line 4,316:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 4,347:
We use a Set (which is automatically sorted) as a priority queue.
{{works with|OCaml|4.02+}}
<langsyntaxhighlight lang="ocaml">type 'a huffman_tree =
| Leaf of 'a
| Node of 'a huffman_tree * 'a huffman_tree
Line 4,396:
let tree = build_tree charFreqs in
print_string "Symbol\tHuffman code\n";
print_tree [] tree</langsyntaxhighlight>
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(define phrase "this is an example for huffman encoding")
 
Line 4,438:
(return (reverse prefix))))))))
(map car table)))
</syntaxhighlight>
</lang>
{{Out}}
<langsyntaxhighlight lang="scheme">
(print "weights: ---------------------------")
(for-each (lambda (ch)
Line 4,451:
(reverse (map car table))
(reverse codes))
</syntaxhighlight>
</lang>
<pre>
weights: ---------------------------
Line 4,496:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use 5.10.0;
use strict;
 
Line 4,552:
print "$enc\n";
 
print decode($enc, $rev_h), "\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 4,580:
=={{header|Phix}}==
{{trans|Lua}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">store_nodes</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">nodes</span><span style="color: #0000FF;">)</span>
Line 4,677:
<span style="color: #000000;">main</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"this is an example for huffman encoding"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 4,707:
{{trans|Python}} (not exactly)
 
<langsyntaxhighlight lang="php"><?php
function encode($symb2freq) {
$heap = new SplPriorityQueue;
Line 4,734:
foreach ($huff as $sym => $code)
echo "$sym\t$symb2freq[$sym]\t$code\n";
?></langsyntaxhighlight>
 
{{out}}
Line 4,762:
=={{header|Picat}}==
{{trans|Prolog}}
<langsyntaxhighlight Picatlang="picat">go =>
huffman("this is an example for huffman encoding").
 
Line 4,810:
N1 = N + 1.
run(V, [Other|RRest], [1,V], [Other|RRest]) :-
different_terms(V, Other).</langsyntaxhighlight>
 
{{out}}
Line 4,837:
Using a cons cells (freq . char) for leaves, and two cells (freq left . right)
for nodes.
<langsyntaxhighlight PicoLisplang="picolisp">(de prio (Idx)
(while (cadr Idx) (setq Idx @))
(car Idx) )
Line 4,854:
(prinl (cdr P) " " L)
(recurse (cadr P) (cons 0 L))
(recurse (cddr P) (cons 1 L)) ) ) )</langsyntaxhighlight>
{{out}}
<pre>n 000
Line 4,877:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source attributes xref or(!);
hencode: Proc Options(main);
/*--------------------------------------------------------------------
Line 5,134:
End;
 
End;</langsyntaxhighlight>
{{out}}
<pre>The list of nodes:
Line 5,204:
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-HuffmanEncodingTable ( $String )
{
Line 5,266:
}
$EncodedString
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 5,297:
=={{header|Prolog}}==
Works with SWI-Prolog
<langsyntaxhighlight Prologlang="prolog">huffman :-
L = 'this is an example for huffman encoding',
atom_chars(L, LA),
Line 5,343:
N1 is N + 1.
run(V, [Other|RRest], [1,V], [Other|RRest]):-
dif(V, Other).</langsyntaxhighlight>
{{out}}
<pre> ?- huffman.
Line 5,370:
=={{header|PureBasic}}==
{{works with|PureBasic|4.50}}
<syntaxhighlight lang="purebasic">
<lang PureBasic>
OpenConsole()
Line 5,454:
CloseConsole()
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,482:
The output is sorted first on length of the code, then on the symbols.
 
<langsyntaxhighlight lang="python">from heapq import heappush, heappop, heapify
from collections import defaultdict
 
Line 5,508:
print "Symbol\tWeight\tHuffman Code"
for p in huff:
print "%s\t%s\t%s" % (p[0], symb2freq[p[0]], p[1])</langsyntaxhighlight>
 
{{out}}
Line 5,538:
This implementation creates an explicit tree structure, which is used during decoding. We also make use of a "pseudo end of file" symbol and padding bits to facilitate reading and writing encoded data to from/to a file.
 
<langsyntaxhighlight lang="python">"""Huffman encoding and decoding. Requires Python >= 3.7."""
from __future__ import annotations
 
Line 5,724:
if __name__ == "__main__":
main()
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,762:
The word <code>huffmanlist</code> will turn the Huffman tree into a nest of nests, each containing an ascii character code and a nest containing a Huffman code. The nests are sorted by ascii character code to facilitate binary splitting.
 
<langsyntaxhighlight Quackerylang="quackery"> [ 2dup peek 1+ unrot poke ] is itemincr ( [ n --> [ )
[ [ 0 128 of ] constant
Line 5,829:
say " Huffman codes sorted by code length." cr
codesort echohuff
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,876:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
Line 5,981:
(define decode (make-decoder tree))
;; Here's what the decoded message looks like:
(decode encoded)</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 5,991:
 
{{works with|rakudo|2015-12-17}}
<syntaxhighlight lang="raku" perl6line>sub huffman (%frequencies) {
my @queue = %frequencies.map({ [.value, .key] }).sort;
while @queue > 1 {
Line 6,003:
multi walk ($node, $prefix) { take $node => $prefix; }
multi walk ([$node1, $node2], $prefix) { walk $node1, $prefix ~ '0';
walk $node2, $prefix ~ '1'; }</langsyntaxhighlight>
 
===Without building a tree===
Line 6,010:
 
{{works with|rakudo|2015-12-17}}
<syntaxhighlight lang="raku" perl6line>sub huffman (%frequencies) {
my @queue = %frequencies.map: { .value => (hash .key => '') };
while @queue > 1 {
Line 6,040:
my $decoded = $encoded .subst: /@codes/, { %decode-key{$_} }, :g;
 
.say for $original, $encoded, $decoded;</langsyntaxhighlight>
 
{{out}}
Line 6,068:
 
=={{header|Red}}==
<langsyntaxhighlight Redlang="red">Red [file: %huffy.red]
 
;; message to encode:
Line 6,159:
]
]
</syntaxhighlight>
</lang>
{{out}}
<pre> = 111
Line 6,189:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 27.12.2013 Walter Pachl
* 29.12.2013 -"- changed for test of s=xrange('00'x,'ff'x)
Line 6,446:
Say ' 'c '-->' code.c
End
Return</langsyntaxhighlight>
{{out}}
<pre>We encode this string:
Line 6,517:
=={{header|Ruby}}==
Uses a {{libheader|RubyGems}} package [http://ruby.brian-amberg.de/priority-queue/ PriorityQueue]
<langsyntaxhighlight lang="ruby">require 'priority_queue'
 
def huffman_encoding(str)
Line 6,570:
enc = encode(str, encoding)
dec = decode(enc, encoding)
puts "success!" if str == dec</langsyntaxhighlight>
<pre>[" ", "111"]
["a", "1011"]
Line 6,597:
Adapted C++ solution.
 
<langsyntaxhighlight lang="rust">
use std::collections::BTreeMap;
use std::collections::binary_heap::BinaryHeap;
Line 6,684:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 6,711:
=={{header|Scala}}==
{{works with|scala|2.8}}
<langsyntaxhighlight lang="scala">object Huffman {
import scala.collection.mutable.{Map, PriorityQueue}
Line 6,759:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 6,786:
==={{header|Scala (Alternate version)}}===
{{works with|scala|2.11.7}}
<langsyntaxhighlight lang="scala">
// this version uses immutable data only, recursive functions and pattern matching
object Huffman {
Line 6,829:
frequencies.foreach(x => println(x._1.value + "\t" + x._2 + s"/${text.length}" + s"\t${encodeChar(huffmanTree, x._1.value)}"))
}
}</langsyntaxhighlight>
<pre>
Char Weight Code
Line 6,855:
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">(define (char-freq port table)
(if
(eof-object? (peek-char port))
Line 6,908:
(define freq-table (char-freq (open-input-string input) '()))
(define tree (huffman-tree (nodeify freq-table)))
(list-encodings tree (map car freq-table))</langsyntaxhighlight>
 
{{out}}
Line 6,934:
 
=={{header|SETL}}==
<langsyntaxhighlight SETLlang="setl">var forest := {}, encTab := {};
 
plaintext := 'this is an example for huffman encoding';
Line 6,971:
forest less:= [f, n];
return [f, n];
end proc;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func walk(n, s, h) {
if (n.contains(:a)) {
h{n{:a}} = s
Line 7,027:
 
say enc
say decode(enc, tree)</langsyntaxhighlight>
{{out}}
<pre>n: 000
Line 7,053:
=={{header|Standard ML}}==
{{works with|SML/NJ}}
<langsyntaxhighlight lang="sml">datatype 'a huffman_tree =
Leaf of 'a
| Node of 'a huffman_tree * 'a huffman_tree
Line 7,109:
print "SYMBOL\tHUFFMAN CODE\n";
printCodes ([], tree)
end</langsyntaxhighlight>
 
=={{header|Swift}}==
Rather than a priority queue of subtrees, we use the strategy of two sorted lists, one for leaves and one for nodes, and "merge" them as we iterate through them, taking advantage of the fact that any new nodes we create are bigger than any previously created nodes, so go at the end of the nodes list.
{{works with|Swift|2+}}
<langsyntaxhighlight lang="swift">enum HuffmanTree<T> {
case Leaf(T)
indirect case Node(HuffmanTree<T>, HuffmanTree<T>)
Line 7,179:
let tree = buildTree(charFreqs)
print("Symbol\tHuffman code")
tree.printCodes("")</langsyntaxhighlight>
{{out}}
<pre>
Line 7,206:
=={{header|Tcl}}==
{{tcllib|struct::prioqueue}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require struct::prioqueue
 
Line 7,258:
 
puts $str
puts [string map $encoding $str]</langsyntaxhighlight>
{{out}}
<pre style='width:full; overflow:scroll'>n 4 000
Line 7,284:
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
<langsyntaxhighlight lang="bash">
#!/bin/bash
 
Line 7,326:
awk -F: '{printf "%c\t%d\t%s\n", $2, $3, $1}' |
sort -k 2,2nr -k 3,3n
</syntaxhighlight>
</lang>
 
=={{header|Ursala}}==
following the algorithm given above
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
#import flo
Line 7,343:
#cast %csAL
 
table = code_table 'this is an example for huffman encoding'</langsyntaxhighlight>
a quick walk through the code starting from the bottom:
* <code>*K2 ^/~&h float+ length</code> compute character frequencies by partitioning the input list of characters by equality, and transforming each equivalence class to a pair containing its member and its cardinality represented as a floating point number
Line 7,381:
{{libheader|Wren-queue}}
Note that the results differ from Java because the PriorityQueue implementation is not the same.
<langsyntaxhighlight lang="ecmascript">import "/queue" for PriorityQueue
 
class HuffmanTree {
Line 7,452:
var tree = buildTree.call(freqs)
System.print("SYMBOL\tWEIGHT\tHUFFMAN CODE")
printCodes.call(tree, "")</langsyntaxhighlight>
 
{{out}}
Line 7,480:
=={{header|zkl}}==
This code was adapted from Perl, Python and most of the other examples.
<langsyntaxhighlight lang="zkl">fcn buildHuffman(text){ //-->(encode dictionary, decode dictionary)
ft:=Dictionary();
foreach c in (text){ ft[c]=ft.find(c,0)+1 } // leafs w/count
Line 7,502:
decodeTable:=encodeTable.pump(Dictionary(),"reverse"); // code:symbol
return(encodeTable,decodeTable);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn encode(text,table){ text.pump(String,table.get) }
fcn decode(bits,table){ // this is a horrible decoder, for testing only
w:=bits.walker(); sink:=Sink(String);
Line 7,510:
}}catch(TheEnd){}
sink.close();
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">text:="this is an example for huffman encoding";
encodeTable,decodeTable := buildHuffman(text);
encodeTable.pump(Console.println,fcn(kv){"%s : %s".fmt(kv.xplode())});
Line 7,520:
println(e);
 
0'|Bits decoded to: "%s"|.fmt(decode(e,decodeTable)).println();</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits