I’m trying to make a DTC in C. This is the structure of the nodes:
typedef struct node{
int value;
struct node *lower;
struct node *higher;
}node;
To free the memory allocated using malloc(), I’ve implemented this function:
void free_nodes(node*a){
if(a-> higher != NULL){
free_nodes(a-> higher);
}
if(a-> lower != NULL){
free_nodes(a-> lower);
}
if(a-> higher == NULL && a -> lower == NULL){
free(a);
}
}
It takes the pointer to the root node as a parameter.
I tried to use recursion here but it does not seem to free all the nodes. It is only able to free two nodes (i checked using valgrind)