September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import Data.List
|
||||
import Control.Arrow
|
||||
import Data.Ord
|
||||
import Data.List (group, insertBy, sort, sortBy)
|
||||
import Control.Arrow ((&&&), second)
|
||||
import Data.Ord (comparing)
|
||||
|
||||
data HTree a
|
||||
= Leaf a
|
||||
|
|
@ -10,7 +10,7 @@ data HTree a
|
|||
|
||||
test :: String -> IO ()
|
||||
test =
|
||||
mapM_ (\(a, b) -> putStrLn ('\'' : a : "\' : " ++ b)) .
|
||||
mapM_ (\(a, b) -> putStrLn ('\'' : a : ("' : " ++ b))) .
|
||||
serialize . huffmanTree . freq
|
||||
|
||||
serialize :: HTree a -> [(a, String)]
|
||||
|
|
@ -23,7 +23,7 @@ huffmanTree
|
|||
=> [(w, a)] -> HTree a
|
||||
huffmanTree =
|
||||
snd .
|
||||
head . until (null . tail) hstep . sortBy (comparing fst) . (second Leaf <$>)
|
||||
head . until (null . tail) hstep . sortBy (comparing fst) . fmap (second Leaf)
|
||||
|
||||
hstep
|
||||
:: (Ord a, Num a)
|
||||
|
|
@ -34,4 +34,7 @@ hstep ((w1, t1):(w2, t2):wts) =
|
|||
freq
|
||||
:: Ord a
|
||||
=> [a] -> [(Int, a)]
|
||||
freq = ((length &&& head) <$>) . group . sort
|
||||
freq = fmap (length &&& head) . group . sort
|
||||
|
||||
main :: IO ()
|
||||
main = test "this is an example for huffman encoding"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
*Main> test "this is an example for huffman encoding"
|
||||
'p' : 00000
|
||||
'r' : 00001
|
||||
'g' : 00010
|
||||
|
|
|
|||
53
Task/Huffman-coding/Julia/huffman-coding.julia
Normal file
53
Task/Huffman-coding/Julia/huffman-coding.julia
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
abstract type HuffmanTree end
|
||||
|
||||
struct HuffmanLeaf <: HuffmanTree
|
||||
ch::Char
|
||||
freq::Int
|
||||
end
|
||||
|
||||
struct HuffmanNode <: HuffmanTree
|
||||
freq::Int
|
||||
left::HuffmanTree
|
||||
right::HuffmanTree
|
||||
end
|
||||
|
||||
function makefreqdict(s::String)
|
||||
d = Dict{Char, Int}()
|
||||
for c in s
|
||||
if !haskey(d, c)
|
||||
d[c] = 1
|
||||
else
|
||||
d[c] += 1
|
||||
end
|
||||
end
|
||||
d
|
||||
end
|
||||
|
||||
function huffmantree(ftable::Dict)
|
||||
trees::Vector{HuffmanTree} = [HuffmanLeaf(ch, fq) for (ch, fq) in ftable]
|
||||
while length(trees) > 1
|
||||
sort!(trees, lt = (x, y) -> x.freq < y.freq, rev = true)
|
||||
least = pop!(trees)
|
||||
nextleast = pop!(trees)
|
||||
push!(trees, HuffmanNode(least.freq + nextleast.freq, least, nextleast))
|
||||
end
|
||||
trees[1]
|
||||
end
|
||||
|
||||
printencoding(lf::HuffmanLeaf, code) = println(lf.ch == ' ' ? "space" : lf.ch, "\t", lf.freq, "\t", code)
|
||||
|
||||
function printencoding(nd::HuffmanNode, code)
|
||||
code *= '0'
|
||||
printencoding(nd.left, code)
|
||||
code = code[1:end-1]
|
||||
|
||||
code *= '1'
|
||||
printencoding(nd.right, code)
|
||||
code = code[1:end-1]
|
||||
end
|
||||
|
||||
const msg = "this is an example for huffman encoding"
|
||||
|
||||
println("Char\tFreq\tHuffman code")
|
||||
|
||||
printencoding(huffmantree(makefreqdict(msg)), "")
|
||||
Loading…
Add table
Add a link
Reference in a new issue