哈夫曼树的应用 输入元素 4 分别输入a 4 b 5 c 6 d 10 输出a--->110 b--->111 c--->10 d--->0#include #include #include #define MAXSIZE 30#define MAX 100using namespace std;typedef struct{char data;int weight;int parent;int left;int right;
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 08:42:56
哈夫曼树的应用 输入元素 4 分别输入a 4 b 5 c 6 d 10 输出a--->110 b--->111 c--->10 d--->0#include #include #include #define MAXSIZE 30#define MAX 100using namespace std;typedef struct{char data;int weight;int parent;int left;int right;
哈夫曼树的应用 输入元素 4 分别输入a 4 b 5 c 6 d 10 输出a--->110 b--->111 c--->10 d--->0
#include
#include
#include
#define MAXSIZE 30
#define MAX 100
using namespace std;
typedef struct
{
char data;
int weight;
int parent;
int left;
int right;
} HTNode,*HuffmanTree;
typedef char **HuffmanCode;
typedef struct
{
char data;
int weight;
} Tdata;
HuffmanTree HT;
HuffmanCode HC;
void SelectMinTree(HuffmanTree HT,int n,int *s1,int *s2);
void HuffmanCoding(HuffmanTree HT,Tdata *w,int n);
void SelectMinTree(HuffmanTree HT,int n,int *s1,int *s2)
{
int i,min1,min2;
*s2=*s1=0;
min1=min2=MAX;
for(i=1; i<=n; i++)
{
if(HT[i].parent==0)
if(HT[i].weight
min2=min1;
min1=HT[i].weight;
*s2=*s1;
*s1=i;
}
else if(HT[i].weight
min2=HT[i].weight;
*s2=i;
}
}
}
void HuffmanCoding(HuffmanTree HT,Tdata *w,int n)
{
int m,s1,s2,start,c,f,i;
HTNode *p;
char *cd;
if(n<=1) return;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT+1,i=1; i<=n; i++,p++)
{
p->data=w[i].data;
p->weight=w[i].weight;
p->left=0;
p->right=0;
p->parent=0;
}
for(; i<=m; i++,p++)
{
p->weight=0;
p->left=0;
p->right=0;
p->parent=0;
}
for(i=n+1; i<=m; i++)
{
SelectMinTree(HT,i-1,&s1,&s2);
HT[s1].parent=i,HT[s2].parent=i;
HT[i].left=s1;
HT[i].right=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
cd=(char*)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1; i<=n; i++)
{
start=n-1;
for(c=i,f=HT[i].parent; f!=0; c=f,f=HT[f].parent)
if(HT[f].left==c)
cd[--start]='0';
else
cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
free(cd);
}
}
int main()
{
Tdata w[MAXSIZE];
int n,i;
cout<<" 请输入元素数:<4>";
cin>>n;
for(i=1; i<=n; i++)
{
cout<<" input"<
cin>>w[i].data>>w[i].weight;
}
cout<<" "<
for(i=1; i<=n; i++)
cout<
哈夫曼树的应用 输入元素 4 分别输入a 4 b 5 c 6 d 10 输出a--->110 b--->111 c--->10 d--->0#include #include #include #define MAXSIZE 30#define MAX 100using namespace std;typedef struct{char data;int weight;int parent;int left;int right;
真不容易啊,给你查出问题了.是申请和释放内存出错了.
把
free(cd);
放在 第二个for循环的外面即可.
#include
#include
#include
#define MAXSIZE 30
#define MAX 100
using namespace std;
typedef struct
{
char data;
int weight;
int parent;
int left;
int right;
} HTNode,*HuffmanTree;
typedef char **HuffmanCode;
typedef struct
{
char data;
int weight;
} Tdata;
HuffmanTree HT;
HuffmanCode HC;
void SelectMinTree(HuffmanTree HT,int n,int *s1,int *s2);
void HuffmanCoding(HuffmanTree HT,Tdata *w,int n);
void SelectMinTree(HuffmanTree HT,int n,int *s1,int *s2)
{
int i,min1,min2;
*s2=*s1=0;
min1=min2=MAX;
for(i=1; i<=n; i++)
{
if(HT[i].parent==0)
if(HT[i].weight
min2=min1;
min1=HT[i].weight;
*s2=*s1;
*s1=i;
}
else if(HT[i].weight
min2=HT[i].weight;
*s2=i;
}
}
}
void HuffmanCoding(HuffmanTree HT,Tdata *w,int n)
{
int m,s1,s2,start,c,f,i;
HTNode *p;
char *cd;
if(n<=1) return;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT+1,i=1; i<=n; i++,p++)
{
p->data=w[i].data;
p->weight=w[i].weight;
p->left=0;
p->right=0;
p->parent=0;
}
for(; i<=m; i++,p++)
{
p->weight=0;
p->left=0;
p->right=0;
p->parent=0;
}
for(i=n+1; i<=m; i++)
{
SelectMinTree(HT,i-1,&s1,&s2);
HT[s1].parent=i,HT[s2].parent=i;
HT[i].left=s1;
HT[i].right=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
cd=(char*)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1; i<=n; i++)
{
start=n-1;
for(c=i,f=HT[i].parent; f!=0; c=f,f=HT[f].parent)
if(HT[f].left==c)
cd[--start]='0';
else
cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}free(cd);//-------------->>>>>>>>>>>>>>>>>>>
}
int main()
{
Tdata w[MAXSIZE];
int n,i;
cout<<" 请输入元素数:<4>";
cin>>n;
for(i=1; i<=n; i++)
{
cout<<" input"<
cin>>w[i].data>>w[i].weight;
}
cout<<" "<
for(i=1; i<=n; i++)
cout<
}
0