2022年数据结构:哈夫曼树的应用 2.pdf
y?Y?11o1t?|?#include#include#include#includea#include#define MAXVALUE 200 /*?*/#define MAXBIT 30 /*?y*/#define MAXNODE 30 /*3?y*/struct haffnode char data;int weight;int flag;int parent;/*?*/int leftchild;/*o?*/int rightchild;/*o?*/;struct haffcode int bitMAXNODE;int start;/*?e?*/char data;int weight;/*?*/;/*o y?*/*/void pprintf(struct haffcode haffcode,int n);/*?3?o y*/void haffmantree(int weight,int n,struct haffnode hafftree,char data);/*?1t?*/void haffmancode(struct haffnode hafftree,int n,struct haffcode haffcode);/*?1t?*/void test(struct haffcode haffcode,int n);/*2a?o y*/名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 6 页 -void end();/*?o y*/*/void haffmantree(int weight,int n,struct haffnode hafftree,char data)/*?y?an?y?aweight?1t?*/int i,j,m1,m2,x1,x2;/*1t?hafftree3?n?12D2n-1?*/for(i=0;i2*n-1;i+)if(in)hafftreei.data=datai;hafftreei.weight=weighti;/*?*/else hafftreei.weight=0;/*?*/hafftreei.data=0;hafftreei.parent=0;/*3?D?*/hafftreei.flag=0;hafftreei.leftchild=-1;hafftreei.rightchild=-1;for(i=0;in-1;i+)/*11?1t?n-1?*/m1=m2=MAXVALUE;x1=x2=0;for(j=0;jn+i;j+)if(hafftreej.weightm1&hafftreej.flag=0)m2=m1;x2=x1;m1=hafftreej.weight;x1=j;else if(hafftreej.weightm2&hafftreej.flag=0)m2=hafftreej.weight;x2=j;hafftreex1.parent=n+i;hafftreex2.parent=n+i;hafftreex1.flag=1;hafftreex2.flag=1;hafftreen+i.weight=hafftreex1.weight+hafftreex2.weight;名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 6 页 -hafftreen+i.leftchild=x1;hafftreen+i.rightchild=x2;void haffmancode(struct haffnode hafftree,int n,struct haffcode haffcode)/*n?1t?hafftree113?1t?haffcode*/int i,j,child,parent;struct haffcode newcode;struct haffcode*cd;cd=&newcode;for(i=0;istart=MAXBIT-1;/*2?3?o?n-1*/cd-weight=hafftreei.weight;cd-data=hafftreei.data;/*?|?*/child=i;parent=hafftreechild.parent;while(parent!=0)if(hafftreeparent.leftchild=child)cd-bitcd-start=0;/*o?a0*/else cd-bitcd-start=1;/*o?a1*/cd-start-;child=parent;parent=hafftreechild.parent;for(j=cd-start+1;jbitj;haffcodei.data=cd-data;haffcodei.start=cd-start;haffcodei.weight=cd-weight;void pprintf(struct haffcode myhaffcode,int n)int i,j,count=0;clrscr();for(i=0;in;i+)textcolor(YELLOW);cprintf(?=%c,myhaffcodei.data);printf();textcolor(YELLOW);名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 6 页 -cprintf(weight=%3d,myhaffcodei.weight);printf();textcolor(YELLOW);cprintf(haffcode=);for(j=myhaffcodei.start+1;j=MAXNODE)printf(you input the data number=MAXNODE.);exit(1);for(i=0;istrlen(sstring);i+)for(j=0;jMAXBIT;j+)if(sstringi=haffcodej.data)k=j;break;if(kMAXNODE-1)printf(?3?D?2?%d?¥?n,i+1);continue;newhaffcodei.start=haffcodek.start;newhaffcodei.weight=haffcodek.weight;newhaffcodei.data=haffcodek.data;名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 6 页 -for(s=haffcodek.start+1;sMAXNODE)printf(you input the haffnode MAXNODE,so you input the data is wrong);printf(n);exit(1);clrscr();textcolor(YELLOW);cprintf(WELCOME!?a?1t?a);printf(n);cprintf(?D?DD?o?Y?D a?a?DD?);printf(n);cprintf(ao?3D?3?D?D?D?T?!);printf(n);getch();textcolor(YELLOW);cprintf(Ready?Enter,if you want to begin!n);printf(n);getch();cprintf(Now?a?Y?1t?.);getch();haffmantree(weight,n,myhafftree,data);haffmancode(myhafftree,n,myhaffcode);pprintf(myhaffcode,n);clrscr();printf(?DD?y?D?3D?.);if(ch=getch()=y|ch=Y)test(myhaffcode,n);getch();clrscr();end();getch();exit(1);名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 6 页 -