1、算法设计与分析实验报告贪心算法班级: 2013156 学号: 201315614 姓名:张 春 阳 哈夫曼编码代码#includefloat small1,small2;int flag1,flag2,count;typedef struct HuffmanTreefloat weight;int lchild,rchild,parent;huffman;huffman huffmantree100;void CreatHuffmanTree(int n,int m)int i;void select();printf(请输入%d个节点的权值:,n);for(i=0;in;i+)scanf(%
2、f,&huffmantreei.weight);printf(n);for(i=0;im;i+)huffmantreei.lchild=-1;huffmantreei.rchild=-1;huffmantreei.parent=-1;for(count=n;countm;count+)select();huffmantreeflag1.parent=count;huffmantreeflag2.parent=count;huffmantreecount.weight=small1+small2;huffmantreecount.lchild=flag1;huffmantreecount.rch
3、ild=flag2;void select()int i,a,b;float stemp;int ftemp;a=0;b=0;for(i=0;ismall2)stemp=small1;small1=small2;small2=stemp;ftemp=flag1;flag1=flag2;flag2=ftemp;for(i=0;icount;i+)if(huffmantreei.parent=-1)if(flag1!=i)&(flag2!=i)if(huffmantreei.weightsmall2)stemp=small1;small1=small2;small2=stemp;ftemp=flag1;flag1=flag2;flag2=ftemp;void huffmancode(int n)int a100;int j,k,i,c;for(i=0;i-1;c-) printf(%d,ac);printf(n);void main()int n,m;printf(请输入一共有几个节点:);scanf(%d,&n);m=2*n-1;CreatHuffmanTree(n,m);huffmancode(n);截图