//#include #define Change(x,y) (x)^=(y)^=(x)^=(y); struct HNode{ int weight; int parent; int LChild; int RChild; char data; bool operator<(HNode & r){ if(this->parent != -1) return false; if(r.parent != -1) return true; return (this->weight < r.weight); } }; struct HCode{ char data; char code[100]; bool operator==(char &_in){ return this->data == _in;} }; class Huffman{ private: HNode *HTree; // 哈弗曼树 HCode *HCodeTable; // 哈弗曼编码表 void SelectMin(int &x,int &y,int begin,int end); // 找两个最小值 void Reveres(char a[]); // 字符串倒置 public: void CreateHTree(const int a[],const char *b,int n); void CreateCodeTable(int n); void Encode(char *s,char *b); void Decode(char *s,char *b); explicit Huffman(const char *a); HCode *GetHCodeTable(){return HCodeTable;} HNode *GetHTree(){return HTree;} ~Huffman(); }; void Huffman::SelectMin(int &x, int &y, int begin, int end){ x = begin-1;while (HTree[++x].parent !=-1); y = x;while (HTree[++y].parent !=-1); if(HTree[y] < HTree[x]) Change(x,y); for(int i = x;i < end;i++) if(HTree[i] < HTree[x]) x = i; for(int i = y;i < end;i++) if(HTree[i] < HTree[y] && i != x) y = i; } void Huffman::Reveres(char *a) { int l = 0; while (a[++l] != '\0'); for(int i = 0;i #include #include #include #ifdef WIN32 #define CLEAR "cls" #elif defined(linux) #define CLEAR "clear" #endif using namespace std; void printTheHuffmanTree(Huffman &_in); void printTheHuffmanCodeTable(Huffman &_in); void DEplay(Huffman &_in); int main(){ system(CLEAR); cout <<"+----------------------------+"<> a; cin.get(a,100); Huffman Huffman1(a); char b[800]={'\0'}; char c[100]={'\0'}; int nb = -1,nc = -1; Huffman1.Encode(a,b); Huffman1.Decode(b,c); while(b[++nb] != '\0'); while(c[++nc] != '\0'); system(CLEAR); cout <<"+----------------------------+"<> chouse; switch(chouse){ case 0: printTheHuffmanTree(Huffman1); break; case 1: printTheHuffmanCodeTable(Huffman1); break; case 2: DEplay(Huffman1); break; case 3: return 0; default: goto goback; } goto goback; } void printTheHuffmanTree(Huffman & _in){ HNode * root = _in.GetHTree(); int a = -1; HNode * Hlist = root; while (Hlist[++a].parent != -1); root = &Hlist[a]; HNode * list[100][100]; list[0][0] = root; int deep = 0; bool end; do{ deep++; for(int bh = 0;bhLChild != -1) list[deep][bh*2] = &Hlist[list[deep-1][bh]->LChild]; if(list[deep-1][bh]->RChild != -1) list[deep][bh*2+1] = &Hlist[list[deep-1][bh]->RChild]; } } end = true; for(int bh = 0;bhdata == '\0') cout << list[n][bh]->weight ; else cout << list[n][bh]->data; } } cout << endl; for(int bh = 0;bh < pow(2,n);bh++) { if(list[n][bh] && list[n][bh]->data == '\0') cout << setw(soot[deep-n-1]+1) << setfill('-') << setiosflags(ios::left) << '|' << setw(soot[deep-n-1]+1) << setfill(' ') << setiosflags(ios::left) << '+'; else cout << setw(2*(soot[deep-n-1]+1)) << setfill(' ') << ' '; } cout << endl; for(int bh = 0;bh < pow(2,n);bh++) { if(list[n][bh] && list[n][bh]->data == '\0') cout << setw(soot[deep-n-1]+1) << setfill(' ') << setiosflags(ios::left) << '|' << setw(soot[deep-n-1]+1) << setfill(' ') << setiosflags(ios::left) << '|'; else cout << setw(2*(soot[deep-n-1]+1)) << setfill(' ') << ' '; } cout << endl; } delete[] soot; cout<< "按回车回到主菜单"; cin.get();cin.get(); } /* * 0 0 * |-----+ |-----+ * | | | | * 1 1 1 1 * |--+ |--+ |--+ |--+ * | | | | | | | | * 2 2 2 2 2 2 2 2 */ void printTheHuffmanCodeTable(Huffman &_in){ HCode * table = _in.GetHCodeTable(); system(CLEAR); cout << "+------+----------+"<> i; char a[800]; char b[100]; if(i == 2){ cout << "请输入需要解码的字符:"; cin >> a; _in.Decode(a,b); cout << "解码完毕 结果是:"<> b; _in.Encode(b,a); cout << "编码完毕 结果为:"<< endl; cout << a<