Huffmancoding huffman code

Huffmancoding huffman code
The function of this program is to encode English text(Othercharacters are ignored) using Huffmancoding.Because of the limitation of my ability,these isnoguaranteeto be perfect.Do nothesitate tomessage if you have any suggestions.Any feedback isappreciated.The program reads the in.txt (this file must be in the samedirectory with the program) and give a Huffman coding for everycharacters in code.txt.Note that,Huffman coding is not alwaysunique.Fallowing are codes in node.h defining the node of the Huffmantree:#ifndef _NODE_H#define _NODE_Hstruct node{char ch;long cnt;struct node *left;struct node *right;};#endifFallowing are codes of the main program:#include#include#include "node.h"int main(){void coding(struct node *root,char prefix[],FILE *fp);void test(struct node *root);struct node nodes[52];int i,j;char ch;//Initializationfor(i=0;i<52;i++){if(i<26){nodes[i].ch=i+97;}else{nodes[i].ch=i+39;}nodes[i].left=NULL;nodes[i].right=NULL;nodes[i].cnt=0;}FILE *infp;//open in.txtif((infp=fopen("in.txt","r"))==NULL){printf("cannot open filen");return 0;}//count the number of occurence of each characterwhile((ch=fgetc(infp))!=EOF){if(ch>='a'&&ch<='z'){nodes[ch-97].cnt+=1;}if(ch>='A'&&ch<='Z'){nodes[ch-39].cnt+=1;}}fclose(infp);//Ascending sort using Bubble sortfor(i=51;i>0;i--)for(j=0;j{if(nodes[j].cnt>nodes[j+1].cnt){long tempcnt=nodes[j].cnt;char tempch=nodes[j].ch;nodes[j].cnt=nodes[j+1].cnt;nodes[j].ch=nodes[j+1].ch;nodes[j+1].cnt=tempcnt;nodes[j+1].ch=tempch;}}//Construct Huffman treefor(i=0;i<51;i++){struct node *p1,*p2,*p3;p1=(struct node *)malloc(sizeof(struct node));p2=(struct node *)malloc(sizeof(struct node));p3=(struct node *)malloc(sizeof(struct node));if(nodes[i].left==NULL)p1->ch=nodes[i].ch;p1->cnt=nodes[i].cnt;p1->left=nodes[i].left;p1->right=nodes[i].right;if(nodes[i+1].left==NULL)p2->ch=nodes[i+1].ch;p2->cnt=nodes[i+1].cnt;p2->left=nodes[i+1].left;p2->right=nodes[i+1].right;p3->cnt=p1->cnt+p2->cnt;p3->left=p1;p3->right=p2;for(j=i+2;j<=52;j++){if(j==52){nodes[51].cnt=p3->cnt;nodes[51].left=p3->left;nodes[51].right=p3->right;break;}if(nodes[j].cntcnt){nodes[j-1].ch=nodes[j].ch;nodes[j-1].cnt=nodes[j].cnt;nodes[j-1].left=nodes[j].left;nodes[j-1].right=nodes[j].right;}else{nodes[j-1].cnt=p3->cnt;nodes[j-1].left=p3->left;nodes[j-1].right=p3->right;break;}}}FILE *codefp;//store results in code.txtif((codefp=fopen("code.txt","w"))==NULL){printf("cannot open code.txtn");return 0;}coding(&nodes[51],"",codefp);fclose(codefp);return 0;}
void coding(struct node *root,char prefix[],FILE *fp){char temp[52]="";char temp2[52]="";if(root->left==NULL){fprintf(fp,"%ct%sn",root->ch,prefix);}else{sprintf(temp,"%s%s",prefix,"0");coding(root->left,temp,fp);sprintf(temp2,"%s%s",prefix,"1");coding(root->right,temp2,fp);}}
Source code file is availablehere

  

爱华网本文地址 » http://www.aihuau.com/a/25101015/238073.html

更多阅读

1390清零错误communicationerror!Error code epson1390清零工具

当出现此提示时,说明打印机内部负责收集废墨的装置已满,需要清零,为什么清零还出现错误communicationerror!Error code,下面我们教大家,解决这个问题!第一步:点破解第二步:解压软件——点AdjProgok第三步:点选择1390清零错误communica

BTC-E各种充值提现方法的分析 OKpay 电汇 CODE btc e code

众所周知,果盘的各种坑,很多玩家都想找一个 值得信懒的平台,BTC-E貌视不错,不过就是充值和提现不太方便,现在将能用的渠道做一个分析。BTCE?提供了很多 提现 和冲值方式,基本上现在可行的方式有 OKpay 、?Egopay?、电汇、CODE码交易 ?这

如何查询swift code 工商银行swift code

如何查询swift code——简介SWIFT Code(银行国际代码)一般用于发电汇,接受电汇一般都需要SWIFT Code。外贸行业、外汇交易出金,国外广告联盟一般都采用电汇的方式支付。如何查询正确的SWIFT Code,将直接影响你是否能收到款项。网上有很多

什么是code-Behind技术?_zhuimengz z code

就是代码隐藏,在ASP.NET中通过ASPX页面指向CS文件的方法实现显示逻辑和处理逻辑的分离,这样有助于web应用程序的创建。比如分工,美工和编程的可以个干各的,不用再像以前asp那样都代码和html代码混在一起,难以维护。ASP.NET中的Code Behin

中国工商银行swiftcode大全_ICMarkets 工商swift code

外汇站提供的来自swift官网的最新数据,整理了一天才出来,现在分享一下。如果通过查询,没有找到所在的银行代码,可以按照以下规则查询。找不到低级分行,填写SwiftCode的时候,按照高一级的城市或省份工行代码填写。1、乡镇分行→县级市分行

声明:《Huffmancoding huffman code》为网友迷人的混蛋分享!如侵犯到您的合法权益请联系我们删除