#include <iostream>#include<iomanip>#include<string>using namespaceStd;typedefstruct{ stringname; intweight; intparent, Lchild, Rchild; intvisited;//set the visited option to indicate a deletion after a minimum weight is found, 0 means not deleted, and 1 for delete}htnode,*Huffmantree;intMin (Huffmantree HT,intm) { intmin; for(intI=1; i<=m;i++)//Select the first weight as the smallest weight in a node that has not been visited { if(ht[i].visited==0) {min=i; Break; } } for(intI=1; i<=m;i++)//The minimum weight is calculated by comparing the selected minimum weight with other unused weights .{//in this error, assign the value of I to Min, causing the loop to assign an error to Min. To assign a value to min outside the loop if(ht[i].visited==0&&ht[min].weight>ht[i].weight) {min=i; } } //output min, verify that the location of the minimum weight is foundHt[min].visited=1; returnmin;}intMain () {Huffmantree ht; //global variables, defining Huffman trees string*HC;//global variables, defining Huffman coding intht1=0, HC1=0; intchoice; intm, N; while(1) { if(ht1==0&&hc1==0) {cout<<"1. Establish Huffman Tree"<<Endl; cout<<"---------------------------------------------------------------------------"<<Endl; } if(ht1==1&&hc1==0) {cout<<"1. Establishment Huffman Tree 2. Huffman Tree Coding"<<Endl; cout<<"---------------------------------------------------------------------------"<<Endl; } if(ht1==1&&hc1==1) {cout<<"1. Establish Huffman Tree 2 Huffman tree Code 3. Code 4. Decoding 5. End"<<Endl; cout<<"---------------------------------------------------------------------------"<<Endl; } cout<<"Please enter your choice:"; CIN>>choice; if(choice==1) {cout<<"Please enter the number of points:"; CIN>>N; M=2*n-1; HT=Newhtnode[m+1]; for(intI=1; i<=m;i++) {Ht[i].lchild=0; Ht[i].rchild=0; Ht[i].parent=0; Ht[i].visited=0; } cout<<"Please enter the appropriate information and weights for these points:"<<Endl; for(intI=1; i<=n;i++) {cin>>Ht[i].name; CIN>>Ht[i].weight; } intS1, S2; for(inti=n+1; i<=m;i++) {S1=min (ht,i-1); S2=min (ht,i-1); //the position of the output minimum weight, the validation algorithm is correctHt[s1].parent=i; Ht[s2].parent=i; Ht[i].lchild=S1; Ht[i].rchild=S2; Ht[i].weight=ht[s1].weight+Ht[s2].weight; } ht1=1; cout<<"Huffman Tree Construction success!"<<Endl; cout<<Endl; } Else if(choice==2) { stringstr; HC=New string[n+1]; Char*cd; CD=New Char[n]; Cd[n-1]=' /'; intstart, F, C; for(intI=1; i<=n;i++) {Start=n-1; C=i; F=ht[i].parent; while(f!=0) { --start; if(ht[f].lchild==c) Cd[start]='0'; ElseCd[start]='1'; C=F; F=ht[f].parent; } STR=cd; Hc[i]=str.substr (start); } Deletecd; HC1=1; cout<<"Huffman Coding Success!"<<Endl; cout<<Endl; } Else if(Choice = =3) { intresult=0;//used to determine whether to find stringsearch; cout<<"Enter the characters you want to query:"; CIN>>search; for(intI=1; i<=n;i++) { if(ht[i].name==search) {cout<<"The search results are:"<Endl; Result=1; } } if(result==0) {cout<<"No such information!"<<Endl; } cout<<Endl; } Else if(Choice = =4) { intresult=0;//used to determine whether to find stringstr; cout<<"Please enter the Huffman code to query:"; CIN>>str; for(intI=1; i<=n;i++) { if(str==Hc[i]) {cout<<"the results of the lookup are:"<Endl; Result=1; } } if(result==0) {cout<<"The code you are looking for does not exist!"<<Endl; } cout<<Endl; } Else if(choice==5) {cout<<"The program is over!"<<Endl; Break; } Else{cout<<"no such feature!"<<Endl; } } return 0;}
The Huffman tree of data structure