简体   繁体   English

霍夫曼算法

[英]huffman algorithm

I'm making a program on the HUFFMAN ALGORITHM where I have one class of nodes ( hnode ) to contain the symbol ( name ) and its frequency of occurence ( freq ); 我正在HUFFMAN 算法编写一个程序,其中有一类节点( hnode )包含符号( 名称 )及其出现频率( freq )。 and other class ( pq ) which is used to form the tree to get the code for the symbols. 和其他类( pq ),用于形成以获取符号的代码。 I have written only the relevant parts of the classes. 我只写了课程的相关部分。 Now when I try to run this program it always gets stuck in the while loop ( in main() ) when it enters the while loop second time. 现在,当我尝试运行该程序时,当它第二次进入while循环时,它总是卡在while循环中( 在main()中 )。 I've tried everything but still couldn't figure out why... Can somebody please help to make this code run !! 我已经尝试了所有方法,但是仍然不知道为什么...有人可以帮忙使这段代码运行!

#define NPTR hnode*
class pq;
class hnode
{
    string name;
    int freq;
    friend class pq;

    public:

    NPTR phnode;
    void show () 
    { cout << "name = " << name << endl << ", freq= " << freq <<endl ; }

    hnode (string x, int fr): name(x), freq(fr)
    {
        phnode = this;
    }

    friend hnode operator + (hnode p, hnode q)
    {
        string s = q.name + p.name;
        int f = q.freq + p.freq ;
        hnode foo (s,f);
        return foo;
    }
};

class pq /// ascending priority queue
{
    struct node
    {
    NPTR data;
    node* next;
    node (NPTR p) : data(p), next(0) {}
    };
    public:
    int count;
    node* getnode (NPTR p) { return new node(p); }
    node* listptr;
    void place (NPTR );
    NPTR mindelete();
    pq() : listptr(0), count(0) {}
};

void pq::place (NPTR p)
{
    if(count == 0)
    {
        listptr = getnode(p);
    }

    else
    {
        node* foo = listptr, *bar = NULL;
        while( (foo!= NULL) && (p->freq >= foo->data->freq) )
        {
            bar = foo;
            foo = foo->next;
        }
        node* ptr = getnode(p);
        ptr->next = bar->next;
        bar->next = ptr;
    }
    ++count;
}

NPTR pq::mindelete()
{
    if(count==0) { cout<<"invalid\n"; exit(1);}
    NPTR val = listptr->data;
    listptr = listptr->next;
    --count;
    return val;
}

void main ()
{
    pq list;
    for ( int i=0; i<3; ++i)
    {
        string s;
        int f;
        cin>>s>>f;
        NPTR p = new hnode(s,f);
        list.place(p);
    }
    while(list.count>1)
    {
        NPTR p1 = list.mindelete();
        NPTR p2 = list.mindelete();
        hnode po = (*p1)+(*p2); // overloaded operator
        NPTR p = &po;
        p->show();
        list.place(p);
    }
}

I suggest having a look here: 我建议在这里看看:

NIST adaptive Huffman NIST自适应霍夫曼

and also here: 还有这里:

NIST Huffman NIST霍夫曼

Code samples can be found there also. 也可以在此处找到代码示例。

Your code's creating a locally scoped hnode po over here: 您的代码在此处创建了本地范围的hnode po

while(list.count > 1)
{
    NPTR p1 = list.mindelete();
    NPTR p2 = list.mindelete();
    hnode po = (*p1)+(*p2); // overloaded operator
    NPTR p = &po;
    p->show();
    list.place(p);
}

Then you're passing that around by address and you're making pq::node hold onto that. 然后按地址传递它,并使pq::node保持住该地址。 Sounds like a really bad idea since hnode po goes out of scope at the end of your while loop on every iteration. 听起来真是个坏主意,因为hnode po在每次迭代的while循环结束时超出范围

In general, you want to be using smart points and RAII for automated memory management rather than new'ing and delete'ing all over the place. 通常,您希望将智能点和RAII用于自动内存管理,而不是在各处进行“添加和删除”操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM