输入n个叶子结点的权值构造哈夫曼树,根据哈夫曼树构造哈夫曼编码,对密文完成解码工作。求以上C语言代码
时间: 2024-01-07 11:49:08 浏览: 109
hafumanshu.rar_构造哈夫曼树
以下是C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEAF_NODE_NUM 1000
#define MAX_HUFFMAN_TREE_NODE_NUM 2 * MAX_LEAF_NODE_NUM - 1
typedef struct {
int weight;
int parent;
int lchild;
int rchild;
} HuffmanTreeNode;
typedef struct {
int leaf_node_num;
HuffmanTreeNode nodes[MAX_HUFFMAN_TREE_NODE_NUM];
} HuffmanTree;
typedef struct {
char code[MAX_LEAF_NODE_NUM];
} HuffmanCode;
void createHuffmanTree(HuffmanTree *tree, int *weights, int n) {
if (n > MAX_LEAF_NODE_NUM) {
fprintf(stderr, "Error: the number of leaf nodes is too large\n");
exit(EXIT_FAILURE);
}
int i, j, p1, p2, min1, min2;
for (i = 0; i < 2 * n - 1; i++) {
tree->nodes[i].weight = 0;
tree->nodes[i].parent = -1;
tree->nodes[i].lchild = -1;
tree->nodes[i].rchild = -1;
}
for (i = 0; i < n; i++) {
tree->nodes[i].weight = weights[i];
}
for (i = 0; i < n - 1; i++) {
min1 = min2 = INT_MAX;
p1 = p2 = -1;
for (j = 0; j < n + i; j++) {
if (tree->nodes[j].parent == -1 && tree->nodes[j].weight < min1) {
min2 = min1;
p2 = p1;
min1 = tree->nodes[j].weight;
p1 = j;
} else if (tree->nodes[j].parent == -1 && tree->nodes[j].weight < min2) {
min2 = tree->nodes[j].weight;
p2 = j;
}
}
tree->nodes[p1].parent = n + i;
tree->nodes[p2].parent = n + i;
tree->nodes[n + i].weight = min1 + min2;
tree->nodes[n + i].lchild = p1;
tree->nodes[n + i].rchild = p2;
}
}
void getHuffmanCode(HuffmanTree *tree, HuffmanCode *codes, int n) {
int i, j, p, c;
char *code = (char *) malloc(n * sizeof(char));
for (i = 0; i < n; i++) {
code[n - 1] = '\0';
p = tree->nodes[i].parent;
c = i;
while (p != -1) {
if (tree->nodes[p].lchild == c) {
code[--n] = '0';
} else {
code[--n] = '1';
}
c = p;
p = tree->nodes[c].parent;
}
strcpy(codes[i].code, &code[n]);
}
free(code);
}
void decode(HuffmanTree *tree, char *cipher, char *plain) {
int i, p = 2 * tree->leaf_node_num - 2;
for (i = 0; cipher[i] != '\0'; i++) {
if (cipher[i] == '0') {
p = tree->nodes[p].lchild;
} else {
p = tree->nodes[p].rchild;
}
if (tree->nodes[p].lchild == -1 && tree->nodes[p].rchild == -1) {
*plain++ = (char) p;
p = 2 * tree->leaf_node_num - 2;
}
}
*plain = '\0';
}
int main() {
int weights[MAX_LEAF_NODE_NUM];
int n, i;
printf("Please input the number of leaf nodes: ");
scanf("%d", &n);
printf("Please input the weight of each leaf node:\n");
for (i = 0; i < n; i++) {
scanf("%d", &weights[i]);
}
HuffmanTree tree;
createHuffmanTree(&tree, weights, n);
HuffmanCode codes[MAX_LEAF_NODE_NUM];
getHuffmanCode(&tree, codes, n);
printf("The Huffman code of each leaf node is:\n");
for (i = 0; i < n; i++) {
printf("%d: %s\n", i, codes[i].code);
}
char cipher[1000], plain[1000];
printf("Please input the cipher to be decoded: ");
scanf("%s", cipher);
decode(&tree, cipher, plain);
printf("The decoded plain text is: %s\n", plain);
return 0;
}
```
其中,`HuffmanTreeNode` 结构体表示哈夫曼树的节点,`HuffmanTree` 结构体表示哈夫曼树,`HuffmanCode` 结构体表示哈夫曼编码。`createHuffmanTree` 函数用于构造哈夫曼树,`getHuffmanCode` 函数用于生成哈夫曼编码,`decode` 函数用于解码密文。在 `main` 函数中,先读入叶子节点的权值,然后调用 `createHuffmanTree` 函数构造哈夫曼树,再调用 `getHuffmanCode` 函数生成哈夫曼编码,最后调用 `decode` 函数解码密文。
阅读全文