hufuman 编码.docVIP

  • 3
  • 0
  • 约9.87千字
  • 约 9页
  • 2017-06-04 发布于河南
  • 举报
hufuman 编码

/***************************************************/??? /*????项目名称:霍夫曼编码算法?????*/??? ????????????????????????????????????? /***************************************************/??? #include?iostream.h? ?? #include?vector? ?? #include?string? ?? #include?stdlib.h? ?? ??? using?namespace?std;??? ??? ??? //存储霍夫曼树节点的数据结构? ?? typedef?struct?treenode??? {??? ????char?data;????????????????????//存储要编码的字符? ?? ????int?weight;???????????????????//存储要编码的字符的权重? ?? ????struct?treenode*?parent;????//父节点? ?? ????struct?treenode*?lchild;????//左子树? ?? ????struct?treenode*?rchild;????//右子树? ?? }TreeNode;??? ??? //存储霍夫曼码表单元的数据结构? ?? typedef?struct?nodecode??? {??? ????char?node;?????//存储要编码的字符? ?? ????string?huffmancode;//存储相应字符的霍夫曼码? ?? }Nodecode;??? ??? //定义全局变量? ?? ?static?int?N;??//记录文件中出现的字符总数? ?? ?static?nodecode?s[30];//计算出的霍夫曼码表? ?? ?static?int?index=0;???? /*******************************************************************/??? /*函数功能:获得字符cc在文件中出现的次重,以此作为权重?? /******************************************************************/??? int?GetCount(FILE*?fp,char?cc)??? {??? ????int?i=1;??? ????while(!feof(fp))??? ????{??? ????????if(cc==fgetc(fp))??? ????????????i++;??? ????}??? ????rewind(fp);??? ????return?i;??? }??? /******************************************************************************/??? //函数功能:判断vector中是否已存在指定字符为cc的treenode? ?? /******************************************************************************/??? int?IsExit(vectorTreeNode*?vec,char?cc)??? {??? ????for(int?i=0;ivec.size();i++)??? ????{??? ????????if(cc==vec[i]-data)??? ????????????return?1;??? ????}??? ????return?0;??? }??? /******************************************************************************/??? //函数功能:从vector中选择权重最小的两个节点lnode,? ?? //?????????rnode,并将其从vector中移除? ?? /******************************************************************************/??? vectorTreeNode*?Select(vectorTreeNode*?vec,TreeNode**?lnode,TreeNode**?rnode)??? {??? ????int?temp=vec[0]-weight;??? ????vectorTreeNode*::iterator?i

文档评论(0)

1亿VIP精品文档

相关文档