首页 > 解决方案 > 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 的实例。

标签: c++avl-tree

解决方案


推荐阅读