如何修复二进制搜索树中的逻辑错误? 我一直在尝试让一个旧的C++二进制搜索树程序来工作。它编译并运行,但我没有得到我所期望的结果。如果我按顺序插入c、d、a、b并尝试删除c,则删除函数将跳过按顺序查找后继项的If条件。为什么跳过了这两个条件句

如何修复二进制搜索树中的逻辑错误? 我一直在尝试让一个旧的C++二进制搜索树程序来工作。它编译并运行,但我没有得到我所期望的结果。如果我按顺序插入c、d、a、b并尝试删除c,则删除函数将跳过按顺序查找后继项的If条件。为什么跳过了这两个条件句,c++,binary-search-tree,C++,Binary Search Tree,它也是使用gcc编译的 Node::Node(string nodeItem, int nodeLine){ item=nodeItem; vector<int> tempVector; tempVector.push_back(nodeLine); lines=tempVector; leftPtr = NULL; rightPtr = NULL; } // recursive method for fi

它也是使用gcc编译的

  Node::Node(string nodeItem,
           int nodeLine){
    item=nodeItem;
    vector<int> tempVector;
    tempVector.push_back(nodeLine);
    lines=tempVector;
    leftPtr = NULL;
    rightPtr = NULL;
}


// recursive method for finding node containing the word
Node* BST::find(string data, Node *curr) {

    if(curr==NULL) {
        cout << data << " is not in the tree" << endl;
        return curr;

    }
    if(curr->getItem().compare("theplaceholder")==0){
        return curr;
    }
    string tempItem = curr->getItem();
    //this if statement is if I am inserting a word that is already in the tree
    // or if I am removing the word from the tree
    if(data.compare(tempItem)==0){
        return curr;
    }
    else if(data.compare(tempItem)<0){
        return find(data,curr->getLeftPtr());
    }
    else{
        return find(data, curr->getRightPtr());
    }

}


void BST::insert(string data, int fromLine) {
    Node *curr;


    curr=find(data, root); 


    if(curr!=NULL && curr->getItem().compare("theplaceholder")==0){

        curr->setData(data);

        curr->addLines(fromLine);
    }


    if(curr==NULL){

        // I want to point to a nonNull node.
        // I am making a new node and having curr point to that instead of NULL
        //then I set it to



        curr=new Node(data, fromLine);


        cout <<curr->getItem() << endl;

        vector<int> foundLines=curr->getNodeLines();
        //cout<< "The word " <<curr->getItem() << " can be found in lines ";
        if(foundLines.empty())
            cout << "foundLines is empty";
        int size=foundLines.size();
        for(int count=0; count<size; count++){

            //cout << foundLines[count] << ", ";
        }

    }

    if(curr->getItem()==data){
        curr->addLines(fromLine);
    }
}
// remove method I am trying to check for in order successors to swap with the deleted node.
void BST::remove(string data) {
    Node *curr=root;


    Node *temp=find(data, curr);
    if(temp==NULL){
        cout << " nothing to remove" << endl;
    }
    else if(temp->getRightPtr()!=NULL){

        curr=temp->getRightPtr();
        cout << curr->getItem() << endl;
        while(curr->getLeftPtr()!=NULL){
            curr=curr->getLeftPtr();
            cout << curr->getItem() << endl;
        }
        temp->setData(curr->getItem());

        temp->setLines(curr->getNodeLines());
        delete curr;
        curr=NULL;
    }
    else if(temp->getLeftPtr()!=NULL){
        cout <<"if !temp->getLeftPtr" << endl;
        curr=temp->getLeftPtr();
        cout << curr->getItem() << endl;
        while(curr->getRightPtr()!=NULL){
            curr=curr->getRightPtr();
            cout << curr->getItem() << endl;
        }
        temp->setData(curr->getItem());

        temp->setLines(curr->getNodeLines());
        delete curr;
        curr=NULL;
    }
    else{
        cout <<"else delete temp" << endl;
        delete temp;
        temp=NULL;

    }

}
Node::Node(字符串nodeItem,
内节点线){
项目=节点项;
向量tempVector;
tempVector.推回(nodeLine);
直线=向量;
leftPtr=NULL;
rightpr=NULL;
}
//查找包含单词的节点的递归方法
Node*BST::find(字符串数据,Node*curr){
if(curr==NULL){
cout getRightPtr());
}
}
void BST::insert(字符串数据,int fromLine){
节点*curr;
curr=查找(数据,根);
如果(curr!=NULL&&curr->getItem()。比较(“theplaceholder”)==0){
当前->设置数据(数据);
curr->addline(fromLine);
}
if(curr==NULL){
//我想指向一个非空节点。
//我正在创建一个新节点,并使curr指向该节点,而不是NULL
//然后我把它设置为
curr=新节点(数据,fromLine);
cout getItem());
临时->设置行(curr->getNodeLines());
删除curr;
curr=NULL;
}
否则{
这条线的原因是什么

else if(temp->getRightPtr()!=NULL){
“永不成功”是指您从未在任何节点上设置正确的指针-getRightPtr只能返回null。如果您在构建树后在调试器中检查了树的状态,或者如果您使用了insert函数,您可能已经看到了这一点。问题是:

  • 如果节点不在树中,则find函数不会返回null,而insert函数希望返回null
  • 插入函数需要在树中定位此节点应位于的位置-通过修复find函数或自行定位,然后创建一个新节点,并根据需要从父节点的左侧或右侧添加对该节点的引用
  • insert函数会将行号显示到第一个插入的节点上两次:一次是在覆盖占位符时,一次是在插入结束时(我可能不会在此处使用占位符,而是在创建第一个节点时将
    root
    初始化为null,而将root=curr)
  • 从左侧分支提升最高节点时,remove函数需要做更多的工作;它需要
    • 正确地从上一个父节点清除该节点-在删除该对象时,但保留任何悬空指针
    • 升级该节点的所有子节点,然后再将其移动到上一个插槽
i、 e


没有人再使用缩进了吗?首先,当在类方法的范围内时,不需要使用
这个
指针。取出它们。我只使用eclipse的自动缩进。当我复制粘贴到这里并试图使所有代码变为灰色时,特定的格式可能已经丢失。@LostNitrogen:然后是你的两个
如果
con附加条件的计算结果永远不会为真。您需要调试、调试,然后再调试一些。设置一些断点,在树中插入项目。在纸上绘制树,然后按照逻辑逐步找出问题。@LostNitrogen:堆栈溢出是寻求帮助的最佳场所。您只需要将问题的格式设置得更好一点。删除与您的问题不相关的代码(您发布了很多)。添加您在调试期间通过
cout
语句获得的一些输出。我建议您显示以下输出:1)创建树。2)执行大约3或4次插入。3)执行删除。显示每个步骤的输出。
      D                                       C                           
     / \                                     / \
    A   E  remove 'D'                       A   E
     \       => 'C' is highest on left       \
      C         but need to move B to C       B
     /
    B