Megan Byers Megan Byers - 1 month ago 33
C++ Question

Binary Search Tree segmentation fault

I am making a binary search tree for a movie rental business (theoretically). I need to output "Movie not found" if a title that the movie rental business doesn't have is typed in. I'm not sure how to do this without getting a segmentation fault every single time. I'm not sure what else to try-to me this code makes sense. I'm only a beginner- please go easy on me. Any help would be greatly appreciated! (You can find my if function that attempts to output "Movie not found" in between the three slashes (///) towards the end of my code.

void MovieTree::rentMovie(std::string title)
{
MovieNode *foundMovie = root;
//MovieNode *parent = NULL;
if (root == NULL)
{
cout << "Movie not found." << endl;
return;
}
else
{
foundMovie = root;
while (foundMovie != NULL)
{
if (foundMovie == NULL) //tree is empty
{
cout << "Movie not found." << endl;
}

else
{
if (foundMovie->title.compare(title) > 0) //uses ASCII to determine where titles are
{
foundMovie->parent = foundMovie;
foundMovie = foundMovie ->leftChild;
//cout << "printed left" << endl; //debugging
if (foundMovie->title.compare(title) == 0 && foundMovie->quantity > 0)
{
foundMovie->quantity--;
cout << "Movie has been rented." << endl;
//Title entered matches title found
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" << foundMovie->ranking << endl;
cout << "Title:" << foundMovie->title << endl;
cout << "Year:" << foundMovie->year << endl;
cout << "Quantity:" << foundMovie->quantity << endl;
break;
}

else if (foundMovie->quantity ==0)
{
//If movie is out of stock
cout << "Movie out of stock." << endl;
break;
}
}
else //check rightChild
{
foundMovie->parent = foundMovie;
foundMovie = foundMovie->rightChild;
//cout << "printed right" << endl; //debugging
if (foundMovie->title.compare(title) == 0 && foundMovie->quantity > 0) //title entered matches title found
{
foundMovie->quantity--;
cout << "Movie has been rented." << endl;
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" << foundMovie->ranking << endl;
cout << "Title:" << foundMovie->title << endl;
cout << "Year:" << foundMovie->year << endl;
cout << "Quantity:" << foundMovie->quantity << endl;
break;
}
else if (foundMovie->quantity ==0)
{
//movie is found but out of stock
cout << "Movie out of stock." << endl;
break;
}
}
}
}
///
if (foundMovie->title == title)
{
cout << "found the movie" << endl;
}
else
{
cout << "Movie not found." << endl;
}
///
}


}

Answer

Some remarks/bugs in your code

  • You can remove no more useful conditions: it reduces the readability of your code.

    while (foundMovie != NULL) 
    { 
        if (foundMovie == NULL) // false by construction!!!
        ...
    }
    
  • foundMovie->parent = foundMovie; should disappear. It creates a circular dependency !

  • Your last statement if (foundMovie->title == title) can create a segmentation fault since foundMovie can be NULL at that point.

Here is a possible rewriting:

void MovieTree::rentMovie(std::string title)
{ 
   MovieNode *foundMovie = root; 
   if (root == NULL) 
   { 
       cout << "Movie not found." << endl;
       return; 
   }

   foundMovie = root; 
   while (foundMovie != NULL) 
   { 
       int compareTitle = foundMovie->title.compare(title); //uses ASCII to determine where titles are 
       if (compareTitle > 0)
       { 
           foundMovie = foundMovie ->leftChild; 
       } 
       else if (compareTitle < 0) //check rightChild 
       { 
           foundMovie = foundMovie->rightChild; 
       }
       else { // compareTitle == 0
           if (foundMovie->quantity > 0) 
           { 
               foundMovie->quantity--;
               cout << "Movie has been rented." << endl;
               //Title entered matches title found 
               cout << "Movie Info:" << endl;
               cout << "===========" << endl; 
               cout << "Ranking:" << foundMovie->ranking << endl; 
               cout << "Title:" << foundMovie->title << endl; 
               cout << "Year:" << foundMovie->year << endl; 
               cout << "Quantity:" << foundMovie->quantity << endl;
               break;
           }
           else if (foundMovie->quantity ==0)
           {
               //If movie is out of stock 
               cout << "Movie out of stock." << endl; 
               break;
           }
        }
    }

    // be sure that foundMovie->title == title is equivalent to foundMovie->title.compare(title) == 0
    if (foundMovie && foundMovie->title == title) 
    {
        cout << "found the movie" << endl; 
    } 
    else 
    { 
        cout << "Movie not found." << endl; 
    }
 }
Comments