Q. Let a binary tree 'T' be in memory. Write a procedure to delete all terminal nodes of the tree.
Ans.
function to Delete Terminal Nodes from Binary Tree
void deleteleaves(struct node* root,struct node* prev)
{
if(root)
{
if(root->left==NULL&&root->right==NULL)
{
if(prev->item>=root->item)
prev->left=NULL;
else
prev->right=NULL;
printf("\n\n %d Is Being Deleted ...",root->item);
free(root);
return;
}
deleteleaves(root->left,root);
deleteleaves(root->right,root);
}
}
the call to this function will deleteleaves (root, root).