initial commit
This commit is contained in:
commit
3dfc7e093f
2 changed files with 72 additions and 0 deletions
52
huffman.go
Normal file
52
huffman.go
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package huff
|
||||||
|
|
||||||
|
import (
|
||||||
|
"container/heap"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Node struct {
|
||||||
|
Weight int64
|
||||||
|
Value interface{}
|
||||||
|
Left *Node
|
||||||
|
Right *Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n Node) Lookup(value interface{}) *Node {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPathTree(paths []string) *Node {
|
||||||
|
nodes := []Node{}
|
||||||
|
for i := range paths {
|
||||||
|
node := Node{Weight: int64(i + 1), Value: paths[i]}
|
||||||
|
nodes = append(nodes, node)
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeHeap := nodeInterface(nodes)
|
||||||
|
heap.Init(nodeHeap)
|
||||||
|
|
||||||
|
return &Node{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Combine(left *Node, right *Node) *Node {
|
||||||
|
var weight int64
|
||||||
|
if left != nil {
|
||||||
|
weight = weight + left.Weight
|
||||||
|
}
|
||||||
|
if right != nil {
|
||||||
|
weight = weight + right.Weight
|
||||||
|
}
|
||||||
|
return &Node{
|
||||||
|
Weight: weight,
|
||||||
|
Left: left,
|
||||||
|
Right: right,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type nodeInterface []Node
|
||||||
|
|
||||||
|
func (ni nodeInterface) Push(x interface{}) {}
|
||||||
|
func (ni nodeInterface) Pop() interface{} { return Node{} }
|
||||||
|
func (ni nodeInterface) Len() int { return len(ni) }
|
||||||
|
func (ni nodeInterface) Less(i, j int) bool { return ni[i].Weight < ni[j].Weight }
|
||||||
|
func (ni nodeInterface) Swap(i, j int) { ni[i], ni[j] = ni[j], ni[i] }
|
20
huffman_test.go
Normal file
20
huffman_test.go
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package huff
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNode(t *testing.T) {
|
||||||
|
paths := []string{
|
||||||
|
"/content/dist/rhel/server/6/$releasever/$relarch/os",
|
||||||
|
"/content/dist/rhui/server/6/$releasever/$relarch/os",
|
||||||
|
"/content/beta/rhel/server/6/$releasever/$relarch/os",
|
||||||
|
"/content/beta/rhel/server/7/$releasever/$relarch/os",
|
||||||
|
"/content/beta/rhui/server/6/$releasever/$relarch/os",
|
||||||
|
"/content/beta/rhui/server/7/$releasever/$relarch/os",
|
||||||
|
}
|
||||||
|
|
||||||
|
pt := NewPathTree(paths)
|
||||||
|
fmt.Printf("%#v\n", pt)
|
||||||
|
}
|
Loading…
Reference in a new issue