-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathhuffman.go
More file actions
112 lines (94 loc) · 2.43 KB
/
huffman.go
File metadata and controls
112 lines (94 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* 霍夫曼编码实现 - Go
* 基于最小堆构建最优前缀编码
*/
package main
import (
"container/heap"
"fmt"
"strings"
)
// 霍夫曼树节点
type HuffmanNode struct {
char rune
freq int
left *HuffmanNode
right *HuffmanNode
}
// 最小堆实现(Go标准库heap.Interface)
type HuffmanHeap []*HuffmanNode
func (h HuffmanHeap) Len() int { return len(h) }
func (h HuffmanHeap) Less(i, j int) bool { return h[i].freq < h[j].freq }
func (h HuffmanHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *HuffmanHeap) Push(x interface{}) { *h = append(*h, x.(*HuffmanNode)) }
func (h *HuffmanHeap) Pop() interface{} {
old := *h
n := len(old)
item := old[n-1]
*h = old[0 : n-1]
return item
}
// 霍夫曼编码入口:返回字符到编码的映射
func HuffmanEncode(text string) map[rune]string {
// 统计字符频率
freqMap := make(map[rune]int)
for _, char := range text {
freqMap[char]++
}
// 初始化:所有字符节点入堆
h := &HuffmanHeap{}
heap.Init(h)
for char, freq := range freqMap {
heap.Push(h, &HuffmanNode{char: char, freq: freq})
}
// 构建Huffman树
for h.Len() > 1 {
left := heap.Pop(h).(*HuffmanNode)
right := heap.Pop(h).(*HuffmanNode)
parent := &HuffmanNode{
freq: left.freq + right.freq,
left: left,
right: right,
}
heap.Push(h, parent)
}
// 生成编码表
encodingMap := make(map[rune]string)
var root *HuffmanNode
if h.Len() > 0 {
root = heap.Pop(h).(*HuffmanNode)
}
generateCodes(root, "", encodingMap)
return encodingMap
}
func generateCodes(node *HuffmanNode, code string, encodingMap map[rune]string) {
if node == nil {
return
}
if node.left == nil && node.right == nil {
if code == "" {
encodingMap[node.char] = "0"
} else {
encodingMap[node.char] = code
}
return
}
generateCodes(node.left, code+"0", encodingMap)
generateCodes(node.right, code+"1", encodingMap)
}
func HuffmanCompress(text string, encodingMap map[rune]string) string {
var compressed strings.Builder
for _, char := range text {
compressed.WriteString(encodingMap[char])
}
return compressed.String()
}
func main() {
text := "hello world"
fmt.Printf("原始文本: %s\n", text)
encodingMap := HuffmanEncode(text)
fmt.Printf("编码表: %v\n", encodingMap)
compressed := HuffmanCompress(text, encodingMap)
fmt.Printf("压缩后: %s\n", compressed)
fmt.Printf("压缩率: %.2f\n", float64(len(compressed))/float64(len(text)*8))
}