1.赫夫曼编码的设计与实现(C++)
哈弗曼树的构造和编码#include
2.赫夫曼编码的设计与实现(C++)
哈弗曼树的构造和编码#include
3.急
#include<string.h> #include<stdlib.h> #include<stdio.h> int m,s1,s2; typedef struct { unsigned int weight; unsigned int parent,lchild,rchild; }HTNode,*HuffmanTree; //动态分配数组存储哈夫曼树 typedef char *HuffmanCode; //动态分配数组存储哈夫曼编码表 void Select(HuffmanTree HT,int n) { int i,j; for(i = 1;i <= n;i++) if(!HT[i].parent){s1 = i;break;} for(j = i+1;j <= n;j++) if(!HT[j].parent){s2 = j;break;} for(i = 1;i <= n;i++) if((HT[s1].weight>HT[i].weight)&&(!HT[i].parent)&&(s2!=i))s1=i; for(j = 1;j <= n;j++) if((HT[s2].weight>HT[j].weight)&&(!HT[j].parent)&&(s1!=j))s2=j; } void HuffmanCoding(HuffmanTree &HT, HuffmanCode HC[], int *w, int n) { // 算法6.13 // w存放n个字符的权值(均>0),构造哈夫曼树HT, // 并求出n个字符的哈夫曼编码HC int i, j; char *cd; int p; int cdlen; if (n<=1) return; m = 2 * n - 1; HT = (HuffmanTree)malloc((m+1) * sizeof(HTNode)); // 0号单元未用 for (i=1; i<=n; i++) { //初始化 HT[i].weight=w[i-1]; HT[i].parent=0; HT[i].lchild=0; HT[i].rchild=0; } for (i=n+1; i<=m; i++) { //初始化 HT[i].weight=0; HT[i].parent=0; HT[i].lchild=0; HT[i].rchild=0; } puts("\n哈夫曼树的构造过程如下所示:"); printf("HT初态:\n 结点 weight parent lchild rchild"); for (i=1; i<=m; i++) printf("\n%4d%8d%8d%8d%8d",i,HT[i].weight, HT[i].parent,HT[i].lchild, HT[i].rchild); printf(" 按任意键,继续 。
"); getchar(); for (i=n+1; i<=m; i++) { // 建哈夫曼树 // 在HT[1..i-1]中选择parent为0且weight最小的两个结点, // 其序号分别为s1和s2。 Select(HT, i-1); HT[s1].parent = i; HT[s2].parent = i; HT[i].lchild = s1; HT[i].rchild = s2; HT[i].weight = HT[s1].weight + HT[s2].weight; printf("\nselect: s1=%d s2=%d\n", s1, s2); printf(" 结点 weight parent lchild rchild"); for (j=1; j<=i; j++) printf("\n%4d%8d%8d%8d%8d",j,HT[j].weight, HT[j].parent,HT[j].lchild, HT[j].rchild); printf(" 按任意键,继续 。
"); getchar(); } //------无栈非递归遍历哈夫曼树,求哈夫曼编码 cd = (char *)malloc(n*sizeof(char)); // 分配求编码的工作空间 p = m; cdlen = 0; for (i=1; i<=m; ++i) // 遍历哈夫曼树时用作结点状态标志 HT[i].weight = 0; while (p) { if (HT[p].weight==0) { // 向左 HT[p].weight = 1; if (HT[p].lchild != 0) { p = HT[p].lchild; cd[cdlen++] ='0'; } else if (HT[p].rchild == 0) { // 登记叶子结点的字符的编码 HC[p] = (char *)malloc((cdlen+1) * sizeof(char)); cd[cdlen] ='\0'; strcpy(HC[p], cd); // 复制编码(串) } } else if (HT[p].weight==1) { // 向右 HT[p].weight = 2; if (HT[p].rchild != 0) { p = HT[p].rchild; cd[cdlen++] ='1'; } } else { // HT[p].weight==2,退回退到父结点,编码长度减1 HT[p].weight = 0; p = HT[p].parent; --cdlen; } } } // HuffmanCoding void main() { HuffmanTree HT;HuffmanCode *HC;int *w,n,i; puts("输入结点数:"); scanf("%d",&n); HC = (HuffmanCode *)malloc(n*sizeof(HuffmanCode)); w = (int *)malloc(n*sizeof(int)); printf("输入%d个结点的权值\n",n); for(i = 0;i < n;i++) scanf("%d",&w[i]); HuffmanCoding(HT,HC,w,n); puts("\n各结点的哈夫曼编码:"); for(i = 1;i <= n;i++) printf("%2d(%4d):%s\n",i,w[i-1],HC[i]); getchar(); }。
4.C++课程设计:哈夫曼编码器
#include
.." << endl; FILE *htmTree; char r[] = { ' ', '\0' }; if ((htmTree = fopen("htmTree.txt", "w")) == NULL) { cout << "can not open file" << endl; return ; } fputs(z, htmTree); for (i = 0; i < n + 1; i++) { fprintf(htmTree, "%6d", *(w + i)); fputs(r, htmTree); } for (i = 1; i <= n; i++) { fputs(HC[i], htmTree); fputs(r, htmTree); } fclose(htmTree); cout << "Characters and corresponding coding has written to the root directory file htmTree.txt中" << endl << endl;}void InputCode(){ //cout<<" Please input your want to code of characters "<
5.哈夫曼编码设计源代码
#include<iostream>using namespace std;typedef struct { int weight; int parent; int lchild; int rchild; }HTreeNode,*HTree;void createHTree(HTree *t ,int * w, int n ){ void select(HTree t, int i, int *s1, int *s2); *t = new HTreeNode[2*n-1]; for(int i=0;i<n;i++){ (*t)[i].weight = w[i]; (*t)[i].parent = 0; (*t)[i].lchild = 0; (*t)[i].rchild = 0; } for(int i=n;i<2*n-1;i++){ (*t)[i].weight = 0; (*t)[i].parent = 0; (*t)[i].lchild = 0; (*t)[i].rchild = 0; } int s1,s2; for(int i=n;i<2*n-1;i++){ select (*t, i,&s1,&s2); (*t)[i].weight=(*t)[s1].weight+(*t)[s2].weight; (*t)[i].lchild = s1; (*t)[i].rchild = s2; (*t)[s1].parent = i; (*t)[s2].parent = i; } for(int i=0;i<2*n-1;i++){ printf("%-3d%4d%4d%4d%4d\n",i,(*t)[i].weight,(*t)[i].parent,(*t)[i].lchild,(*t)[i].rchild); }}void select(HTree t, int i, int *s1, int *s2){ for(int j=0;j<i;j++){ if(t[j].parent==0){*s1=j; break;} } for(int j=*s1;j<i;j++){ if( t[j].parent==0 && t[j].weight<t[*s1].weight ){ *s1 = j; } }///////////////////////////////////////// for(int j=0;j<i;j++){ if(t[j].parent==0 && j!=*s1){*s2=j; break;} } for(int j=*s2;j<i;j++){ if(t[j].parent==0 && t[j].weight<t[*s2].weight && j!=*s1){ *s2 = j; } } int temp; if(*s1>*s2){ temp = *s1; *s1= *s2; *s2=temp; }}void encode(HTree t, int n){ int temp,temp2; string s = ""; for(int i = 0 ;i<n;i++){ printf("weight: %-4d",t[i].weight); temp = i; s=""; while(t[temp].parent !=0){ temp2 = temp; temp = t[temp].parent; if(t[temp].lchild == temp2) s.append("0"); if(t[temp].rchild == temp2) s.append("1"); } string::reverse_iterator it = s.rbegin(); while(it != s.rend()){ cout<<*it; it++; } cout<<endl; } }int main(){ int w[8] ={5,29,7,8,14,23,3,11}; int n = 8; HTree t = NULL; createHTree( &t,w,n); cout<<endl; encode(t,n); system("PAUSE"); }。
6.题目:哈夫曼编码系统 设计任务:
#include
\n\n"; return; } LeafNum=Num; Node=new HuffmanNode[2*Num-1]; for(int i=0;i
\n"; return; } fop.write((char*)&Num,sizeof(Num)); //先写入哈夫曼树的叶子结点个数 for(int n=0;n<2*Num-1;n++) { //最后写入哈夫曼树的各个结点(存储在Node[]中) fop.write((char*)&Node[n],sizeof(Node[n])); flush(cout); } fop.close(); //关闭文件 cout<<"\n哈夫曼树已成功写入hfmTree.dat文件。\n"; } ////////////////////////////////////////////////////////////////////////////// // 从文件建立哈夫曼树函数 // 函数功能:从文件建立哈夫曼树 //函数参数:无 //参数返回值:无 void HuffmanTree::(){ ifstream fip; fip.open("。
7.哈夫曼编码类的设计并实现包括以下功能
#include
8.题目:哈夫曼编码系统 设计任务:
#include
\n\n"; return; } LeafNum=Num; Node=new HuffmanNode[2*Num-1]; for(int i=0;i
\n"; return; } fop.write((char*)&Num,sizeof(Num)); //先写入哈夫曼树的叶子结点个数 for(int n=0;n<2*Num-1;n++) { //最后写入哈夫曼树的各个结点(存储在Node[]中) fop.write((char*)&Node[n],sizeof(Node[n])); flush(cout); } fop.close(); //关闭文件 cout<<"\n哈夫曼树已成功写入hfmTree.dat文件。\n";}//////////////////////////////////////////////////////////////////////////////// 从文件建立哈夫曼树函数// 函数功能:从文件建立哈夫曼树//函数参数:无//参数返回值:无void HuffmanTree::(){ ifstream fip; fip.open("hfmTree.dat",ios::binary|ios::in); if(fip.fail()) { cout<<。
9.哈夫曼编码设计源代码
#include