哈夫曼树,c++。假设用于通信的电文仅由8个字母组成,
字母在电文中出现的频率分别为0.05,0.30,0.07,0.08,0.13,0.23,0.03,0.11。试为这8个字母设计哈夫曼编码。编写算法并上机验证。大概这到这里...
字母在电文中出现的频率分别为0.05,0.30,0.07,0.08,0.13,0.23,0.03,0.11。试为这8个字母设计哈夫曼编码。编写算法并上机验证。
大概这到这里,然后怎么把这个哈夫曼树给输出来,谢谢。
#include<iostream>
using namespace std;
struct element
{ int weight;
int parent,lchild,rchild;
};
void Select(element a[],int &s1,int &s2)
{
int t=15;
int i = 1;
s1 = s2 = 0;
a[0].weight = 65535;
while( i <= t )
{ //遍历查找权值最小的结点S1
if( a[i].parent == 0 && a[i].weight < a[s1].weight )
s1 = i;
i++;
}
i = 1;
while( i <= t )
{
//遍历查找除S1外权值最小的结点S2
if( i != s1 &&a[i].parent == 0 && a[i].weight <a[s2].weight )
s2 = i;
i++;
}
}
void HuffmanTree(element huffTree[], int w[ ], int n )
{
for (int i=0; i<2*n-1; i++) //初始化
{
huffTree [i].parent= -1;
huffTree [i].lchild= -1;
huffTree [i].rchild= -1;
}
for (i=0; i<n; i++) //构造n棵只含有根结点的二叉树
huffTree [i].weight=w[i];
int i1,i2;
for (int k=n; k<2*n-1; k++) //n-1次合并
{
Select(huffTree, i1, i2); //在huffTree中找权值最小的两个结点i1和i2
huffTree[i1].parent=k; //将i1和i2合并,则i1和i2的双亲是k
huffTree[i2].parent=k;
huffTree[k].weight= huffTree[i1].weight+huffTree[i2].weight;
huffTree[k].lchild=i1;
huffTree[k].rchild=i2;
}
}
int main()
{
const int m=8;
element ht[m];
int p[]={5,30,7,8,13,23,3,11};
char k[]={'a','b','c','d','e','f','g','h'};
HuffmanTree(ht,p,m);
return 0;
} 展开
大概这到这里,然后怎么把这个哈夫曼树给输出来,谢谢。
#include<iostream>
using namespace std;
struct element
{ int weight;
int parent,lchild,rchild;
};
void Select(element a[],int &s1,int &s2)
{
int t=15;
int i = 1;
s1 = s2 = 0;
a[0].weight = 65535;
while( i <= t )
{ //遍历查找权值最小的结点S1
if( a[i].parent == 0 && a[i].weight < a[s1].weight )
s1 = i;
i++;
}
i = 1;
while( i <= t )
{
//遍历查找除S1外权值最小的结点S2
if( i != s1 &&a[i].parent == 0 && a[i].weight <a[s2].weight )
s2 = i;
i++;
}
}
void HuffmanTree(element huffTree[], int w[ ], int n )
{
for (int i=0; i<2*n-1; i++) //初始化
{
huffTree [i].parent= -1;
huffTree [i].lchild= -1;
huffTree [i].rchild= -1;
}
for (i=0; i<n; i++) //构造n棵只含有根结点的二叉树
huffTree [i].weight=w[i];
int i1,i2;
for (int k=n; k<2*n-1; k++) //n-1次合并
{
Select(huffTree, i1, i2); //在huffTree中找权值最小的两个结点i1和i2
huffTree[i1].parent=k; //将i1和i2合并,则i1和i2的双亲是k
huffTree[i2].parent=k;
huffTree[k].weight= huffTree[i1].weight+huffTree[i2].weight;
huffTree[k].lchild=i1;
huffTree[k].rchild=i2;
}
}
int main()
{
const int m=8;
element ht[m];
int p[]={5,30,7,8,13,23,3,11};
char k[]={'a','b','c','d','e','f','g','h'};
HuffmanTree(ht,p,m);
return 0;
} 展开
1个回答
推荐于2016-04-04
展开全部
#include "stdio.h"
#include "conio.h"
main( )
{
long a,b,c,d,e,x;
scanf("%ld",&x);
a=x/10000;/*分解出万位*/
b=x%10000/1000;/*分解出千位*/
c=x%1000/100;/*分解出百位*/
d=x%100/10;/*分解出十位*/
e=x%10;/*分解出个位*/
if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
else if (e!=0) printf(" there are 1,%ld\n",e);
getch();
}
#include "conio.h"
main( )
{
long a,b,c,d,e,x;
scanf("%ld",&x);
a=x/10000;/*分解出万位*/
b=x%10000/1000;/*分解出千位*/
c=x%1000/100;/*分解出百位*/
d=x%100/10;/*分解出十位*/
e=x%10;/*分解出个位*/
if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
else if (e!=0) printf(" there are 1,%ld\n",e);
getch();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询