I need to write a program that will accept a valid text file, read it, then create a Huffman tree from the file, encode the text, then decode it to prove that my tree works. I have already written the code to create the priority queue for the tree, but when i try to actually build the tree at the end my root node isn't linked to its right or left children. Why isn't it linking like it should?
void build_tree()
{
int progress = 1;
while(priority_list.size()>1)
{
char c=' ';
Node* temp = new Node(c);
temp->left = priority_list[priority_list.size()-1];
priority_list.pop_back();
temp->right=priority_list[priority_list.size()-1];
priority_list.pop_back();
temp->order = progress; //keeps track of which new nodes are the oldest.
progress++;
int left_freq=0;
int right_freq=0;
if(temp->left!=NULL)
left_freq=temp->left->freq;
if(temp->right!=NULL)
right_freq=temp->right->freq;
temp->freq = left_freq + right_freq;
priority_list.push_back(temp);
organize_priority_list();
}
root = priority_list[0];
priority_list.pop_back();
}
so i thought that once i reached the end, my priority list will only have 1 node left in it (which i tested and it does) so i could just assign the root (which is a Node pointer) to that remaining object. but when i run my print() method (which is also tested and works fine), the root doesn't have any children linked. can someone help please?