0
struct Node{
    int freq;
    string character;

    struct Node *left;
    struct Node *right;
};

I've parsed a file and created a priority queue of Nodes. Each node has an int of frequencies, a string for the characters being represented in the node. And two Node pointers for the left and right branches.

The following code is located in the main function.

priority_queue<Node, vector<Node>, CompareNodes> pq;

// Build Tree //
while( pq.size() > 1 )
{
    Node * l = new Node;
    Node * r = new Node;

    Node r1 = pq.top();
    r->character = r1.character;
    r->freq = r1.freq;
    pq.pop();

    Node l1 = pq.top();
    l->character = l1.character;
    l->freq = l1.freq;
    pq.pop();


    Node * combined = new Node;
    combined->character = l1.character + r1.character;
    combined->freq = l1.freq + r1.freq;
    combined->left = l;
    combined->right = r;

    pq.push(*combined);

}

after running the code above and checking each left and right pointer, all the left and right pointers beyond the first level are NULL.

Essentially, traveling up the tree from the root is impossible. Only the first left and right pointers lead to a Node. All other children left/right pointers lead no where.

I have a feeling that I am not allocating space properly. The locations *l and *r after each pass should still be accessible and contain a Node right? Or are they local to the while loop and get deleted after each pass?

  • You dont need to new up l and r - just use the existing l1 and r1 as combined->left and combined->right – Mike Vine Mar 23 '15 at 20:54
  • I've removed the l and r. I used them just to be sure. Code still functions the same. The addresses of l1 and r1 should still exist and should be being passed to the new parent. But following them and trying to print them out or view their contents leads nowhere and crashes the program. – Alfredo Gonzalez Mar 23 '15 at 21:01
  • OK I think I can see the problem. You are using a `queue`. When you copy the Node's as they enter and exit the queue you are losing information. An "easy" fix would be to use a `queue` and `new` up the Nodes as you first enter them. That combined with the above fix of removing the l and r variables would make this work (you would need to also change CompareNodes comparator to take by pointer also. A more correct fix would be to have something like a `queue>` as then you could have better lifetime management but then you would require a lot more changes. – Mike Vine Mar 23 '15 at 21:37

1 Answers1

0

The answer was as Mike Vine explained.

You are using a queue. When you copy the Node's as they enter and exit the queue you are losing information. An "easy" fix would be to use a queue Node* and new up the Nodes as you first enter them. That combined with the above fix of removing the l and r variables would make this work (you would need to also change CompareNodes comparator to take by pointer also. A more correct fix would be to have something like a queue unique_ptr Node as then you could have better lifetime management but then you would require a lot more changes.

Thank you, Mike.