asked on # Crashing when deleting value with no child in Binary Tree

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. :)

ProgrammingC

Log in or sign up to see answer

Become an EE member today7-DAY FREE TRIAL

Members can start a 7-Day Free trial then enjoy unlimited access to the platform

or

Learn why we charge membership fees

We get it - no one likes a content blocker. Take one extra minute and find out why we block content.

Not exactly the question you had in mind?

Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.

ask a questionLog in or sign up to see answer

Become an EE member today7-DAY FREE TRIAL

Members can start a 7-Day Free trial then enjoy unlimited access to the platform

or

Learn why we charge membership fees

We get it - no one likes a content blocker. Take one extra minute and find out why we block content.

Not exactly the question you had in mind?

Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.

ask a question
Thank you! I did try adding elses before the ifs but I may have added them in the wrong places.

You are welcome.

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!

James Murphy