I'm creating a huffman tree in C. I store all the characters with their frequencies as a queue_elem element. I put all those elements in a priority queue then take the least frequent elements, allocate a new queue_elem element with its left and right child being those 2 elements then add that new parent node to the pariotiy queue and lastly detele+free those 2 child nodes from the priority queue. My probem is that I'm struggling to "move" those 2 elements into the children nodes of the parent node.
I either add them, free those 2 nodes, but they are then freed entierly (from both the priority queue and parent's children nodes) or I try allocating memory like I did here (this is a small test program I wrote to test how I would do it in the real file):
#include <stdio.h>
#include <stdlib.h>
typedef struct queue_elem queue_elem;
// The type for elements in the priority queue
struct queue_elem {
//How often a chracter is repeated
int freq;
struct queue_elem *left_child;
struct queue_elem *right_child;
unsigned char val;
};
queue_elem *create_pq_elem(int val, int freq);
void delete_nodes(queue_elem *qe);
queue_elem *create_pq_elem(int val, int freq) {
queue_elem *qe = malloc(sizeof(queue_elem));
qe->freq = freq;
qe->val = val;
qe->left_child = NULL;
qe->right_child = NULL;
return qe;
}
void delete_nodes(queue_elem *qe)
{
if(qe != NULL)
{
if(qe->left_child != NULL || qe->right_child != NULL)
{
delete_nodes(qe->left_child);
delete_nodes(qe->right_child);
}
free(qe);
qe = NULL;
}
}
int main() {
queue_elem *qe1 = create_pq_elem(97, 5);
queue_elem *qe2 = create_pq_elem(65, 3);
queue_elem *qe3 = create_pq_elem(0, qe1->freq + qe2->freq);
qe3->left_child =create_pq_elem(qe2->val, qe2->freq);
qe3->right_child = create_pq_elem(qe1->val, qe1->freq);
free(qe2);
free(qe1);
queue_elem *qe4 = create_pq_elem(0, qe3->freq);
qe4->left_child =create_pq_elem(qe3->left_child->val, qe3->left_child-freq);
// Here, I'm supossed to delete+free the child node from the priority queue after it's been added.
free(qe3);
delete_nodes(qe4);
return 0;
}
But as you may have noticed, I end up losing the memory I allocated for qe3 left and right child.
I tried doing it like this, but I just free the child nodes.
#include <stdio.h>
#include <stdlib.h>
typedef struct queue_elem queue_elem;
// The type for element in the priority queue
struct queue_elem {
//How often a chracter is repeated
int freq;
queue_elem *left_child;
queue_elem *right_child;
//the character
unsigned char val;
};
queue_elem *create_pq_elem(int val, int freq) {
queue_elem *qe = malloc(sizeof(queue_elem));
qe->freq = freq;
qe->val = val;
qe->left_child = NULL;
qe->right_child = NULL;
return qe;
}
int main() {
queue_elem *qe1 = create_pq_elem(97, 5);
queue_elem *qe2 = create_pq_elem(65, 3);
queue_elem *qe3 = create_pq_elem(0, qe1->freq + qe2->freq);
qe3->left_child = qe2;
qe3->right_child = qe1;
free(qe2);
free(qe1);
printf("%c has a frequency of %d", qe3->left_child->val, qe3->right_child->freq);
return 0;
}
Anyone got a solution for this? TL;DR, I wanna move the child nodes to a new parent node but can't seem to figure it out.
The above code is missing the logic of adding the elements to the priority queue. The elements need to be added to the queue after