nedintc;hc=(HuffmanCode*)malloc((n+1)*sizeof(char*));cd=(char*)malloc(n*sizeof(char));cd[n-1]='\0';for(i=1;i<=n;i++)//求n个结点对应的哈夫曼编码{a[i]=0;start=n-1;//起始指针位置在最右边for(c=i,p=(*ht)[i].parent;p!=0;c=p,p=(*ht)[p].parent)//从叶子到根结点求编码{if((*ht)[p].LChild==c)?{ cd[--start]='1'; a[i]++;?}else?{ cd[--start]='0'; a[i]++;?}}hc[i]=(char*)malloc((n-start)*sizeof(char));//为第i个编码分配空间strcpy(hc[i],&cd[start]);}free(cd);for(i=1;i<=n;i++)printf("权值为%d的哈夫曼编码为:%s\n",(*ht)[i].weight,hc[i]);for(i=1;i<=n;i++)w+=(*ht)[i].weight*a[i];printf("\n带权路径长度WPL为:%d\n\n",w);}voidmain(){HuffmanTreeHT;HuffmanCodeHC;int*w,i,n,wei;printf("\t\t\t\t哈夫曼编码\n");printf("请输入结点个数:");scanf("%d",&n);w=(int*)malloc((n+1)*sizeof(int));printf("\n请分别输入这%d个结点的权值:\n",n);for(i=1;i<=n;i++){printf("结点%d:",i);fflush(stdin);scanf("%d",&wei);w[i]=wei;}