标签:
借鉴的博客:
http://baike.sogou.com/v111784.htm
http://blog.csdn.net/cqnuztq/article/details/8919204
http://blog.csdn.net/pony_maggie/article/details/41620389
http://blog.csdn.net/JavaAlpha/article/details/4222845
实现代码:
1 #include <iostream> 2 #define MAXVALUE 100000 3 using namespace std; 4 const int n=4;//叶子节点个数 5 string l; 6 int size; 7 //构造哈夫曼树结点 8 typedef struct{ 9 int weight;//权值 10 int parent;//父节点 11 int lchild;//左子树 12 int rchild;//右子树 13 }HNodeType; 14 15 HNodeType HFMTree[2*n-1];//结点数 16 17 //构造哈夫曼编码数组 18 typedef struct{ 19 int bit[n]; 20 int start; 21 }HCodeType; 22 23 HCodeType HFMCode[n]; 24 //创建哈夫曼树 25 void createHFMTree(HNodeType HFMTree[],int n){ 26 int m1,x1,m2,x2; 27 int i,j; 28 //初始化 29 for(i=0;i<2*n-1;i++){ 30 HFMTree[i].weight=0; 31 HFMTree[i].parent=-1; 32 HFMTree[i].lchild=-1; 33 HFMTree[i].rchild=-1; 34 } 35 cout<<"*******************哈夫曼树字符串最优转码程序***********************"<<endl; 36 cout<<"请输入一个字符串:(只含有A,B,C,D四种字符,输入回车结束)"<<endl; 37 cin>>l; 38 string str(l); 39 size=str.size(); 40 for(int i=0;i<size;++i){ 41 if(str.at(i)==‘A‘)HFMTree[0].weight++; 42 else if(str.at(i)==‘B‘)HFMTree[1].weight++; 43 else if(str.at(i)==‘C‘)HFMTree[2].weight++; 44 else if(str.at(i)==‘D‘)HFMTree[3].weight++; 45 else{ 46 cout<<"输入有误!"<<endl; 47 break; 48 } 49 } 50 51 for(i=0;i<n-1;i++){ 52 x1=x2=MAXVALUE; 53 m1=m2=0; 54 for(j=0;j<n+i;j++){ 55 if(HFMTree[j].parent==-1&&HFMTree[j].weight<x1){ 56 x2=x1; 57 m2=m1; 58 x1=HFMTree[j].weight; 59 m1=j; 60 } 61 else if(HFMTree[j].parent==-1&&HFMTree[j].weight<x2){ 62 x2=HFMTree[j].weight; 63 m2=j; 64 } 65 } 66 HFMTree[m1].parent=n+i;HFMTree[m2].parent=n+i; 67 HFMTree[n+i].weight=HFMTree[m1].weight+HFMTree[m2].weight; 68 HFMTree[n+i].lchild=m1; 69 HFMTree[n+i].rchild=m2; 70 } 71 } 72 //转化编码 73 void createHFMCode(HNodeType HFMTree[],HCodeType HFMCode[]){ 74 HCodeType cd; 75 int i,j,c,p; 76 for(i=0;i<n;i++){ 77 cd.start=n-1; 78 c=i; 79 p=HFMTree[c].parent; 80 while(p!=-1) 81 { 82 if(HFMTree[p].lchild==c)cd.bit[cd.start]=0; 83 else cd.bit[cd.start]=1; 84 cd.start--; 85 c=p; 86 p=HFMTree[c].parent; 87 } 88 for(j=cd.start+1;j<n;j++) 89 HFMCode[i].bit[j]=cd.bit[j]; 90 HFMCode[i].start=cd.start+1; 91 } 92 } 93 //主函数 94 int main() 95 { 96 int i,j; 97 //创建树 98 createHFMTree(HFMTree,n); 99 //转码 100 createHFMCode(HFMTree,HFMCode); 101 cout<<endl; 102 int k=65; 103 for(i=0;i<n;i++) 104 { 105 cout<<(char)k<<"的编码:"; 106 for(j=HFMCode[i].start;j<=n-1;j++) 107 { 108 cout<<HFMCode[i].bit[j]; 109 } 110 k++; 111 cout<<endl; 112 } 113 cout<<"转码后的字符串为:"<<endl; 114 string str(l); 115 size=str.size(); 116 for(int i=0;i<size;++i){ 117 if(str.at(i)==‘A‘){ 118 for(j=HFMCode[0].start;j<=n-1;j++) 119 cout<<HFMCode[0].bit[j]; 120 } 121 else if(str.at(i)==‘B‘){ 122 for(j=HFMCode[1].start;j<=n-1;j++) 123 cout<<HFMCode[1].bit[j]; 124 } 125 else if(str.at(i)==‘C‘){ 126 for(j=HFMCode[2].start;j<=n-1;j++) 127 cout<<HFMCode[2].bit[j]; 128 } 129 else if(str.at(i)==‘D‘){ 130 for(j=HFMCode[3].start;j<=n-1;j++) 131 cout<<HFMCode[3].bit[j]; 132 } 133 else{ 134 cout<<"输入有误!"<<endl; 135 break; 136 } 137 } 138 return 0; 139 }
标签:
原文地址:http://www.cnblogs.com/wangmengmeng/p/4857297.html