哈夫曼编码/译码器数据结构课程设计
时间: 2023-11-29 13:47:18 浏览: 113
数据结构课程设计(哈夫曼编码).pdf
5星 · 资源好评率100%
以下是哈夫曼编码/译码器的数据结构课程设计:
1. 数据结构设计
(1)哈夫曼树节点的数据结构
```c
typedef struct HuffmanNode {
char data; // 节点存储的字符
int weight; // 节点的权重
int parent; // 节点的父节点下标
int lchild; // 节点的左孩子下标
int rchild; // 节点的右孩子下标
} HuffmanNode;
```
(2)哈夫曼编码的数据结构
```c
typedef struct HuffmanCode {
char data; // 字符
char *code; // 编码
} HuffmanCode;
```
2. 哈夫曼编码/译码器的实现
(1)统计文本文件中每个字符出现的次数,生成哈夫曼树
```c
void createHuffmanTree(HuffmanNode *huffmanTree, int n) {
int i, j, min1, min2;
for (i = 0; i < 2 * n - 1; i++) {
huffmanTree[i].parent = -1;
huffmanTree[i].lchild = -1;
huffmanTree[i].rchild = -1;
}
for (i = 0; i < n; i++) {
printf("请输入第%d个字符及其权重:", i + 1);
scanf("%s%d", &huffmanTree[i].data, &huffmanTree[i].weight);
}
for (i = 0; i < n - 1; i++) {
min1 = min2 = -1;
for (j = 0; j < n + i; j++) {
if (huffmanTree[j].parent == -1) {
if (min1 == -1) {
min1 = j;
} else if (min2 == -1) {
min2 = j;
} else {
if (huffmanTree[j].weight < huffmanTree[min1].weight) {
min2 = min1;
min1 = j;
} else if (huffmanTree[j].weight < huffmanTree[min2].weight) {
min2 = j;
}
}
}
}
huffmanTree[min1].parent = n + i;
huffmanTree[min2].parent = n + i;
huffmanTree[n + i].lchild = min1;
huffmanTree[n + i].rchild = min2;
huffmanTree[n + i].weight = huffmanTree[min1].weight + huffmanTree[min2].weight;
}
}
```
(2)生成哈夫曼编码
```c
void createHuffmanCode(HuffmanNode *huffmanTree, HuffmanCode *huffmanCode, int n) {
char *code = (char *) malloc(n * sizeof(char));
int i, j, parent, current;
code[n - 1] = '\0';
for (i = 0; i < n; i++) {
huffmanCode[i].data = huffmanTree[i].data;
current = i;
parent = huffmanTree[current].parent;
while (parent != -1) {
if (huffmanTree[parent].lchild == current) {
code[--n] = '0';
} else {
code[--n] = '1';
}
current = parent;
parent = huffmanTree[current].parent;
}
huffmanCode[i].code = (char *) malloc((strlen(&code[n]) + 1) * sizeof(char));
strcpy(huffmanCode[i].code, &code[n]);
}
free(code);
}
```
(3)编码文本文件
```c
void encodeFile(char *fileName, HuffmanCode *huffmanCode, int n) {
FILE *fp1, *fp2;
char ch;
int i, j;
if ((fp1 = fopen(fileName, "r")) == NULL) {
printf("文件打开失败!\n");
exit(0);
}
if ((fp2 = fopen(strcat(fileName, ".cod"), "w")) == NULL) {
printf("文件打开失败!\n");
exit(0);
}
while ((ch = fgetc(fp1)) != EOF) {
for (i = 0; i < n; i++) {
if (huffmanCode[i].data == ch) {
fprintf(fp2, "%s", huffmanCode[i].code);
break;
}
}
}
fclose(fp1);
fclose(fp2);
}
```
(4)译码编码文件
```c
void decodeFile(char *fileName, HuffmanNode *huffmanTree, int n) {
FILE *fp1, *fp2;
char ch;
int i, j, current = 2 * n - 2;
if ((fp1 = fopen(fileName, "r")) == NULL) {
printf("文件打开失败!\n");
exit(0);
}
fileName[strlen(fileName) - 4] = '\0';
if ((fp2 = fopen(fileName, "w")) == NULL) {
printf("文件打开失败!\n");
exit(0);
}
while ((ch = fgetc(fp1)) != EOF) {
if (ch == '0') {
current = huffmanTree[current].lchild;
} else {
current = huffmanTree[current].rchild;
}
if (huffmanTree[current].lchild == -1 && huffmanTree[current].rchild == -1) {
fprintf(fp2, "%c", huffmanTree[current].data);
current = 2 * n - 2;
}
}
fclose(fp1);
fclose(fp2);
}
```
阅读全文