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?