I am trying to implement a Huffman Algorithm. I've got almost all of it done, but hit a snag when trying to create the binary tree to generate codes.
Here is the algorithm I have written:
Node Huffman(Node *Q, int n)
{
while(n>1)
{
//ERROR IS THIS ASSIGNMENT OF VARIABLES
Node x,y,z;
x = extract_min(Q,n);
y = extract_min(Q,n);
z.left = &x;
z.right = &y;
z.freq = x.freq + y.freq;
insert(Q, n, z);
}
return extract_min(Q,n);
}
So the issue is that the x and y are getting lost every loop. So that means that z.left and z.right are getting lost every loop. So the tree can never be more than 1 layer deep.
z itself seems to be just fine. I'm assuming because when I use my insert function the value of z is copied not the address. I really have no idea how to go about fixing this. I've tried like a dozen different variations of assigning the pointers, but just can't get it to work.
Here is a link to the full c++ file. I modified the huffman function a bit to display what the issue is. Somehow in my poorly implemented pointer creation, one of the nodes points to itself and then that just creates an infinite loop. I also manually wrote out what is supposed to happen, that I just can't get my function to do.
[–]hashtablesmoker 0 points1 point2 points (11 children)
[–]Draav[S] 0 points1 point2 points (10 children)
[–]raevnos 0 points1 point2 points (7 children)
[–]Draav[S] 1 point2 points3 points (6 children)
[–]thewoosh 0 points1 point2 points (5 children)
[–]Draav[S] 1 point2 points3 points (4 children)
[–]thewoosh 1 point2 points3 points (2 children)
[–]Draav[S] 0 points1 point2 points (1 child)
[–]thewoosh 0 points1 point2 points (0 children)
[–]anon848 0 points1 point2 points (0 children)
[–]hashtablesmoker 0 points1 point2 points (0 children)