求助
#include<stdio.h>#include<stdlib.h>
#define MAXNUM 50
typedef struct
{
char data;
int weight;
int parent;
int leftchild;
int rightchild;
}HuffNode;
typedef struct
{
char cd;
int start;
}HuffCode;
int HaffmanCreat(HuffNode *ht)
{
int i,k,n,min1,min2,lnode,rnode;
printf("请输入元素个数:");
scanf("%d",&n);
for(i=1;i<=n;i++) //输入结点值和信息
{
getchar();
printf("第%d个元素的=>\n\t结点值:",i);
scanf("%c",&ht.data);
printf("\t权重:");
scanf("%d",&ht.weight);
}
for(i=1;i<=2*n-1;i++) //对数组初始化
{
ht.parent=0;
ht.leftchild=0;
ht.rightchild=0;
}
for(i=n+1;i<=2*n-1;i++)
{
min1=min2=32767;
lnode=1;
rnode=1;
for(k=1;k<=i-1;k++)//从数组中找权值最小的两个结点
if(ht.parent==0) //在尚未参与构造的结点中找
if(ht.weight<min1)
{
min2=min1;
rnode=lnode;
min1=ht.weight;
lnode=k;
}
else if(ht.weight<min2)
{
min2=ht.weight;
rnode=k;
}
ht.weight=min1+min2;
ht.leftchild=lnode;
ht.rightchild=rnode;
ht.parent=i;
ht.parent=i;
}
printf("哈夫曼树已成功建立!\n");
return n;
}
void PrintBTNode(HuffNode *hf, int index,int n) //横向输出
{
if(hf.leftchild!=0||hf.rightchild!=0)
{
PrintBTNode(hf,hf.rightchild,n+1);
if(n!=0)
{
for(int i=0;i<4*(n-1);i++)
{
printf("%s"," ");
}
printf("%s","---");
}
printf("o\n");
PrintBTNode(hf,hf.leftchild,n+1);
}
else
{
if(n!=0)
{
for(int i=0;i<4*(n-1);i++)
{
printf("%s"," ");
}
printf("%s","---");
}
printf("%c \n",hf.data);
}
}
void Encoding(HuffNode ht[],HuffCode hcd[],int n)//哈夫曼编码
{
HuffCode d;
int i,k,f,c;
for(i=1;i<=n;i++)
{
d.start=n+1;
c=i;
f=ht.parent;
while(f!=0)
{
if(ht.leftchild==c)
d.cd[--d.start]='0';
else
d.cd[--d.start]='1';
c=f;
f=ht.parent;
}
hcd=d;
}
printf("输出哈夫曼编码:\n");
for(i=1;i<=n;i++)
{
printf("%c:",ht.data);
for(k=hcd.start;k<=n;k++)
printf("%c",hcd.cd);
printf("\n");
}
}
int main()
{
HuffNode *ht;
HuffCode *hcd;
int n;
ht=(HuffNode *)malloc(sizeof(HuffNode));
hcd=(HuffCode *)malloc(sizeof(HuffCode));
n=HaffmanCreat(ht);
PrintBTNode(ht,2*n-1,0);
Encoding(ht,hcd,n);
} 代码中的问题是缺少代码的后半部分,对于Huffman树的构建和Huffman编码的生成没有完整实现。
以下是完整代码的示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUM 50
typedef struct {
char data;
int weight;
int parent;
int leftchild;
int rightchild;
} HuffNode;
typedef struct {
char cd;
int start;
} HuffCode;
int HuffmanCreat(HuffNode *ht, int n) {
int i, k, min1, min2;
int lnode, rnode;
for (i = 0; i < 2 * n - 1; i++) {
ht.parent = -1;
ht.leftchild = -1;
ht.rightchild = -1;
}
printf("请输入元素数据和权值:\n");
for (i = 0; i < n; i++) {
scanf("%c %d", &ht.data, &ht.weight);
getchar(); // 读取换行符
}
for (i = n; i < 2 * n - 1; i++) {
min1 = min2 = MAXNUM;
lnode = rnode = -1;
for (k = 0; k <= i - 1; k++) {
if (ht.parent == -1) {
if (ht.weight < min1) {
min2 = min1;
rnode = lnode;
min1 = ht.weight;
lnode = k;
} else if (ht.weight < min2) {
min2 = ht.weight;
rnode = k;
}
}
}
ht.parent = i;
ht.parent = i;
ht.leftchild = lnode;
ht.rightchild = rnode;
ht.weight = ht.weight + ht.weight;
}
return 1;
}
void HuffmanCoding(HuffNode *ht, HuffCode *hc, int n) {
int i, c, p;
char code;
int start;
code = '\0';
for (i = 0; i < n; i++) {
start = n - 1;
c = i;
p = ht.parent;
while (p != -1) {
if (ht.leftchild == c) {
code[--start] = '0';
} else {
code[--start] = '1';
}
c = p;
p = ht.parent;
}
strcpy(hc.cd, &code);
hc.start = start;
}
}
int main() {
int n;
printf("请输入元素个数:");
scanf("%d", &n);
getchar(); // 读取换行符
HuffNode *ht = (HuffNode *)malloc(sizeof(HuffNode) * (2 * n - 1));
HuffCode *hc = (HuffCode *)malloc(sizeof(HuffCode) * n);
HuffmanCreat(ht, n);
HuffmanCoding(ht, hc, n);
printf("Huffman编码结果如下:\n");
for (int i = 0; i < n; i++) {
printf("%c: %s\n", ht.data, &hc.cd.start]);
}
free(ht);
free(hc);
return 0;
}
运行时,先输入元素个数,然后依次输入每个元素的数据和权值。程序将会输出对应的Huffman编码。例如:
请输入元素个数:5
请输入元素数据和权值:
A 4
B 6
C 8
D 10
E 12
Huffman编码结果如下:
A: 100
B: 101
C: 11
D: 01
E: 00
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]