views:

115

answers:

3

I am programming the huffman encoding. This is the beginning of my program:

using namespace std;

//Counting methods
int *CountCharOccurence(string text)
{
    int *charOccurrence = new int[127];
    for(int i = 0; i < text.length(); i++)
    {
        charOccurrence[text[i]]++;
    }
    return charOccurrence;
}

void DisplayCharOccurence(int *charOccurrence)
{
    for(int i = 0; i < 127; i++)
    {
        if(charOccurrence[i] > 0)
        {
            cout << (char)i << ": " << charOccurrence[i] << endl;
        }
    }
}

//Node struct
struct Node
{
    public:
        char character;
        int occurrence;

        Node(char c, int occ) {
            character = c;
            occurrence = occ;
        }

        bool operator < (const Node* node)
        {
            return (occurrence < node->occurrence);
        }
};

void CreateHuffmanTree(int *charOccurrence)
{
    priority_queue<Node*, vector<Node*> > pq;
    for(int i = 0; i < 127; i++)
    {
        if(charOccurrence[i])
        {
            Node* node = new Node((char)i, charOccurrence[i]);
            pq.push(node);
        }
    }

    //Test
    while(!pq.empty())
    {
        cout << "peek: " << pq.top()->character <<  pq.top()->occurrence << endl;
        pq.pop();
    }
}

int main(int argc, char** argv) {

    int *occurrenceArray;
    occurrenceArray = CountCharOccurence("SUSIE SAYS IT IS EASY");
    DisplayCharOccurence(occurrenceArray);
    CreateHuffmanTree(occurrenceArray);

    return (EXIT_SUCCESS);
}

The program first outputs the characters with their occurence number. This looks fine:

 : 4
A: 2
E: 2
I: 3
S: 6
T: 1
U: 1
Y: 2

but the test loop which has to display the node contents in priority order outputs this:

peek: Y2
peek: U1
peek: S6
peek: T1
peek: I3
peek: E2
peek:  4
peek: A2

This is not the expected order. Why?

+1  A: 

You should tell your priority queue what it should sort by. In your case, you have to tell it to sort by Node::occurence.

David Sauter
+2  A: 

Elements in your priority queue are pointers. Since you don't provide a function that takes 2 pointers to Node objects , default compare function compares 2 pointers.

bool compareNodes(Node* val1, Node* val2)
{
   return val1->occurence < val2->occurence;
}
priority_queue<Node*, vector<Node*>,compareNodes > pq;

Your operator < is used when Node compares with Node*

a1ex07
+1  A: 

You are storing pointers to nodes in the queue, but haven't provided a suitable comparison function, so they are sorted by comparing the pointers. The operator< you've provided will compare a node with a pointer, which isn't what you want.

There are two options:

  • Provide a function for comparing two node pointers according to their values, and give this function to the queue, or
  • Store node objects in the queue, and provide an operator< to compare two nodes.

The second option will also fix the memory leak in your code, and remove a whole pile of unnecessary memory allocations, so I would suggest that.

Mike Seymour
Tnx, It works now. I program for a while now, but I'am new to C++. I have to say the better you get to know the language the more you love it. But the beginning is quite hard I think.
TheArchitect