数据结构1-05-树9 Huffman Codes

泪湿孤枕 提交于 2020-04-06 19:07:45

 

 

  1 #include<stdio.h>
  2 #include<string.h>
  3 #define MAXN 1001
  4 struct node{
  5     int data,left,right,parent;
  6     node(){
  7         data = -1;
  8         left = -1;
  9         right=-1;
 10         parent=-1;
 11     }
 12 };
 13 struct node huffT[MAXN],testT[MAXN];
 14 char c[MAXN],a[MAXN];
 15 int f[MAXN],heap[MAXN],m,n,WPL;
 16 void read();
 17 void insert(int x);
 18 int del();
 19 void Cwpl();
 20 int test();
 21 int main(){
 22     int i;
 23     
 24     read();
 25     for(i=1;i<=m;i++){
 26         if(test()==1) printf("Yes\n");
 27         else printf("No\n");
 28     }
 29     return 0;
 30 }
 31 void read(){
 32     int i;
 33     scanf("%d",&n);
 34     heap[0] =0;
 35     for(i=1;i<=n;i++){
 36         getchar();
 37         scanf("%c %d",&c[i],&f[i]);    
 38         huffT[i].data = f[i];
 39         huffT[i].left = -1;
 40         huffT[i].right = -1;
 41         huffT[i].parent = -1;
 42         insert(i);
 43     }
 44     Cwpl();
 45     scanf("%d",&m);
 46 } 
 47 void insert(int x){
 48     if(heap[0]==0){
 49         heap[0]++;
 50         heap[heap[0]] = x;
 51         return;
 52     }
 53     int i,count,i1,i2;
 54     heap[0]++;
 55     heap[heap[0]] = x;
 56     for(i=heap[0];i>1;i=i/2){
 57         i1 = heap[i];
 58         i2 = heap[i/2];
 59         if(huffT[x].data< huffT[i2].data){
 60             heap[i] = i2;
 61         }
 62         else break;
 63     }
 64     heap[i] = x;
 65 }
 66 int test(){
 67     char tempc,tempcode[MAXN];
 68     int i,j,tempwpl=0,flag=1,root=1,count=1,tempindex;
 69     for(i=1;i<MAXN;i++){
 70         testT[i].data = -1;
 71         testT[i].left = -1;
 72         testT[i].parent = -1;
 73         testT[i].right = -1;
 74     }
 75     for(i=1;i<=n;i++){
 76         getchar();
 77         scanf("%c %s",&tempc,tempcode);
 78         tempwpl += strlen(tempcode)*f[i];
 79         tempindex = root;
 80         for(j=0;j<strlen(tempcode);j++){
 81             if(tempcode[j]=='1'){
 82                 if (testT[tempindex].right==-1){
 83                     testT[tempindex].right = ++count;
 84                     tempindex = count;
 85                 }   
 86                 else tempindex = testT[tempindex].right;
 87                 
 88             }
 89             else if(tempcode[j]=='0'){
 90                 if(testT[tempindex].left==-1){
 91                     testT[tempindex].left = ++count;
 92                     tempindex = count;
 93                 }
 94                 else tempindex = testT[tempindex].left;
 95             }
 96             
 97             
 98             if(j==strlen(tempcode)-1){
 99                 if(testT[tempindex].data==-1)
100                   if(testT[tempindex].left==-1 && testT[tempindex].right==-1) testT[tempindex].data = f[i];
101                   else flag=0;
102                 else flag=0;
103             }
104                 
105             }
106         }
107         
108     
109     if(flag==0)  return 0;
110     else{
111         if(tempwpl==WPL)  return 1;
112         else return 0;
113     }
114 }
115 void Cwpl(){
116     int temp=0,count=n,value;
117     int i1,i2,i;
118     
119     for(i=1;i<n;i++){
120         i1 = del();
121         i2 = del();
122         if(i1==-1 || i2==-1) break;
123         count++;
124         huffT[count].data = huffT[i1].data +  huffT[i2].data;
125         huffT[count].left = i1;
126         huffT[count].right = i2;
127         huffT[count].parent = -1;
128         huffT[i1].parent = count;
129         huffT[i2].parent = count;
130         insert(count);
131     }
132     
133     WPL=0;
134     for(i=1;i<=n;i++){
135         value = 0;
136         temp = huffT[i].parent;
137         while(temp!=-1){
138             value ++;
139             temp = huffT[temp].parent;
140         }
141         WPL += f[i]*value;
142     }
143 //    printf("WPL: %d\n",WPL);
144     
145     
146 }
147 
148 int del(){
149     int temp;
150     if(heap[0]==0) return -1; 
151     temp = heap[1];
152     heap[1] = heap[heap[0]];
153     heap[0]--;
154 //    adjust heap[1]
155     int i,value,i1,i2,index;
156     for(i=1;i<heap[0];){
157         if(i*2+1<=heap[0]){
158             i1 = heap[i*2];
159             i2 = heap[i*2+1];
160             if(huffT[i1].data>huffT[i2].data) index = i*2+1;
161             else index = i*2;                                  ///
162         }
163         else if(i*2<=heap[0]) index = i*2;
164         
165         if(huffT[heap[i]].data > huffT[heap[index]].data){
166             value = heap[i];
167             heap[i] = heap[index];
168             heap[index] = value;
169             i = index;
170         }
171         else break;
172         
173     }
174     return temp;
175 }
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!