Huffman coding: Difference between revisions
Content deleted Content added
Line 1,339:
var trees treeHeap
for c, f := range charFreqs {
trees = append(trees,
}
heap.Init(&trees)
Line 1,348:
// put into new node and re-insert into queue
heap.Push(&trees,
}
return heap.Pop(&trees).(HuffmanTree)
Line 1,355:
func printCodes(tree HuffmanTree, prefix []int) {
switch i := tree.(type) {
case
// print out character, frequency, and code for this leaf (which is just the prefix)
fmt.Printf("%c\t%d\t%s\n", i.value, i.freq, string(prefix))
case
// traverse left
prefix = append(prefix, '0')
|