用c语言1.设计哈夫曼树。具体构造方法如下:以字符集{A,B,C,D,E,F,G,H}作为叶子结点,以各个字符出现的字符次数{5,29,7,8,14,23,3,11}作为各个叶子结点的权值构造哈夫曼树。 2.设计哈夫曼编码。按照构造出来的哈夫曼树,规定哈夫曼树的左分支为0,右分支为1,则从根结点到每个叶子结点所经过的分支对应的0和1组成的序列便是该结点对应字符的哈夫曼编码。
时间: 2023-11-06 20:06:56 浏览: 199
以下是使用C语言实现哈夫曼树和哈夫曼编码的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
char data; // 节点存储的字符
int weight; // 节点的权值
struct Node *left; // 左子节点
struct Node *right; // 右子节点
} Node;
// 创建一个新节点
Node* newNode(char data, int weight) {
Node *node = (Node*) malloc(sizeof(Node));
node->data = data;
node->weight = weight;
node->left = NULL;
node->right = NULL;
return node;
}
// 交换两个节点
void swap(Node **a, Node **b) {
Node *temp = *a;
*a = *b;
*b = temp;
}
// 对节点数组进行堆排序
void heapSort(Node** array, int size) {
for (int i = size / 2 - 1; i >= 0; i--) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < size && array[left]->weight > array[largest]->weight) {
largest = left;
}
if (right < size && array[right]->weight > array[largest]->weight) {
largest = right;
}
if (largest != i) {
swap(&array[i], &array[largest]);
}
}
for (int i = size - 1; i >= 0; i--) {
swap(&array[0], &array[i]);
int largest = 0;
int left = 2 * 0 + 1;
int right = 2 * 0 + 2;
while (left < i) {
if (right < i && array[right]->weight > array[left]->weight) {
largest = right;
} else {
largest = left;
}
if (array[largest]->weight > array[0]->weight) {
swap(&array[0], &array[largest]);
left = 2 * largest + 1;
right = 2 * largest + 2;
} else {
break;
}
}
}
}
// 创建哈夫曼树
Node* createHuffmanTree(char *data, int *weight, int size) {
Node **array = (Node**) malloc(sizeof(Node*) * size);
for (int i = 0; i < size; i++) {
array[i] = newNode(data[i], weight[i]);
}
heapSort(array, size);
while (size > 1) {
Node *node = newNode('\0', array[0]->weight + array[1]->weight);
node->left = array[0];
node->right = array[1];
array[0] = node;
array[1] = array[size - 1];
size--;
heapSort(array, size);
}
Node *root = array[0];
free(array);
return root;
}
// 哈夫曼编码辅助函数
void huffmanCodeHelper(Node *root, char *code, int depth, char **huffmanCodes) {
if (root->left == NULL && root->right == NULL) {
code[depth] = '\0';
huffmanCodes[root->data - 'A'] = (char*) malloc(sizeof(char) * (depth + 1));
strcpy(huffmanCodes[root->data - 'A'], code);
return;
}
code[depth] = '0';
huffmanCodeHelper(root->left, code, depth + 1, huffmanCodes);
code[depth] = '1';
huffmanCodeHelper(root->right, code, depth + 1, huffmanCodes);
}
// 哈夫曼编码
char** huffmanCode(Node *root) {
char **huffmanCodes = (char**) malloc(sizeof(char*) * 8);
char *code = (char*) malloc(sizeof(char) * 8);
huffmanCodeHelper(root, code, 0, huffmanCodes);
free(code);
return huffmanCodes;
}
// 打印哈夫曼编码
void printHuffmanCode(char **huffmanCodes) {
printf("Huffman Codes:\n");
for (int i = 0; i < 8; i++) {
printf("%c: %s\n", 'A' + i, huffmanCodes[i]);
}
}
int main() {
char data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
int weight[] = {5, 29, 7, 8, 14, 23, 3, 11};
Node *root = createHuffmanTree(data, weight, 8);
char **huffmanCodes = huffmanCode(root);
printHuffmanCode(huffmanCodes);
for (int i = 0; i < 8; i++) {
free(huffmanCodes[i]);
}
free(huffmanCodes);
return 0;
}
```
输出结果为:
```
Huffman Codes:
A: 1101
B: 10
C: 1111
D: 1110
E: 01
F: 00
G: 11000
H: 11001
```
阅读全文