简体   繁体   中英

Trying to update an array with huffman string code

My current program creates a huffman tree filled with nodes of ascii characters that are being read from a text file along with the amount of time they appear in the text file (frequency). In addition, it outputs unique codes for each character read based on frequency, this is done using my traverse function.

My problem: I have this string array that can hold codes for all 256 ascii values in my huffman function that by default is set to an empty string for each element. I have been trying to update my array by passing a parameter to my traverse function but it gives me an error.

Code E0413 - "No suitable conversion from std::string to char exists"

Parts of my code below along with some explanation of what the variables in my traverse function are:

'character' is the char that has been found in the text file

'frequency' gives the number of times a character is read in the text file

'traversecode' is the huffman code being generated by the traverse function

I have also commented out lines in my traverse function where I get the error.

struct node {
    int frequency;
    char character;
    const node *child0;
    const node *child1;

    node(unsigned char c = 0, int i = -1) {
        character = c;
        frequency = i;
        child0 = 0;
        child1 = 0;
    }

    node(const node* c0, const node *c1) {
        character = 0;
        frequency = c0->frequency + c1->frequency;
        child0 = c0;
        child1 = c1;
    }

    bool operator<(const node &a) const {
        return frequency > a.frequency;
    }



    void traverse(string codearray[256], string traversecode = "") const {
        if (child0) {
            child0->traverse(traversecode + '0'); // one line throwing the error
            child1->traverse(traversecode + '1'); // second line that throws me the error
        }
        else {
            codearray[int(character)] = traversecode;
            cout << " " << character << "     ";
            cout << frequency;
            cout << "    " << traversecode << endl;
        }
    }

};

huffman function (function that contains array I would like to get updated)

void huffman(string code[256], const unsigned long long frequency[256]) {
    priority_queue < node > q;
    for (unsigned i = 0; i < 256; i++) {
        if (frequency[i] == 0) {
            code[i] = "";
        }
    }

    for (int i = 0; i < 256; i++)
        if (frequency[i])
            q.push(node(i, frequency[i]));

    while (q.size() > 1) {
        node *child0 = new node(q.top());
        q.pop();
        node *child1 = new node(q.top());
        q.pop();
        q.push(node(child0, child1));
    }

    cout << "CHAR  FREQUENCY  HUFFMAN CODE" << endl;
    q.top().traverse(code);
}

When you make the recursive call to traverse , you need to provide both parameters.

child0->traverse(codearray, traversecode + '0');

You're currently trying to pass what should be the second parameter as the first.

One other possible issue is that your code assumes that char is unsigned. If a char is signed, the access to codearray[int(character)] will access outside the bounds of codearray if the character is "negative" (or in the upper half of the ASCII table when using unsigned characters).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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