删除节点功能不会从二叉搜索树中删除某些节点

时间:2016-03-24 22:43:47

标签: c++ pointers binary-search-tree

树按字母顺序排列为单词的第一个字母。有些话我可以删除,有些我不能删除。就像在我尝试删除它并没有任何反应,但它仍然退出deleteWord函数。它也会在树中留下两个字,当我尝试删除其中一个时,程序什么也不做。它只是给了我一个空白行,我无法输入任何东西;它在某个地方停留了一段时间,但我无法弄清楚原因。有时我尝试删除一个单词,而不是删除它,它被另一个节点的数据替换,我有一个重复的节点。我知道这里有一个逻辑错误,但我找不到它。

删除字的代码是:

void BinaryTree::deleteWord(string delWord)
{
current = root;
bool found = false;
while(!found)
{
    if(current -> data.getWord() == delWord)
    {
        if(current -> right)
        {
            if(current -> right -> left)
            {
                temp = current -> right;
                temp -> parent = current;
                while(temp -> left)
                {
                    temp -> left -> parent = temp;
                    temp = temp -> left;
                }

                if(temp -> right)
                {
                    current -> data = temp -> data;
                    temp -> data = temp -> right -> data;
                    temp -> parent -> left = temp -> right;
                    temp = NULL;
                    found = true;
                }
                else
                {
                    current -> data = temp -> data;
                    temp -> parent -> left = NULL;
                    temp = NULL;
                    found = true;
                }
            }

            else if(current -> right -> right)
            {
                current -> data = current -> right -> data;
                current -> right -> right -> parent = current;
                current -> right = current -> right -> right;

                found = true;
            }
            else
            {
                current -> data = current -> right -> data;
                current -> right = NULL;
                found = true;
            }

        }
        else if(current -> left)
        {
            if(current -> left -> right)
            {
                if(current -> left -> right -> left)
                {
                    temp = current -> left -> right;
                    temp -> parent = current -> left;


                    while (temp -> left)
                    {
                        temp -> left -> parent = temp;
                        temp = temp -> left;
                    }

                    current -> data = temp -> data;

                    if(temp -> right)
                    {
                        temp -> data = temp -> right -> data;
                        temp -> right = NULL;
                        found = true;
                    }
                    else
                    {
                        temp = NULL;
                        found = true;
                    }

                }

                else if(current -> left -> right -> right)
                {
                    temp = current -> left -> right;
                    current -> data = temp -> data;
                    current -> left -> right = temp -> right;
                    temp -> right -> parent = current -> left;
                    temp = NULL;
                    found = true;
                }
                else
                {
                    current -> data = current -> left -> right -> data;
                    current -> left -> right = NULL;
                    found = true;
                }
            }
            else if(current -> left -> left)
            {
                current -> data = current -> left -> data;
                current -> left -> left -> parent = current;
                current -> left = current -> left -> left;
                found = true;
            }
            else
            {
                current -> data = current -> left -> data;
                current -> left = NULL;
            }
        }
        else
        {
            current = NULL;
            found = true;
        }
    }
    else if(current -> data.getWord() > delWord)
    {
        if(current -> left)
            current = current -> left;
    }

    else if(current -> data.getWord() < delWord)
    {
        if(current -> right)
            current = current -> right;
    }
    else
    {
        cout << "word somehow not found, check code.";
        found = true;
    }
}
}

显示数组的代码是:

void BinaryTree::displayAll(WordNode* n)
{
if(n)
    {
        displayAll(n->right);
        print(n -> data);
        displayAll(n->left);
    }
}

并向树中添加单词

void BinaryTree::addWord(string newWord, string newMeaning)
{
WordNode* temp = new WordNode;
if(empty())
{
    temp -> data = Word(newWord, newMeaning);
    temp -> left = NULL;
    temp -> right = NULL;
    temp -> parent = NULL;
    root = temp;

}//end if
else
{
    current = root;
    while(current)
    {
        if(newWord > current -> data.getWord())
        {
            if(current -> right == NULL)
            {
                temp -> data = Word(newWord, newMeaning);
                temp -> right = NULL;
                temp -> left = NULL;
                temp -> parent = current;
                current -> right = temp;
                current = NULL;
            }
            else
                current = current -> right;
        }//if
        else if(newWord < current -> data.getWord())
        {
            if(current -> left == NULL)
            {
                temp -> data = Word(newWord, newMeaning);
                temp -> right = NULL;
                temp -> left = NULL;
                temp -> parent = current;
                current -> left = temp;
                current = NULL;
            }
            else
                current = current -> left;
        }//elseif
    }//while
}//else
}//funct

1 个答案:

答案 0 :(得分:0)

编辑了最后的else语句:

else
        {
            cout << "else entered.\n";
            if(current -> data.getWord() < current -> parent -> data.getWord())
            {
                current -> parent -> left = NULL;
                current = NULL;
                found = true;
            }
            else if(current -> data.getWord() > current -> parent -> data.getWord())
            {
                current -> parent -> right = NULL;
                current = NULL;
                found = true;
            }
            else 
            {
                current = NULL;
                found = true;
            }
        }