。 An empty statement semicolon is missing when the minimum s1s2 is selected: Commissioning for a long time. Hang Daddy.
This is the best coding strategy to achieve the fewest coding
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <math.h>5#include <queue>6#include <algorithm>7 using namespacestd;8typedefstruct9 {TenUnsignedintweight; OneUnsignedintParent,lchild,rchild; A}htnode,*Huffmantree; -typedefChar*Huffmancode; - void Select(Huffmantree *ht,intNint*S1,int*S2)//Huffman Tree Pointer the { - inti; - for(i=1;i<=n&& (*HT) [i].parent!=0; i++);//!!!!!!!! Pit Daddy -*s1=i; + for(i=1; i<=n;i++) - if((*HT) [i].parent==0&& ((*HT) [i].weight< (*HT) [*s1].weight)) +*s1=i; A for(i=1; i<=n;i++) at if((*HT) [i].parent==0&&i!=*S1) - Break; -*s2=i; - for(i=1; i<=n;i++) - if((*HT) [i].parent==0&&i!=*s1&& ((*HT) [i].weight< (*HT) [*s2].weight)) -*s2=i; in } - voidHuffmancoding (Huffmantree *ht,huffmancode *HC,int*w,intN) to { + Char*cd; - intm,i,start,f,s1,s2; theUnsignedintC; * if(n<=1)return; $m=2*n-1;Panax Notoginseng*ht= (Huffmantree)malloc((m+1)*sizeof(Htnode)); - for(i=1; i<=n;i++) the { +(*HT) [i].weight=W[i]; A(*HT) [i].lchild=0; the(*HT) [i].rchild=0; +(*HT) [i].parent=0; -}//Assign Value $ for(i=n+1; i<=m;i++)//back space Clear 0 $ { -(*HT) [i].weight=0; -(*HT) [i].parent=0; the(*HT) [i].lchild=0; -(*HT) [i].rchild=0;Wuyi } the for(i=n+1; i<=m;i++) - { Wu Select(ht,i-1, &S1,&S2);//find the smallest s1,s2 junction. -(*HT) [S1].parent=i; (*HT) [s2].parent=i; About(*HT) [I].lchild=s1; (*HT) [i].rchild=S2; $(*HT) [I].weight= (*HT) [S1].weight+ (*HT) [S2].weight; - } -Hc= (huffmancode*)malloc((n+1)*sizeof(Char*));//N-Coded head pointers -Cd= (Char*)malloc(nsizeof(Char));//Current encoding Space Acd[n-1]=' /';//right-to-left coding, first put end of end flag + for(i=1; i<=n;i++)//find the Huffman code of Knot point the { -start=n-1; $ for(c=i,f= (*HT) [i].parent;f!=0; c=f,f= (*HT) [F].parent] the { the if((*HT) [f].lchild==c) thecd[--start]='0'; the Else -cd[--start]='1'; in } theHc[i]= (Char*)malloc((N-start) *sizeof(Char)); thestrcpy (hc[i],&Cd[start]); About } the Free(CD); the for(i=1; i<= -; i++) theprintf"%c---%s\n", i+ the, Hc[i]); + } - Chars[ +]; the inta[ -]={0};Bayi intMain () the { the Huffmancode HC; - Huffmantree ht; - intN,t,i=0, J,l,r,len; the int*b; the floatnum=0; the gets (s); thet=strlen (s); -B= (int*)malloc( -*sizeof(int)); the while(i<t) the { the if(s[i]>='A'&&s[i]<='Z')94{a[s[i]- -]++;num++;} the if(s[i]>='a'&&s[i]<='Z') the{a[s[i]- the]++;num++;} thei++;98 } About for(i=1; i<= -; i++) - {101b[i]= (a[i]/num) *10000;102printf"%c:%d%.3f---%d\n", i+ the, a[i],a[i]/num,b[i]);103 }104Huffmancoding (&ht,&hc,b, -); the return 0;106}
Heffman (optimal binary tree)