[英]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. 就像我尝试删除它一样,什么也没有发生,但是它仍然退出deleteWord函数。 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语句编辑为:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.