Browse Source

Add codes table generation, add tests

master
arnaucube 4 years ago
parent
commit
d9d8023372
5 changed files with 153 additions and 0 deletions
  1. +5
    -0
      README.md
  2. +8
    -0
      go.mod
  3. +12
    -0
      go.sum
  4. +40
    -0
      huffman.go
  5. +88
    -0
      huffman_test.go

+ 5
- 0
README.md

@ -0,0 +1,5 @@
# huffman-coding
https://en.wikipedia.org/wiki/Huffman_coding

+ 8
- 0
go.mod

@ -0,0 +1,8 @@
module huffman-coding
go 1.13
require (
github.com/stretchr/testify v1.5.1
gopkg.in/go-playground/assert.v1 v1.2.1
)

+ 12
- 0
go.sum

@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 40
- 0
huffman.go

@ -90,3 +90,43 @@ func buildTree(nodes []*Node) *Node {
}
return nodes[0]
}
func descendTable(n *Node, path string, table map[byte]string) map[byte]string {
if n.typ == MID {
table = descendTable(n.l, path+"0", table)
table = descendTable(n.r, path+"1", table)
} else if n.typ == LEAF {
table[n.leaf] = path
}
return table
}
func generateTable(n *Node) map[byte]string {
table := make(map[byte]string)
table = descendTable(n, "", table)
return table
}
func Huffman(b []byte) error {
// compute frequencies
values := make(map[byte]int64)
for _, v := range b {
values[v]++
}
leafs := leafsFromValues(values)
// sort frequencies
sortedLeafs := sortNodes(leafs)
// build binary tree
n := buildTree(sortedLeafs)
fmt.Println(n)
// get the table (binary code for each value)
table := generateTable(n)
fmt.Println(table)
// WIP
return nil
}

+ 88
- 0
huffman_test.go

@ -0,0 +1,88 @@
package huffman
import (
"fmt"
"testing"
"gopkg.in/go-playground/assert.v1"
)
var debug = true
func TestBuildTree0(t *testing.T) {
values := make(map[byte]int64)
values[65] = 3
values[66] = 5
values[67] = 6
values[68] = 4
values[69] = 2
leafs := leafsFromValues(values)
sortedLeafs := sortNodes(leafs)
n := buildTree(sortedLeafs)
/*
Expected result (both valid):
20
/ \
9 \
/ \ \
/ 5 11
/ / \ / \
4 2 3 5 6
20
/ \
/ 11
/ / \
9 5 \
/ \ / \ \
4 5 2 3 6
The second tree is what is genereated in this test
*/
assert.Equal(t, int64(20), n.key)
assert.Equal(t, int64(9), n.l.key)
assert.Equal(t, int64(4), n.l.l.key)
assert.Equal(t, int64(5), n.l.r.key)
assert.Equal(t, int64(11), n.r.key)
assert.Equal(t, int64(5), n.r.l.key)
assert.Equal(t, int64(2), n.r.l.l.key)
assert.Equal(t, int64(3), n.r.l.r.key)
assert.Equal(t, int64(6), n.r.r.key)
}
func TestBuildTree1(t *testing.T) {
values := make(map[byte]int64)
values[97] = 10
values[101] = 15
values[105] = 12
values[115] = 3
values[116] = 4
values[112] = 13
values[10] = 1
leafs := leafsFromValues(values)
sortedLeafs := sortNodes(leafs)
n := buildTree(sortedLeafs)
assert.Equal(t, int64(58), n.key)
assert.Equal(t, int64(25), n.l.key)
assert.Equal(t, int64(33), n.r.key)
}
func TestGenerateTable(t *testing.T) {
values := make(map[byte]int64)
values[65] = 3
values[66] = 5
values[67] = 6
values[68] = 4
values[69] = 2
leafs := leafsFromValues(values)
sortedLeafs := sortNodes(leafs)
n := buildTree(sortedLeafs)
table := generateTable(n)
fmt.Println(table)
}

Loading…
Cancel
Save