C++ 错误:红黑树中存在无限循环,因为没有符合条件的if语句

C++ 错误:红黑树中存在无限循环,因为没有符合条件的if语句,c++,C++,我从这里得到了这个模板(这个网站上的其他人说是有效的) 我真的看不出这里有什么问题。我已经看了3个小时了。无限循环是平衡方法中的主要错误,这是因为它通过每个if语句而没有找到任何匹配的质量。此外,RBT中需要纠正的错误是,一行中有2个红色节点 建造师 MovieTree:: MovieTree(){ nil = new MovieNode; nil->isRed=false; root= nil; root->left=nil; root

我从这里得到了这个模板(这个网站上的其他人说是有效的)

我真的看不出这里有什么问题。我已经看了3个小时了。无限循环是平衡方法中的主要错误,这是因为它通过每个if语句而没有找到任何匹配的质量。此外,RBT中需要纠正的错误是,一行中有2个红色节点

建造师

MovieTree:: MovieTree(){

    nil = new MovieNode;
    nil->isRed=false;

    root= nil;
    root->left=nil;
    root->right=nil;

}
添加方法

void MovieTree::addMovieNode(int ranking, std::string title, int year, int quantity){

cout<<title<<" added."<<endl;

if(root == nil){

    root =new  MovieNode(ranking, title, year, quantity);
    root->isRed=false;
    root->left = nil;
    root->right = nil;
    root->parent = nil;
    int b = rbValid(root);
    cout<<b<<endl;
    return;
}



MovieNode *temp = root; //root
MovieNode* parent = nil; //parent


//node is node to add

while(temp != nil){//gets to bottom of tree

    parent = temp;//sets temp to parent of location for where node is added

    if(title < temp->title){
        temp=temp->left;
    }
    else{temp=temp->right;}
}

MovieNode *node;

if(title < parent->title){//adds to either left or right
    cout<<"added to left"<<endl;

    node = new MovieNode(ranking, title, year, quantity);
    node->left = nil;
    node->right = nil;
    node->parent = nil;

    node->isRed=true;

    parent->left = node;
    node->parent=parent;
}

else{
        cout<<"added to right"<<endl;

    node = new MovieNode(ranking, title, year, quantity);
    node->left = nil;
    node->right = nil;
    node->parent = nil;

    node->isRed=true;

    parent->right=node;

    node->parent=parent;
}
int a = rbValid(root);
cout<<a<<endl;
rbAddFixup(node);//seg fault is here

}
void MovieTree::addMovieNode(整数排名,标准::字符串标题,整数年,整数数量){
coutparent=父母;
}
否则{
coutisRed=真;
父->右=节点;
节点->父节点=父节点;
}
int a=rbValid(根);
coutleft){//第一个事件不适用
MovieNode*叔叔=节点->父节点->父节点->右侧;
如果(叔叔->isRed==真){
节点->父节点->isRed=false;//开始rbcase1
叔叔->伊斯雷德=假;
节点->父节点->父节点->isRed=true;//结束rbcase1
节点=节点->父节点->父节点;
}
否则{
如果(节点==节点->父节点->右侧){
节点=节点->父节点;
左旋转(节点);
节点->父节点->isRed=false;//案例3
节点->父节点->父节点->isRed=true;
右旋转(节点->父节点->父节点);//结束案例3
}
}
}否则{
MovieNode*叔叔=节点->父节点->父节点->左侧;
如果(叔叔->isRed==真){
节点->父节点->isRed=false;
叔叔->伊斯雷德=假;
节点->父节点->父节点->isRed=true;
节点=节点->父节点->父节点;
}
否则{
//无限循环发生在这里
如果(节点==节点->父节点->左){
节点=节点->父节点;
右旋转(节点);
节点->父节点->isRed=false;
节点->父节点->父节点->isRed=true;
左旋转(节点->父节点->父节点);
}
}
}
}
root->isRed=false;
}
减少:

while(condition1)
{
    if(condition2)
    {
         statements
    } else {
         statement that doesn't affect condition1
         if (condition2)
         {
             statements
         } else {
             // infinite loop happens here
             if (condition3)
             {
                statements
             }
         }
    }
}
问题:

您有一个while循环,它会下降到无法前进的状态。你的评论很好地定位了这个职位。如果
condition1
为真,而
condition3
为假,则不会发生任何事情,因此代码永远不会停止


我很确定剩下的案例是
node==node->parent->right
,但我不确定是否能够编写它。红黑不是对称的。

那么我链接的示例代码是如何工作的呢?它只有和我一样的箱子?
while(condition1)
{
    if(condition2)
    {
         statements
    } else {
         statement that doesn't affect condition1
         if (condition2)
         {
             statements
         } else {
             // infinite loop happens here
             if (condition3)
             {
                statements
             }
         }
    }
}