回 帖 发 新 帖 刷新版面

主题:菜鸟求助,霍夫曼树的构造及编码问题

谁能帮我看看这个程序应该怎么修改?谢谢了!
#include<iostream>
#include<malloc.h>
#include<string.h>
#include<stdio.h>
using namespace std;
typedef struct{
    unsigned int weight;
    unsigned int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char * * HuffmanCode;
void HuffmanCoding(HuffmanTree&HT,HuffmanCode&HC,int *w,int n){
    if(n<1)return;
    int m=2*n-1;
    HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
    int i;
    HuffmanTree p;//p应该是什么类型?
    for(p=HT+1,i=1;i<=n;++i,++p,++w) *p={*w,0,0,0};
    for(;i<m;++i,++p) *p={0,0,0,0};
    //找出权值最小的两个结点
    int min1=513,min2=513;
    for(i=n+1;i<m;++i){
        if(w[i]<w[min1]){min2=min1;min1=i;}
        else if(w[i]<w[min2])
            min2=i;
    
    
HT[min1].parent=i;HT[min2].parent=i;
HT[i].lchild=min1;HT[i].rchild=min2;
HT[i].weight=HT[min1].weight+HT[min2].weight;


}

HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
char* cd;//cd应该是什么类型
cd=(char*)malloc(n*sizeof(char));
cd[n-1]="\0";
int c,f;

for(i=1;i<=n;++i){
    int start;
    start=n-1;
    
    for(c=i,f=HT[i].parent;f!=0;c=f;f=HT[f].parent)
        if(HT[i].lchild==c) cd[--start]="0";
        else cd[--start]="1";
        HC[i]=(char *)malloc((n-start)*sizeof(char));
        strcpy(HC[i],&cd[start]);
}
delete cd;
}


//出错信息
 Build started: Project: Huffman Tree, Configuration: Debug Win32 ------
1>Compiling...
1>Huffman Tree.cpp
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(17) : error C2059: syntax error : '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(17) : error C2143: syntax error : missing ';' before '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(17) : error C2143: syntax error : missing ';' before '}'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(18) : error C2059: syntax error : '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(18) : error C2143: syntax error : missing ';' before '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(18) : error C2143: syntax error : missing ';' before '}'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(37) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1>        There is no context in which this conversion is possible
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(44) : error C2146: syntax error : missing ')' before identifier 'f'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(44) : error C2059: syntax error : ';'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(44) : error C2059: syntax error : ')'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(45) : error C2143: syntax error : missing ';' before 'if'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(45) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1>        There is no context in which this conversion is possible
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(46) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1>        There is no context in which this conversion is possible
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(47) : error C2146: syntax error : missing ')' before identifier ')malloc'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(47) : error C3861: ')malloc': identifier not found
1>Build log was saved at "file://c:\Users\HP\Documents\Visual Studio 2008\Projects\Huffman Tree\Huffman Tree\Debug\BuildLog.htm"
1>Huffman Tree - 15 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

回复列表 (共1个回复)

沙发


你的选择两个权值最小的那个地方出错了,也就是你可以用一个select函数,例如:void Select(HuffmanTree HT,int i,int &s1,int &s2)
{
    
int q=100;int t=100;int j,k;
    for(j=1;j<=i;j++)
        
            if(HT[j].parent == 0 /*m和n只是一个穿插的过程*/ 
            && (HT[j].weight < t || HT[j].weight < q))
         {
             if(t < q)         /*把大的除去,小的继续比较放那里*/ 
             {
                 q = HT[j].weight;
                 s2 = j;
             }
             else
             {
                 t = HT[j].weight;
                 s1 = j;
             }
            }//if
        
     
     if(s1 > s2)            /*把小的放前面*/
     {
         k = s1;
         s1 = s2;
         s2 = k;
     }
我也是初学,不对的地方,多多包涵····

我来回复

您尚未登录,请登录后再回复。点此登录或注册