Skip to content

add huffman encoding in go #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions contents/huffman_encoding/code/golang/huffman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package main

import (
"fmt"
"sort"
)

type node struct {
freq int
char rune
left *node
right *node
}

type codebook map[rune]string

func buildTree(message string) *node {
freqMap := make(map[rune]*node)
nodeList := make([]*node, 0)

for _, char := range message {
if _, ok := freqMap[char]; ok {
freqMap[char].freq++
} else {
newNode := new(node)
newNode.freq = 1
newNode.char = char
freqMap[char] = newNode
nodeList = append(nodeList, newNode)
}
}

for len(nodeList) > 1 {
sort.Slice(nodeList, func(i, j int) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically destroys the algorithmic complexity of Huffman-encoding. We could either use container/heap or roll our own version of it (a heap is pretty easy to implement and it could avoid having to cast back from interface).

return nodeList[i].freq < nodeList[j].freq
})

left := nodeList[0]
right := nodeList[1]
nodeList = nodeList[2:]
branch := new(node)
nodeList = append(nodeList, branch)
branch.left = left
branch.right = right
branch.freq = left.freq + right.freq
}

return nodeList[0]
}

func codebookRecurse(node *node, cb *codebook, code []rune) {
if node == nil {
return
}

if node.left == nil && node.right == nil {
(*cb)[node.char] = string(code)
}

// 0x30 == "0" and 0x31 == "1"
code = append(code, 0x30)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use '0' and '1'?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i forgot that '1' is a rune/byte

codebookRecurse(node.left, cb, code)
code = append(code[:len(code)-1], 0x31)
codebookRecurse(node.right, cb, code)
}

func encode(message string) (string, *node, codebook) {
ret := ""
root := buildTree(message)
cb := generateCodebook(root)
for _, char := range message {
ret += cb[char]
}

return ret, root, cb
}

func decode(message string, root *node) string {
cur := root
ret := ""

for _, char := range message {
if cur == nil {
return message
}

switch string(char) {
case "0":
if cur.left == nil {
ret += string(cur.char)
cur = root.left
} else {
cur = cur.left
}
case "1":
if cur.right == nil {
ret += string(cur.char)
cur = root.right
} else {
cur = cur.right
}
}
}

if cur.char != 0 {
ret += string(cur.char)
}

return ret
}

func generateCodebook(root *node) codebook {
cb := make(codebook)
codeArr := make([]rune, 0)
codebookRecurse(root, &cb, codeArr)
return cb
}

func main() {
enc, root, cb := encode("bibbity_bobbity")
fmt.Println("Codebook:")
for r, c := range cb {
fmt.Println(string(r), "->", c)
}
fmt.Println("\nEncoded:", enc)
fmt.Println("Decoded:", decode(enc, root))
}
2 changes: 2 additions & 0 deletions contents/huffman_encoding/huffman_encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Whether you use a stack or straight-up recursion also depends on the language, b
[import, lang:"javascript"](code/javascript/huffman.js)
{% sample lang="java" %}
[import, lang:"java"](code/java/huffman.java)
{% sample lang="go" %}
[import, lang:"go"](code/golang/huffman.go)
{% endmethod %}

<script>
Expand Down