二叉搜索树分割故障

时间:2016-10-11 16:40:02

标签: c++ binary-search-tree

我正在为电影租赁业务制作二元搜索树(理论上)。如果键入了电影租赁业务所没有的标题,我需要输出“未找到电影”。我不知道如何在没有每次都出现分段错误的情况下执行此操作。我不知道还有什么可以尝试我这个代码是有道理的。我只是一个初学者 - 请放轻松我。任何帮助将不胜感激! (你可以找到我的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; 
    }
    ///
}

}

1 个答案:

答案 0 :(得分:1)

代码中的一些评论/错误

  • 您可以删除不再有用的条件:它会降低代码的可读性。

    while (foundMovie != NULL) 
    { 
        if (foundMovie == NULL) // false by construction!!!
        ...
    }
    
  • foundMovie->parent = foundMovie;应该会消失。它创建了一个循环依赖!

  • 您的上一个语句if (foundMovie->title == title)可能会导致分段错误 此时foundMovie可以为NULL。

这是一个可能的重写:

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