70,037
社区成员
发帖
与我相关
我的任务
分享#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define n 8
#define m 2*n-1
typedef struct
{int weight;
int lchild,rchild,parent;
}HTNode;
typedef HTNode HuffmanTree[m+1];
typedef struct
{char ch;
char code[n+1];
}CodeNode;
typedef CodeNode HuffmanCode[n+1];
void InitHuffmanTree(HuffmanTree ht)
{ int i;
for(i=0;i<=m;i++)
{ht[i].weight=0;
ht[i].lchild = ht[i].rchild = ht[i].parent=0;
}
}
void InputWeight(HuffmanTree ht)
{ int i;
for(i=1;i<=n;i++)
{printf("Please Inter Num %d:",i);
scanf("%d",&ht[i].weight);
}
}
void SelectMin(HuffmanTree ht,int i,int *p1,int *p2)
{int j,min1,min2;
min1 = min2 = 32767;
*p1 = *p2 = 0;
for(j = 1;j <= i;j++)
{ if(ht[j].parent == 0)
if(ht[j].weight < min1 || min1 == 32767)
{ if(min1 != 32767)
{min2 = min1;
*p2 = *p1;
min1 = ht[j].weight;
*p1= j;
}
else
if(ht[j].weight < min2 ||min2 == 32767)
{min2 = ht[j].weight; *p2 = j;}
}
}
void CreateHuffmanTree(HuffmanTree ht)
{int i,p1,p2;
InitHuffmanTree(ht);
InputWeight(ht);
for(i = n + 1;i <= m;i++)
{ SelectMin(ht,i-1,&p1,&p2);
ht[p1].parent = ht[p2].parent = i;
ht[i].lchild = p1;
ht[i].rchild= p2;
ht[i].weight = ht[p1].weight + ht[p2].weight;
}
}
void Huffmancode(huffmanTree ht,HuffmanCode hcd)
{ int c,p,i;
char cd[n+1];
int start;
cd[n] = '\0';
printf("Please Inter Char!");
for(i = 1;i <=n;i++)
{hcd[i].ch = getch();
start = n;
c = i;
while((p = ht[c].parent) != 0)
{cd[--start] = (ht[p].lchild == c)?'0':'1';
c = p;
}
strcpy(hcd[i].code,&cd[start]);
}
printf("\n");
for(i = 1;i <= n;i++)
printf("第%d个zifu%c deCode is %s\n",i,hcd[i].ch,hcd[i].code);
}
void main()
{HuffmanTree t;
HuffmanCode h;
printf("\n Please Enter Num %d :",n);
CreateHuffmanTree(t);
Huffmancode(t,h);
}
都很认真,帮顶!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define n 8
#define m 2*n-1
typedef struct
{
int weight;
int lchild, rchild, parent;
} HTNode;
typedef HTNode HuffmanTree[m + 1];
typedef struct
{
char ch;
char code[n + 1];
} CodeNode;
typedef CodeNode HuffmanCode[n + 1];
void
InitHuffmanTree (HuffmanTree ht)
{
int i;
for (i = 0; i <= m; i++)
{
ht[i].weight = 0;
ht[i].lchild = ht[i].rchild = ht[i].parent = 0;
}
}
void
InputWeight (HuffmanTree ht)
{
int i;
for (i = 1; i <= n; i++)
{
printf ("Please Inter Num %d:", i);
scanf ("%d", &ht[i].weight);
}
}
void
SelectMin (HuffmanTree ht, int i, int *p1, int *p2)
{
int j, min1, min2;
min1 = min2 = 32767;
*p1 = *p2 = 0;
for (j = 1; j <= i; j++)
{
if (ht[j].parent == 0)
if (ht[j].weight < min1 || min1 == 32767)
{
if (min1 != 32767)
{
min2 = min1;
*p2 = *p1;
min1 = ht[j].weight;
*p1 = j;
}
else if (ht[j].weight < min2 || min2 == 32767)
{
min2 = ht[j].weight;
*p2 = j;
}
}
}
}
void
CreateHuffmanTree (HuffmanTree ht)
{
int i, p1, p2;
InitHuffmanTree (ht);
InputWeight (ht);
for (i = n + 1; i <= m; i++)
{
SelectMin (ht, i - 1, &p1, &p2);
ht[p1].parent = ht[p2].parent = i;
ht[i].lchild = p1;
ht[i].rchild = p2;
ht[i].weight = ht[p1].weight + ht[p2].weight;
}
}
void
Huffmancode (HuffmanTree ht, HuffmanCode hcd)
{
int c, p, i;
char cd[n + 1];
int start;
cd[n] = '\0';
printf ("Please Inter Char!\n");
for (i = 1; i <= n; i++)
{
hcd[i].ch = getchar ();
start = n;
c = i;
while ((p = ht[c].parent) != 0)
{
cd[--start] = (ht[p].lchild == c) ? '0' : '1';
c = p;
}
strcpy (hcd[i].code, &cd[start]);
}
printf ("\n");
for (i = 1; i <= n; i++)
printf ("第%d个zifu%c deCode is %s\n", i, hcd[i].ch, hcd[i].code);
}
void
main ()
{
HuffmanTree t;
HuffmanCode h;
printf ("\n Please Enter %d Num :\n", n);
CreateHuffmanTree (t);
Huffmancode (t, h);
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>
#define n 8
#define m 2*n-1
typedef struct
{int weight;
int lchild,rchild,parent;
}HTNode;
typedef HTNode HuffmanTree[m+1];
typedef struct
{char ch;
char code[n+1];
}CodeNode;
typedef CodeNode HuffmanCode[n+1];
void InitHuffmanTree(HuffmanTree ht)
{ int i;
for(i=0;i<=m;i++)
{ht[i].weight=0;
ht[i].lchild = ht[i].rchild = ht[i].parent=0;
}
}
void InputWeight(HuffmanTree ht)
{ int i;
for(i=1;i<=n;i++)
{printf("Please Inter Num %d:",i);
scanf("%d",&ht[i].weight);
}
}
void SelectMin(HuffmanTree ht,int i,int *p1,int *p2)
{int j,min1,min2;
min1 = min2 = 32767;
*p1 = *p2 = 0;
for(j = 1;j <= i;j++)
{ if(ht[j].parent == 0)
if(ht[j].weight < min1 || min1 == 32767)
{ if(min1 != 32767)
{min2 = min1;
*p2 = *p1;
min1 = ht[j].weight;
*p1= j;
}
else
if(ht[j].weight < min2 ||min2 == 32767)
{min2 = ht[j].weight; *p2 = j;}
}
}
}
void CreateHuffmanTree(HuffmanTree ht)
{
int i,p1,p2;
InitHuffmanTree(ht);
InputWeight(ht);
for(i = n + 1;i <= m;i++)
{ SelectMin(ht,i-1,&p1,&p2);
ht[p1].parent = ht[p2].parent = i;
ht[i].lchild = p1;
ht[i].rchild= p2;
ht[i].weight = ht[p1].weight + ht[p2].weight;
}
}
void Huffmancode(HuffmanTree ht,HuffmanCode hcd)
{ int c,p,i;
char cd[n+1];
int start;
cd[n] = '\0';
printf("Please Inter Char!");
for(i = 1;i <=n;i++)
{hcd[i].ch = getch();
start = n;
c = i;
while((p = ht[c].parent) != 0)
{cd[--start] = (ht[p].lchild == c)?'0':'1';
c = p;
}
strcpy(hcd[i].code,&cd[start]);
}
printf("\n");
for(i = 1;i <= n;i++)
printf("第%d个zifu%c deCode is %s\n",i,hcd[i].ch,hcd[i].code);
}
void main()
{HuffmanTree t;
HuffmanCode h;
printf("\n Please Enter Num %d :",n);
CreateHuffmanTree(t);
Huffmancode(t,h);
}