public void deleteLeaves(BSTNode<T> p){ //deletes leaves
if(p.left != null){
if(isLeaf(p.left))
p.left = null;
}
else if(p.right != null){
if(isLeaf(p.right))
p.right = null;
}
else{
deleteLeaves(p.right);
deleteLeaves(p.left);
}
}
I cannot seem to figure out as to why it is not correctly deleting the leaves. It seems to only delete the final leaf of the tree. Any advice that helps would greatly be appreciated.
Your if-statements are a bit messed up; Only one of the branches will be taken. Think about what should happen if neighter
p.leftnorp.rightis null.If I understand your data structure correctly, I’d probably write it like this: