September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,85 +0,0 @@
|
|||
huffman_encoding_table = (counts) ->
|
||||
# counts is a hash where keys are characters and
|
||||
# values are frequencies;
|
||||
# return a hash where keys are codes and values
|
||||
# are characters
|
||||
|
||||
build_huffman_tree = ->
|
||||
# returns a Huffman tree. Each node has
|
||||
# cnt: total frequency of all chars in subtree
|
||||
# c: character to be encoded (leafs only)
|
||||
# children: children nodes (branches only)
|
||||
q = min_queue()
|
||||
for c, cnt of counts
|
||||
q.enqueue cnt,
|
||||
cnt: cnt
|
||||
c: c
|
||||
while q.size() >= 2
|
||||
a = q.dequeue()
|
||||
b = q.dequeue()
|
||||
cnt = a.cnt + b.cnt
|
||||
node =
|
||||
cnt: cnt
|
||||
children: [a, b]
|
||||
q.enqueue cnt, node
|
||||
root = q.dequeue()
|
||||
|
||||
root = build_huffman_tree()
|
||||
|
||||
codes = {}
|
||||
encode = (node, code) ->
|
||||
if node.c?
|
||||
codes[code] = node.c
|
||||
else
|
||||
encode node.children[0], code + "0"
|
||||
encode node.children[1], code + "1"
|
||||
|
||||
encode(root, "")
|
||||
codes
|
||||
|
||||
min_queue = ->
|
||||
# This is very non-optimized; you could use a binary heap for better
|
||||
# performance. Items with smaller priority get dequeued first.
|
||||
arr = []
|
||||
enqueue: (priority, data) ->
|
||||
i = 0
|
||||
while i < arr.length
|
||||
if priority < arr[i].priority
|
||||
break
|
||||
i += 1
|
||||
arr.splice i, 0,
|
||||
priority: priority
|
||||
data: data
|
||||
dequeue: ->
|
||||
arr.shift().data
|
||||
size: -> arr.length
|
||||
_internal: ->
|
||||
arr
|
||||
|
||||
freq_count = (s) ->
|
||||
cnts = {}
|
||||
for c in s
|
||||
cnts[c] ?= 0
|
||||
cnts[c] += 1
|
||||
cnts
|
||||
|
||||
rpad = (s, n) ->
|
||||
while s.length < n
|
||||
s += ' '
|
||||
s
|
||||
|
||||
examples = [
|
||||
"this is an example for huffman encoding"
|
||||
"abcd"
|
||||
"abbccccddddddddeeeeeeeee"
|
||||
]
|
||||
|
||||
for s in examples
|
||||
console.log "---- #{s}"
|
||||
counts = freq_count(s)
|
||||
huffman_table = huffman_encoding_table(counts)
|
||||
codes = (code for code of huffman_table).sort()
|
||||
for code in codes
|
||||
c = huffman_table[code]
|
||||
console.log "#{rpad(code, 5)}: #{c} (#{counts[c]})"
|
||||
console.log()
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
> coffee huffman.coffee
|
||||
---- this is an example for huffman encoding
|
||||
000 : n (4)
|
||||
0010 : s (2)
|
||||
0011 : m (2)
|
||||
0100 : o (2)
|
||||
01010: t (1)
|
||||
01011: x (1)
|
||||
01100: p (1)
|
||||
01101: l (1)
|
||||
01110: r (1)
|
||||
01111: u (1)
|
||||
10000: c (1)
|
||||
10001: d (1)
|
||||
1001 : i (3)
|
||||
101 : (6)
|
||||
1100 : a (3)
|
||||
1101 : e (3)
|
||||
1110 : f (3)
|
||||
11110: g (1)
|
||||
11111: h (2)
|
||||
|
||||
---- abcd
|
||||
00 : a (1)
|
||||
01 : b (1)
|
||||
10 : c (1)
|
||||
11 : d (1)
|
||||
|
||||
---- abbccccddddddddeeeeeeeee
|
||||
0 : e (9)
|
||||
1000 : a (1)
|
||||
1001 : b (2)
|
||||
101 : c (4)
|
||||
11 : d (8)
|
||||
Loading…
Add table
Add a link
Reference in a new issue