Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
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