Solved

Crashing when deleting value with no child in Binary Tree

Posted on 2017-04-12
4
79 Views
Last Modified: 2017-04-12
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:

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");
}

Open in new window


I'm a noobie at programming so any help would be appreciated. :)
0
Comment
Question by:Jad Dava
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 42090398
Look at line 16 - what happens when you get there and you have just been through lines 9 - 14? You have wiped out your tree and then on line 16 you are testing it.

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

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 42090399
So either
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");
}

Open in new window

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");
}

Open in new window

0
 

Author Comment

by:Jad Dava
ID: 42090490
Thank you! I did try adding elses before the ifs but I may have added them in the wrong places.
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 42090497
You are welcome.
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question