好的,对于我的项目,我正在使用AVL树制作字典。我已经成功启用了插入和平衡功能。但是,当我使用删除功能时,它什么也没做。我是新手,所以我可能会忽略一些基本的错误或失误。请帮助
我尝试了不同的方法,但是它似乎没有更新。我第一次使用C ++类是因为我在Java中没有这个问题。
struct pBook
{
string key;
string email;
string phNum;
string phType;
int height;
pBook *left;
pBook *right;
};
class Phonebook
{
private:
pBook *root;
public:
pBook* deleteC(pBook *node, std::string key)
{
if (node == NULL)
{
return node;
}
else if (!compare(key, node->key))
{
node->left = deleteC(node->left, key);
}
else if (compare(key, node->key))
{
node->right = deleteC(node->right, key);
}
else
{
if (node == NULL)
{
cout << "Error\n";
}
else if (node->left == NULL && node->right == NULL)
{
delete node;
}
else if (node->left == NULL)
{
pBook *temp = node->right;
free(temp);
return node;
}
else if (node->right == NULL)
{
pBook *temp = node->left;
free(temp);
return node;
}
else
{
pBook *temp = minValueNode(node->right);
node->key = temp->key;
node->right = deleteC(root->right, temp->key);
}
return node;
}
return node;
}
pBook * minValueNode(pBook *node)
{
pBook *current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
void callDelete(pBook *node,std::string key)
{
deleteC(root, key);
root = node;
}
};
删除前和删除后结果没有差异。