#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 100 typedef struct { char data; unsigned int weight; unsigned int parent,lchild, rchild; }HTNode; typedef struct { char cd[N]; int start;} HCode; // 创建Huffman树 void createHT(HTNode ht[], int n) { int i, k, lnode, rnode; double min1, min2; for (i = 0; i < 2 * n - 1; i++) ht[i].parent = ht[i].lchild = ht[i].rchild = -1; for (i = n; i <= 2 * n - 2; i++) { min1 = min2 = 32767; lnode = rnode = -1; for (k = 0; k <= i - 1; k++) { if (ht[k].parent == -1) { if (ht[k].weight < min1) { min2 = min1; rnode = lnode; min1 = ht[k].weight; lnode = k; } else if (ht[k].weight < min2) { min2 = ht[k].weight; rnode = k; } } } ht[i].weight = ht[lnode].weight + ht[rnode].weight; ht[i].lchild = lnode; ht[i].rchild = rnode; ht[lnode].parent = i; ht[rnode].parent = i; }} // 生成编码 void CreateHCode(HTNode ht[], HCode hcd[], int n) { int i, f, c; HCode hc; for (i = 0; i < n; i++) { hc.start = n; c = i; f = ht[i].parent; while (f != -1) { if (ht[f].lchild == c) hc.cd[hc.start--] = '0'; else hc.cd[hc.start--] = '1'; c = f; f = ht[f].parent; } hc.start++; hcd[i] = hc; }} int main() { char str[N] = "\0"; int x; printf("请输入字符串:"); gets(str); int n = strlen(str); HTNode ht[N]; for (int i = 0; i < n; i++) { ht[i].data = str[i]; printf("请输入第%d个结点权重:", i + 1); scanf("%d", &x); ht[i].weight = x; } HCode hcd[N]; createHT(ht, n); CreateHCode(ht, hcd, n); for (int j = 0; j < n; j++) { printf("%s", hcd[j].cd); } return 0; }改正错误
时间: 2024-01-14 17:03:41 浏览: 102
在代码中,定义了一个结构体 HCode,但没有给它赋初值,导致在生成编码时可能会出现错误。正确的方式是在定义时给 HCode 赋初值,例如:
```
typedef struct {
char cd[N];
int start;
} HCode = {"", 0};
```
另外,get() 函数在有些编译器中可能已经被弃用,可以使用 fgets() 函数代替。完整的修改后代码如下:
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 100
typedef struct {
char data;
unsigned int weight;
unsigned int parent,lchild, rchild;
}HTNode;
typedef struct {
char cd[N];
int start;
} HCode = {"", 0};
// 创建Huffman树
void createHT(HTNode ht[], int n) {
int i, k, lnode, rnode;
double min1, min2;
for (i = 0; i < 2 * n - 1; i++)
ht[i].parent = ht[i].lchild = ht[i].rchild = -1;
for (i = n; i <= 2 * n - 2; i++) {
min1 = min2 = 32767;
lnode = rnode = -1;
for (k = 0; k <= i - 1; k++) {
if (ht[k].parent == -1) {
if (ht[k].weight < min1) {
min2 = min1;
rnode = lnode;
min1 = ht[k].weight;
lnode = k;
}
else if (ht[k].weight < min2) {
min2 = ht[k].weight;
rnode = k;
}
}
}
ht[i].weight = ht[lnode].weight + ht[rnode].weight;
ht[i].lchild = lnode;
ht[i].rchild = rnode;
ht[lnode].parent = i;
ht[rnode].parent = i;
}
}
// 生成编码
void CreateHCode(HTNode ht[], HCode hcd[], int n) {
int i, f, c;
HCode hc;
for (i = 0; i < n; i++) {
hc.start = n;
c = i;
f = ht[i].parent;
while (f != -1) {
if (ht[f].lchild == c)
hc.cd[hc.start--] = '0';
else
hc.cd[hc.start--] = '1';
c = f;
f = ht[f].parent;
}
hc.start++;
hcd[i] = hc;
}
}
int main() {
char str[N] = "\0";
int x;
printf("请输入字符串:");
fgets(str, N, stdin);
int n = strlen(str) - 1; // 去掉换行符
HTNode ht[N];
for (int i = 0; i < n; i++) {
ht[i].data = str[i];
printf("请输入第%d个结点权重:", i + 1);
scanf("%d", &x);
ht[i].weight = x;
}
HCode hcd[N];
createHT(ht, n);
CreateHCode(ht, hcd, n);
for (int j = 0; j < n; j++) {
printf("%s", hcd[j].cd);
}
return 0;
}
```
阅读全文