|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
// Huffman树节点结构
typedef struct Node {
char data;
double weight;
struct Node* parent;
struct Node* lchild;
struct Node* rchild;
} HFTree;
// Huffman编码结构
typedef struct {
char data;
char* code;
} HFCode;
// 构建Huffman树的函数
void createHFTree(HFTree* HT, char value[], double fr[], int n);
// 输出Huffman树结点信息的函数
void displayHFTree(HFTree* node, int level);
// 生成Huffman编码本的函数
void createHFCode(HFTree* HT, HFCode* HC, int n);
// 输出Huffman编码本的函数
void printHFCode(HFCode* HC, int n);
int main() {
char value[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
double fr[] = {0.07, 0.19, 0.02, 0.06, 0.32, 0.03, 0.21, 0.10};
int n = sizeof(value) / sizeof(value[0]);
HFTree HT[2 * n - 1];
// 构建Huffman树
createHFTree(HT, value, fr, n);
// 输出Huffman树结点信息
printf("Huffman树结点信息:\n");
displayHFTree(&HT[2 * n - 2], 0);
// 生成Huffman编码本
HFCode HC[n];
createHFCode(&HT[2 * n - 2], HC, n);
// 输出Huffman编码本
printf("\nHuffman编码本:\n");
printHFCode(HC, n);
return 0;
}
// 构建Huffman树的函数实现
void createHFTree(HFTree* HT, char value[], double fr[], int n) {
int lnode, rnode;
int min1, min2;
for (int i = 0; i < 2 * n - 1; i++) {
HT[i].parent = HT[i].lchild = HT[i].rchild = NULL;
HT[i].data = '\0';
if (i < n) {
HT[i].data = value[i];
HT[i].weight = fr[i];
}
}
for (int i = n; i < 2 * n - 1; i++) {
min1 = min2 = 32767;
lnode = rnode = -1;
for (int j = 0; j <= i - 1; j++) {
if (HT[j].parent == NULL) {
if (HT[j].weight < min1) {
min2 = min1;
rnode = lnode;
min1 = HT[j].weight;
lnode = j;
} else if (HT[j].weight < min2) {
min2 = HT[j].weight;
rnode = j;
}
}
}
HT[i].weight = HT[lnode].weight + HT[rnode].weight;
HT[i].lchild = &HT[lnode];
HT[i].rchild = &HT[rnode];
HT[lnode].parent = &HT[i];
HT[rnode].parent = &HT[i];
}
}
// 输出Huffman树结点信息的函数实现
void displayHFTree(HFTree* node, int level) {
if (node != NULL) {
displayHFTree(node->rchild, level + 1);
for (int i = 0; i < level; i++) {
printf(" ");
}
if (node->data != '\0') {
printf("%c:%.2f\n", node->data, node->weight);
} else {
printf("-:%.2f\n", node->weight);
}
displayHFTree(node->lchild, level + 1);
}
}
// 生成Huffman编码本的函数实现
void createHFCode(HFTree* HT, HFCode* HC, int n) {
for (int i = 0; i < n; i++) {
HC[i].data = HT[i].data;
HC[i].code = (char*)malloc(n * sizeof(char));
int current = i;
int index = 0; // 修正为从0开始
// 从叶子节点追溯到根节点,构建Huffman编码
while (HT[current].parent != NULL) {
if (HT[HT[current].parent - HT].lchild == &HT[current]) { // 修正为从根节点找父节点
HC[i].code[index++] = '0';
} else {
HC[i].code[index++] = '1';
}
current = HT[current].parent - HT;
}
// 在Huffman编码的末尾添加字符串结束符'\0'
HC[i].code[index] = '\0';
}
}
// 输出Huffman编码本的函数实现
void printHFCode(HFCode* HC, int n) {
for (int i = 0; i < n; i++) {
printf("%c: %s\n", HC[i].data, HC[i].code);
}
}
为什么输出Huffman编码本的函数实现没有输出结果
出现这个问题是因为在函数createHFCode中有几个地方出现了语法错误和逻辑错误。
首先,在createHFCode函数中,传入的参数HT和HC应该是指针类型的数组,而不是单个变量,所以函数的参数类型应该是HFTree* HT[]和HFCode* HC[]。
其次,在createHFCode函数中,对HC.code进行赋值的语句应该改为HC[i].code = '0'和HC[i].code = '1',以及最后添加字符串结束符的语句应该改为HC[i].code[index] = '\0'。
另外,在printHFCode函数中,应该使用HC[i].data和HC[i].code来输出相应的数据。
下面是修改后的代码:
#include
#include
// Huffman树节点结构
typedef struct Node {
char data;
double weight;
struct Node* parent;
struct Node* lchild;
struct Node* rchild;
} HFTree;
// Huffman编码结构
typedef struct {
char data;
char* code;
} HFCode;
// 构建Huffman树的函数
void createHFTree(HFTree* HT[], char value[], double fr[], int n);
// 输出Huffman树结点信息的函数
void displayHFTree(HFTree* node, int level);
// 生成Huffman编码本的函数
void createHFCode(HFTree* HT[], HFCode* HC[], int n);
// 输出Huffman编码本的函数
void printHFCode(HFCode* HC[], int n);
int main() {
char value[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
double fr[] = {0.07, 0.19, 0.02, 0.06, 0.32, 0.03, 0.21, 0.10};
int n = sizeof(value) / sizeof(value[0]);
HFTree* HT[n];
for (int i = 0; i < n; i++) {
HT[i] = malloc(sizeof(HFTree));
}
// 构建Huffman树
createHFTree(HT, value, fr, n);
// 输出Huffman树结点信息
printf("Huffman树结点信息:\n");
displayHFTree(*HT[0], 0);
// 生成Huffman编码本
HFCode* HC[n];
for (int i = 0; i < n; i++) {
HC[i] = malloc(sizeof(HFCode));
}
createHFCode(HT, HC, n);
// 输出Huffman编码本
printf("\nHuffman编码本:\n");
printHFCode(HC, n);
return 0;
}
// 构建Huffman树的函数实现
void createHFTree(HFTree* HT[], char value[], double fr[], int n) {
int lnode, rnode;
int min1, min2;
for (int i = 0; i < 2 * n - 1; i++) {
HT[i]->parent = HT[i]->lchild = HT[i]->rchild = NULL;
HT[i]->data = '\0';
if (i < n) {
HT[i]->data = value[i];
HT[i]->weight = fr[i];
}
}
for (int i = n; i < 2 * n - 1; i++) {
min1 = min2 = 32767;
lnode = rnode = -1;
for (int j = 0; j < i; j++) {
if (HT[j]->parent == NULL) {
if (HT[j]->weight < min1) {
min2 = min1;
rnode = lnode;
min1 = HT[j]->weight;
lnode = j;
} else if (HT[j]->weight < min2) {
min2 = HT[j]->weight;
rnode = j;
}
}
}
HT[lnode]->parent = HT[rnode]->parent = HT[i];
HT[i]->lchild = HT[lnode];
HT[i]->rchild = HT[rnode];
HT[i]->weight = HT[lnode]->weight + HT[rnode]->weight;
}
}
// 输出Huffman树结点信息的函数实现
void displayHFTree(HFTree* node, int level) {
if (node != NULL) {
displayHFTree(node->rchild, level + 1);
for (int i = 0; i < level; i++) {
printf(" ");
}
if (node->data != '\0') {
printf("%c:%.2f\n", node->data, node->weight);
} else {
printf("-:%.2f\n", node->weight);
}
displayHFTree(node->lchild, level + 1);
}
}
// 生成Huffman编码本的函数实现
void createHFCode(HFTree* HT[], HFCode* HC[], int n) {
for (int i = 0; i < n; i++) {
HC[i]->data = HT[i]->data;
HC[i]->code = malloc(n * sizeof(char));
int current = i;
int index = 0;
// 从叶子节点追溯到根节点,构建Huffman编码
while (HT[current]->parent != NULL) {
if (HT[HT[current]->parent - HT]->lchild == HT[current]) {
HC[i]->code[index] = '0';
} else {
HC[i]->code[index] = '1';
}
current = HT[current]->parent - HT;
index++;
}
// 在Huffman编码的末尾添加字符串结束符'\0'
HC[i]->code[index] = '\0';
}
}
// 输出Huffman编码本的函数实现
void printHFCode(HFCode* HC[], int n) {
for (int i = 0; i < n; i++) {
printf("%c: %s\n", HC[i]->data, HC[i]->code);
}
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|