简体   繁体   中英

Delete node function will not delete certain nodes from a binary search tree

The tree is sorted alphabetically the first letter of words. Some words I can delete, some I cannot. As in I try to delete it and nothing happens, but it still exits the deleteWord function. It will also get down to two words left in the tree and when I try to delete one of them, the program does nothing. It simply gives me a blank line which i cannot enter anything into; its stuck in a while loop somewhere but I cant figure out why. Sometimes I try to delete a word and instead of it being deleted, it is replaced by the data of another node and I have a duplicate node. I know theres a logic error in here but I cant find it.

the code for the word deletion is:

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

the code to display the array is:

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

and for adding words to the tree

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

edited the final else statement to read:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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