c++ - AVL树删除c ++
问题描述
我在删除 AVL 树中的元素时遇到问题。
这是我的删除功能
template <class TYPE, class KTYPE>
bool AvlTree <TYPE, KTYPE> :: AVL_Delete (KTYPE dltKey)
{
// Local Definitions
bool shorter;
bool success;
NODE<TYPE> *newRoot;
// Statements
newRoot = _delete (tree, dltKey, shorter, success);
if (success)
{
tree = newRoot;
count--;
} // if
return success;
} // AVL_Delete
template <class TYPE, class KTYPE>
NODE<TYPE>* AvlTree<TYPE, KTYPE>
:: _delete (NODE<TYPE> *root,
KTYPE dltKey,
bool& shorter,
bool& success)
{
// Local Definitions
NODE<TYPE> *dltPtr;
NODE<TYPE> *exchPtr;
NODE<TYPE> *newRoot;
// Statements
if (!root)
{
shorter = false;
success = false;
return NULL;
} // if -- base case
if (dltKey < root->data.key)
{
root->left = _delete (root->left, dltKey,
shorter, success);
if (shorter)
root = dltRightBalance (root, shorter);
} // if less
else if (dltKey > root->data.key)
{
root->right = _delete (root->right, dltKey,
shorter, success);
if (shorter)
root = dltLeftBalance (root, shorter);
} // if greater
else
// Found equal node
{
dltPtr = root;
if (!root->right)
// Only left subtree
{
newRoot = root->left;
success = true;
shorter = true;
delete (dltPtr);
return newRoot; // base case
} // if true
else
if (!root->left)
// Only right subtree
{
newRoot = root->right;
success = true;
shorter = true;
delete (dltPtr);
return newRoot; // base case
} // if
else
// Delete NODE has two subtrees
{
exchPtr = root->left;
while (exchPtr->right)
exchPtr = exchPtr->right;
root->data = exchPtr->data;
root->left = _delete (root->left,
exchPtr->data.key,
shorter,
success);
if (shorter)
root = dltRightBalance (root, shorter);
} // else
} // equal node
return root;
} // _delete
这就是我想要实现的目标
for (int loop=0;loop<newIdea.size();loop++)
{
for (int j = 0; j < newIdea[loop].getKeyword().size(); j++) {
key = newIdea[loop].getKeyword()[j];
Index modIndex;
if (tree.AVL_Retrieve(key, modIndex)){
for (int i = 0; i < modIndex.idList.size(); i++) {
if (ID == modIndex.idList[i]){
modIndex.idList.erase(modIndex.idList.begin()+i);
tree.AVL_Delete(modIndex);
}
}
}
}
}
这是我得到的错误
[错误] 没有匹配函数调用 'AvlTree >::AVL_Delete(Index&)'
这是我要插入到我的树中的结构,它有效
struct Index {
string key;
vector<int> idList;
};
其中 key 表示一个单词,idList 表示该单词所在的整数 ID。我上面的代码旨在搜索树中的单词,将其从我的结构中的 Idlist 中删除,然后从我的 AVL 树中删除该单词和 ID 的实例。
解决方案
推荐阅读
- sql - 如何在 SQL 中发出查询请求以在主选择之前预选行
- python - 我的 Python 代码在尝试登录时不起作用?
- docker - github 操作 - 无法连接到 mysql
- bash - 有没有办法杀死 exec > >( process ) 为 I/o 重定向创建的进程
- python - 将不同的可调用对象传递给 uWSGI 而不是烧瓶应用程序对象
- sas - 使用 {ODS RTF CONTENTS=YES} 的目录不起作用
- model - 模型、元模型和元元模型
- mysql - MySQL 条件与 concat 似乎无法正常工作
- pandas - 我在将特定字典转换为 pandas 数据框时遇到了困难
- django - 单个表与每个用户的表