In my binary tree, I can delete values that have other values extending from it. If they don't, or if it is the only value, the program crashes. Not sure how to fix it. This is the deletion part of the code:

I'm a noobie at programming so any help would be appreciated. :)

```
void delete_tree (binarytree *t, int x)
{
binarytree temp;
if (!is_empty(*t))
{
if (x == (*t)-> data)
{
if (((*t) -> left == NULL)&&((*t) -> right == NULL))
{
temp = (*t);
(*t) = NULL;
free (temp);
}
if (((*t) -> left != NULL)&&((*t)-> right == NULL))
{
temp = (*t);
(*t) = (*t) -> left;
free (temp);
}
if (((*t)-> left == NULL)&&((*t)-> right != NULL))
{
temp = (*t);
(*t) = (*t) -> right;
free (temp);
}
if (((*t) -> left != NULL)&&((*t) -> right != NULL))
{
temp = (*t)-> right;
while (temp -> left != NULL)
{
temp = (*t) -> left;
}
temp -> left = (*t) ->left;
temp = (*t);
(*t) = (*t) -> right;
free (temp);
}
}
else {
if ( x <= (*t) -> data)
delete_tree(&(*t) -> left, x);
else
delete_tree(&(*t) -> right, x);
}
}
else printf("No data found.\n");
}
```

I'm a noobie at programming so any help would be appreciated. :)

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial```
void delete_tree (binarytree *t, int x)
{
binarytree temp;
if (!is_empty(*t))
{
if (x == (*t)-> data)
{
if (((*t) -> left == NULL)&&((*t) -> right == NULL))
{
temp = (*t);
(*t) = NULL;
free (temp);
// ADD A RETURN
return;
}
if (((*t) -> left != NULL)&&((*t)-> right == NULL))
{
temp = (*t);
(*t) = (*t) -> left;
free (temp);
// ADD A RETURN
return;
}
if (((*t)-> left == NULL)&&((*t)-> right != NULL))
{
temp = (*t);
(*t) = (*t) -> right;
free (temp);
// ADD A RETURN
return;
}
if (((*t) -> left != NULL)&&((*t) -> right != NULL))
{
temp = (*t)-> right;
while (temp -> left != NULL)
{
temp = (*t) -> left;
}
temp -> left = (*t) ->left;
temp = (*t);
(*t) = (*t) -> right;
free (temp);
}
}
else {
if ( x <= (*t) -> data)
delete_tree(&(*t) -> left, x);
else
delete_tree(&(*t) -> right, x);
}
}
else printf("No data found.\n");
}
```

Or```
void delete_tree (binarytree *t, int x)
{
binarytree temp;
if (!is_empty(*t))
{
if (x == (*t)-> data)
{
if (((*t) -> left == NULL)&&((*t) -> right == NULL))
{
temp = (*t);
(*t) = NULL;
free (temp);
}
// ADD AN ELSE
else if (((*t) -> left != NULL)&&((*t)-> right == NULL))
{
temp = (*t);
(*t) = (*t) -> left;
free (temp);
}
// ADD AN ELSE
else if (((*t)-> left == NULL)&&((*t)-> right != NULL))
{
temp = (*t);
(*t) = (*t) -> right;
free (temp);
}
// ADD AN ELSE
else if (((*t) -> left != NULL)&&((*t) -> right != NULL))
{
temp = (*t)-> right;
while (temp -> left != NULL)
{
temp = (*t) -> left;
}
temp -> left = (*t) ->left;
temp = (*t);
(*t) = (*t) -> right;
free (temp);
}
}
else {
if ( x <= (*t) -> data)
delete_tree(&(*t) -> left, x);
else
delete_tree(&(*t) -> right, x);
}
}
else printf("No data found.\n");
}
```

Programming

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

There needs to be a return in your different cases - or an else before each