// ThisIsMyCPP_Alternate.cpp : Defines the entry point for the console application. //HuffmanTree《数据结构》.严蔚敏.吴伟民.P147 //电子批阅系统.Huffman 树 //如有BUG,联系邮箱:dabbysunshine@qq.com ^@^ /// #include "stdafx.h" #include "stdlib.h" #include "stdio.h" #include <iostream> using namespace std; typedef char ** HuffmanCode; typedef struct { char *Character; HuffmanCode HC; int Array_Weight[26]; int Length; }Huff_Pro; void InitHuff_Pro(Huff_Pro &Huff_Sol); void CreateHuff_Pro(Huff_Pro &Huff_Sol,int Amount); void PrintHuff_Pro(Huff_Pro Huff_Sol); void DestoryHC(HuffmanCode &HC,int Length); void DestoryHuff_Pro(Huff_Pro &Huff_Sol); typedef struct { int Weight; int parent,lchild,rchild; }HuffmanTreeNode,*HuffmanTree; void Select_Two_Min(HuffmanTree HT,int m,int &min1,int &min2); void InitHuffmanTree(HuffmanTree &HT,int Length,int *Array_Weight); void CreateHC(HuffmanCode &HC,HuffmanTree HT,int Length); int _tmain(int argc, _TCHAR* argv[]) { int Amount = 0; Huff_Pro Huff_Sol; HuffmanTree HT = NULL; //scanf("%d",&Amount); cin>>Amount; getchar(); InitHuff_Pro(Huff_Sol); CreateHuff_Pro(Huff_Sol,Amount);//未完全创造 InitHuffmanTree(HT,Huff_Sol.Length,Huff_Sol.Array_Weight); CreateHC(Huff_Sol.HC,HT,Huff_Sol.Length); PrintHuff_Pro(Huff_Sol); DestoryHuff_Pro(Huff_Sol); system("PAUSE"); return 0; } void InitHuff_Pro(Huff_Pro &Huff_Sol) { Huff_Sol.Character = NULL; Huff_Sol.HC = NULL; Huff_Sol.Length = 0; } void CreateHuff_Pro(Huff_Pro &Huff_Sol,int Amount) { int Data[26] = {0},max = 0,iTemp = 0; char s[27]; char ch; for(int i = 0;i < Amount;i++) { ch = getchar(); Data[ch - 'A']++; getchar(); } for(int i = 0;i < 26;i++) { for(int j = 0; j < 26;j++) { if(max < Data[j]) { max = Data[j]; iTemp = j; } } if(max == 0) { s[i] = '/0'; break; } else { s[i] = (char)(iTemp + 'A'); Data[iTemp] = 0; Huff_Sol.Array_Weight[i] = max; max = 0; } } Huff_Sol.Length = strlen(s); Huff_Sol.Character = (char*)malloc(strlen(s) + 1); strcpy(Huff_Sol.Character,s); } void PrintHuff_Pro(Huff_Pro Huff_Sol) { for(int i = 0;i < Huff_Sol.Length;i++) { cout<<Huff_Sol.Character[i]<<' '<<Huff_Sol.Array_Weight[i]<<' '<<Huff_Sol.HC[i + 1]<<endl;//这里遇到了一个BUG^-^ // cout<<Huff_Sol.Character[i]<<' '<<Huff_Sol.Array_Weight[i]<<endl; } } void DestoryHC(HuffmanCode &HC,int Length) { for(int i = 1;i <= Length;i++) { free(HC[i]); } free(HC); HC = NULL; } void DestoryHuff_Pro(Huff_Pro &Huff_Sol) { free(Huff_Sol.Character); Huff_Sol.Character = NULL; DestoryHC(Huff_Sol.HC,Huff_Sol.Length); Huff_Sol.Length = 0; } void InitHuffmanTree(HuffmanTree &HT,int Length,int *Array_Weight) { if(Length < 1) { exit(EXIT_FAILURE); } int m = Length * 2 - 1; int i = 0; int min1,min2; HT = (HuffmanTree)calloc(m + 1,sizeof(HuffmanTreeNode)); for(i = i + 1; i <= Length;i++) { HT[i].lchild = 0; HT[i].parent = 0; HT[i].rchild = 0; HT[i].Weight = Array_Weight[i - 1]; } for(;i <= m; i++) { HT[i].lchild = 0; HT[i].parent = 0; HT[i].rchild = 0; HT[i].Weight = 0; } for(i = Length + 1;i <=m;i++) { Select_Two_Min(HT,i - 1,min1,min2); //cout<<min1<<' '<<min2<<endl; HT[min1].parent = i; HT[min2].parent = i; HT[i].lchild = min1; HT[i].rchild = min2; HT[i].Weight = HT[min1].Weight + HT[min2].Weight; //cout<<HT[i].Weight<<endl; } } void Select_Two_Min(HuffmanTree HT,int m,int &min1,int &min2) { //这一段代码要注意,不完善,有BUG. ///Where is BUG?找不到呀~! int i =1 , iTemp = 0; while(HT[i].parent != 0) { i++; if(i > m) { exit(EXIT_FAILURE); } } iTemp = i; i++; while(HT[i].parent != 0 && i <= m) { i++; if(i > m) { exit(EXIT_FAILURE); } } if(HT[iTemp].Weight < HT[i].Weight) { min1 = iTemp; min2 = i; } else { min2 = iTemp; min1 = i; } for(i++;i <= m;i++) { if(HT[i].parent == 0 && HT[i].Weight <= HT[min1].Weight ) { min2 = min1; min1 = i; } } } /** void Select_Two_Min(HuffmanTree HT,int m,int &min1,int &min2) { //这段代码也有问题,不过比上面的要好一些(仅针对此题) int min = 0,iTemp,i; for(i = 1;i <= m;i++) { if(HT[i].parent == 0 && HT[i].Weight != 0) { if(min == 0 || min > HT[i].Weight) { min = HT[i].Weight; iTemp = i; } } } min1 = iTemp; HT[iTemp].Weight = 0; min = 0; for(i = 1;i <= m;i++) { if(HT[i].parent == 0 && HT[i].Weight != 0) { if(min == 0 || min > HT[i].Weight) { min = HT[i].Weight; iTemp = i; } } } min2 = iTemp; } **/ void CreateHC(HuffmanCode &HC,HuffmanTree HT,int Length) { int i ,start,c,f; char *cd; cd = (char*)malloc(Length*sizeof(char)); cd[Length - 1] = '/0'; HC = (HuffmanCode)malloc((Length + 1)*sizeof(char*)); for(i = 1;i <= Length;++i) { start = Length - 1; for(c = i,f = HT[i].parent;f != 0;c = f,f = HT[f].parent) { if(HT[f].lchild == c) { cd[--start] = '0'; } else { cd[--start] = '1'; } } HC[i] = (char*)malloc(strlen(&cd[start]) + 1); strcpy(HC[i],&cd[start]); //puts(HC[i]); } } 有错误的,但却很好!