简体   繁体   English

二叉搜索树分割故障

[英]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. (您可以找到我的if函数,该函数尝试在代码末尾的三个斜杠(///)之间输出“找不到电影”。

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; 
    }
    ///
}

} }

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. 您的最后一条语句if (foundMovie->title == title)可能会造成分段错误,因为foundMovie可以为NULL。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM