本文实例讲述了基于C++实现的哈夫曼编码解码操作。分享给大家供大家参考,具体如下:
哈夫曼编码是一个通过哈夫曼树进行的一种编码,一般情况下,以字符:‘0'与‘1'表示。编码的实现过程很简单,只要实现哈夫曼树,通过遍历哈夫曼树,这里我们从每一个叶子结点开始向上遍历,如果该结点为父节点的左孩子,则在字符串后面追加“0”,如果为其右孩子,则在字符串后追加“1”。结束条件为没有父节点。然后将字符串倒过来存入结点中。
C++实现代码如下:
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#include<iostream> #include<string> using namespace std; struct Node { double weight; string ch; string code; int lchild, rchild, parent; }; void Select(Node huffTree[], int *a, int *b, int n) //找权值最小的两个a和b { int i; double weight = 0; //找最小的数 for (i = 0; i <n; i++) { if (huffTree[i].parent != -1) //判断节点是否已经选过 continue ; else { if (weight == 0) { weight = huffTree[i].weight; *a = i; } else { if (huffTree[i].weight < weight) { weight = huffTree[i].weight; *a = i; } } } } weight = 0; //找第二小的数 for (i = 0; i < n; i++) { if (huffTree[i].parent != -1 || (i == *a)) //排除已选过的数 continue ; else { if (weight == 0) { weight = huffTree[i].weight; *b = i; } else { if (huffTree[i].weight < weight) { weight = huffTree[i].weight; *b = i; } } } } int temp; if (huffTree[*a].lchild < huffTree[*b].lchild) //小的数放左边 { temp = *a; *a = *b; *b = temp; } } void Huff_Tree(Node huffTree[], int w[], string ch[], int n) { for ( int i = 0; i < 2 * n - 1; i++) //初始过程 { huffTree[i].parent = -1; huffTree[i].lchild = -1; huffTree[i].rchild = -1; huffTree[i].code = "" ; } for ( int i = 0; i < n; i++) { huffTree[i].weight = w[i]; huffTree[i].ch = ch[i]; } for ( int k = n; k < 2 * n - 1; k++) { int i1 = 0; int i2 = 0; Select(huffTree, &i1, &i2, k); //将i1,i2节点合成节点k huffTree[i1].parent = k; huffTree[i2].parent = k; huffTree[k].weight = huffTree[i1].weight + huffTree[i2].weight; huffTree[k].lchild = i1; huffTree[k].rchild = i2; } } void Huff_Code(Node huffTree[], int n) { int i, j, k; string s = "" ; for (i = 0; i < n; i++) { s = "" ; j = i; while (huffTree[j].parent != -1) //从叶子往上找到根节点 { k = huffTree[j].parent; if (j == huffTree[k].lchild) //如果是根的左孩子,则记为0 { s = s + "0" ; } else { s = s + "1" ; } j = huffTree[j].parent; } cout << "字符 " << huffTree[i].ch << " 的编码:" ; for ( int l = s.size() - 1; l >= 0; l--) { cout << s[l]; huffTree[i].code += s[l]; //保存编码 } cout << endl; } } string Huff_Decode(Node huffTree[], int n,string s) { cout << "解码后为:" ; string temp = "" ,str= "" ; //保存解码后的字符串 for ( int i = 0; i < s.size(); i++) { temp = temp + s[i]; for ( int j = 0; j < n; j++) { if (temp == huffTree[j].code) { str=str+ huffTree[j].ch; temp = "" ; break ; } else if (i == s.size()-1&&j==n-1&&temp!= "" ) //全部遍历后没有 { str= "解码错误!" ; } } } return str; } int main() { //编码过程 const int n=5; Node huffTree[2 * n]; string str[] = { "A" , "B" , "C" , "D" , "E" }; int w[] = { 30, 30, 5, 20, 15 }; Huff_Tree(huffTree, w, str, n); Huff_Code(huffTree, n); //解码过程 string s; cout << "输入编码:" ; cin >> s; cout << Huff_Decode(huffTree, n, s)<< endl;; system ( "pause" ); return 0; } |
运行结果如下:
希望本文所述对大家C++程序设计有所帮助。
原文链接:https://www.cnblogs.com/gyk666/p/6851821.html