I wrote a function to nullify all the leaves of a BST. The BST has a left and right pointer of course and a char called data to store the value of the node.
void removeLeaves(struct Tree* T){
if(T->left == NULL && T->right == NULL){
printf("removing %c\n", T->data);
T=NULL;
}
else{
if(T->left!=NULL){
removeLeaves(T->left);
}
if(T->right!=NULL){
removeLeaves(T->right);
}
}
}
I Print the tree before and after envoking this function.
And although the print statement above works and prints the nodes that are nullified, the resulting tree is the same.
I have something like:
print(BST);
removeLeaves(BST);
print(BST);
Any idea what’s going on?
Thanks.
T=NULL;assigns null to a local pointer, not to anything in your tree. You need to use astruct Tree **so that you can modify thestruct Tree *: