| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:求助 关于哈夫曼编码的问题
只看楼主 加入收藏
紫凤双飞
Rank: 2
等 级:论坛游民
帖 子:76
专家分:61
注 册:2011-3-26
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:3 
求助 关于哈夫曼编码的问题
使用顺序存储的

编译没有问题,就是一运行就出现 window正在查找解决方案(在vc2005中提示:访问冲突)
不知问题出在哪儿,请大神帮帮忙

代码如下:
程序代码:
#include<string>
#include<iostream>
using std::cout;
using std::string;

template<typename type>
class HfTree
{
private:
    static const int ROOT=-1;

    struct node
    {
        type data;
        int weight;
        int parent,lchild,rchild;
    };
    int length;
    node* elem;
   
    struct hfCode
    {
        type data;// store the character
        string code;// the code of the data
    };
    hfCode* result;
   
    void getCode();
public:
   
    HfTree(const type* d,const int* w,int size);
   
    ~HfTree(){delete [] elem;delete [] result;}
    void printCode();
};


template<typename type>
HfTree<type>::HfTree(const type *d, const int *w, int size)
{
    length=size*2-1;
    elem=new node[length];
    //init table"elem"
    for(int i=size-1;i<length;++i)
    {
        elem[i].data=d[i-size+1];
        elem[i].weight=w[i-size+1];
        elem[i].lchild=elem[i].rchild=elem[i].parent=ROOT;
    }

    elem[0].parent=ROOT;//It shore the root of the hfTree and has no father

    for(int i=size-2;i>=0;--i)
    {
        int xMIN_SHORT=32767;
        int yMIN_SHORT=32767;
        int xIndex=ROOT;
        int yIndex=ROOT;

        for(int j=i+1;j<length;++j)
        {
            if(elem[j].parent==ROOT)//if this is true,then it has not been visited.
            {
                if(elem[j].weight<xMIN_SHORT)
                {
                    yMIN_SHORT=xMIN_SHORT;
                    yIndex=xIndex;
                    xMIN_SHORT=elem[j].weight;
                    xIndex=j;
                }
                else if(elem[j].weight<yMIN_SHORT)
                {
                    yMIN_SHORT=elem[j].weight;
                    yIndex=j;
                }
            }
        }

        elem[i].weight=xMIN_SHORT+yMIN_SHORT;
        elem[i].lchild=xIndex;
        elem[i].rchild=yIndex;
        elem[xIndex].parent=elem[yIndex].parent=i;
    }
}

template<typename type>
void HfTree<type>::getCode()
{
    int size=length/2+1;
    int father,index;
    result=new hfCode[size];
   
    for(int i=size-1;i<length;++i)
    {
        result[i-size+1].data=elem[i].data;
        result[i-size+1].code="";

        father=elem[i].parent;
        index=i;
        while(father!=ROOT)
        {
            if(elem[father].lchild==index)
                result[i-size+1].code='0'+result[i-size+1].code;
            else
                result[i-size+1].code='1'+result[i-size+1].code;
            index=father;
            father=elem[father].parent;
        }
    }
}

template<typename type>
void HfTree<type>::printCode()
{
    getCode();
    int size=length/2-1;
    for(int i=0;i<size;++i)
        cout<<result[i].data<<"  :"<<result[i].code<<'\n';
    cout<<'\n';
}

int main()
{
    char ch[]={"aeistdn"};
    int w[]={10,15,12,3,4,13,1};

    HfTree<char> tree(ch,w,7);

    tree.printCode();
    return 0;
}


搜索更多相关主题的帖子: 解决方案 window 
2011-09-15 22:19
xg5699
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:140
专家分:522
注 册:2011-7-27
收藏
得分:20 
程序代码:
#include<string>
#include<iostream>
using std::cout;
using std::string;

template<typename type>
class HfTree
{
private:
    static const int ROOT=-1;

    struct node
    {
        type data;
        int weight;
        int parent,lchild,rchild;
    };
    int length;
    node* elem;
  
    struct hfCode
    {
        type data;// store the character
        string code;// the code of the data
    };
    hfCode* result;
  
    void getCode();
public:
  
    HfTree(const type* d,const int* w,int size);
  
    ~HfTree(){delete [] elem;delete [] result;}
    void printCode();
};


template<typename type>
HfTree<type>::HfTree(const type *d, const int *w, int size)
{
   
    length=size*2-1;
    elem=new node[length];
    //init table"elem"
    for(int i=size-1;i<length;++i)
    {
        elem[i].data=d[i-size+1];
        elem[i].weight=w[i-size+1];
        elem[i].lchild=elem[i].rchild=elem[i].parent=ROOT;
    }

    elem[0].parent=ROOT;//It shore the root of the hfTree and has no father
  
    for(int i=size-2;i>=0;--i)
    {
        int xMIN_SHORT=32767;
        int yMIN_SHORT=32767;
        int xIndex=ROOT;
        int yIndex=ROOT;

        for(int j=i+1;j<length;++j)
        {
            if(elem[j].parent==ROOT)//if this is true,then it has not been visited.
            {
                if(elem[j].weight<xMIN_SHORT)
                {
                    yMIN_SHORT=xMIN_SHORT;
                    yIndex=xIndex;
                    xMIN_SHORT=elem[j].weight;
                    xIndex=j;
                }
                else if(elem[j].weight<yMIN_SHORT)
                {
                    yMIN_SHORT=elem[j].weight;
                    yIndex=j;
                }
                       
            }
       
       
        }
    
      
         elem[i].weight=xMIN_SHORT+yMIN_SHORT;
        elem[i].lchild=xIndex;
        elem[i].rchild=yIndex;
      elem[xIndex].parent=elem[yIndex].parent=i;
       
    
    }
   
      
    }

template<typename type>
void HfTree<type>::getCode()
{
    int size=length/2+1;
    int father,index;
    result=new hfCode[size];
  
    for(int i=size-1;i<length;++i)
    {
        result[i-size+1].data=elem[i].data;
        result[i-size+1].code="";

        father=elem[i].parent;
       
        index=i;
        while(father!=ROOT)
        {
           
            if(elem[father].lchild==index)
                result[i-size+1].code='0'+result[i-size+1].code;
            else
                result[i-size+1].code='1'+result[i-size+1].code;
            index=father;
            father=elem[father].parent;
                cout<<father;             //估计错在"这里",因为把cout移上去是正常输出4,但是如果放到这里就是-842150451,father变成了这个当然要出错啦           
        }
    }
   
}

template<typename type>
void HfTree<type>::printCode()
{
    getCode();
    int size=length/2-1;
    for(int i=0;i<size;++i)
        cout<<result[i].data<<"  :"<<result[i].code<<'\n';
    cout<<'\n';
}

int main()
{
    char ch[]={"aeistdn"};
   
    int w[]={10,15,12,3,4,13,1};

    HfTree<char> tree(ch,w,7);

   tree.printCode();
    return 0;
}



都不结贴我郁闷那!
2011-09-16 01:15
紫凤双飞
Rank: 2
等 级:论坛游民
帖 子:76
专家分:61
注 册:2011-3-26
收藏
得分:0 
回复 2楼 xg5699
对,确实是parent的问题,已经改好了
Thank you
2011-09-16 21:16
lqsh
Rank: 2
来 自:山东济南
等 级:论坛游民
帖 子:26
专家分:58
注 册:2011-8-29
收藏
得分:0 
程序代码:
class Hfman
{
private:
    struct node
    {
        int type;
        int wight;
        int parent,leftchild,rightchild;
    };
    int length;//节总数点
    node *p;
    struct hfcode
    {
        int elem;
        string s;
    };
    hfcode *c;
public:
    Hfman();
    void creathfmantree(int size);
    ~Hfman()
    {
        delete[]p;
        delete[]c;
    }
    void select(int n,int &s1,int &s2);
    void printcode(int size);
    void getcode(int size);
};
Hfman::Hfman()
{
    length=0;
    p=NULL;
    c=NULL;
}
void Hfman::getcode(int size)
{
    int f;
    int cc;
    c=new hfcode[size];
    if(!c)
    {
        cout<<"内存分配失败!"<<endl;

    }
    for(int i=0;i<size;i++)
    {
        c[i].elem=p[i].type;
        for(cc=i,f=p[i].parent;f!=0;cc=f,f=p[f].parent)
        {
            if(p[f].leftchild==cc)
            {
                c[i].s+='0';
            }
            else
                c[i].s+='1';

        }
        reverse(c[i].s.begin(),c[i].s.end());

    }


}
void Hfman::printcode(int size)
{
    for(int i=0;i<length;i++)
    {
        cout<<p[i].type<<" "<<p[i].wight<<" "<<p[i].parent<<" "<<p[i].leftchild<<" "<<p[i].rightchild<<endl;
    }
    for(int j=0;j<size;j++)
    {
        cout<<c[j].elem<<" "<<c[j].s<<endl;
    }
}
void Hfman::select(int n,int &s1,int &s2)//找两个最小值
{
   int s,j=0;
   int tag1=0;
   int tag2=0;
   int temp;
   for(;j<n;j++)
   {
       if(p[j].parent==0)
       {
           if(tag1==0)
           {
               tag1=p[j].wight;
               s1=j;
           }
           else
           {
               if(tag2==0)
               {
                   tag2=p[j].wight;
                   s2=j;
                   if(tag2<tag1)
                   {
                       int temp=tag1;
                       tag1=tag2;
                       tag2=temp;
                       temp=s1;
                       s1=s2;
                       s2=temp;
                   }
               }
               else
               {
                   if(p[j].wight<tag1)
                   {
                       tag1=p[j].wight;
                       s1=j;
                   }
                   else if(p[j].wight>tag1&&p[j].wight<tag2)
                   {
                       tag2=p[j].wight;
                       s2=j;
                   }
               }
           }
       }
   }

}
void Hfman::creathfmantree(int size)
{
    int i,j;
    int num,w;
    length=size*2-1;
    int s1=0,s2=0;
    p=new node[length];
    if(!p)
    {
        cout<<"内存分配失败1"<<endl;
        exit(0);
    }
    for(i=0;i<size;i++)
    {
        cin>>p[i].type>>p[i].wight;
        p[i].parent=p[i].leftchild=p[i].rightchild=0;
    }
    for(;i<length;i++)
    {
        p[i].parent=p[i].leftchild=p[i].rightchild=0;
    }
    for(j=size;j<length;j++)
    {
        select(j,s1,s2);
        p[s1].parent=j;
        p[s2].parent=j;
        p[j].leftchild=s1;
        p[j].rightchild=s2;
        p[j].wight=p[s1].wight+p[s2].wight;
    }



}
int main()
{
    Hfman m;
    m.creathfmantree(8);
    m.getcode(8);
    m.printcode(8);
    return 0;
}
2011-09-17 21:43
快速回复:求助 关于哈夫曼编码的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036141 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved