C++ 正在覆盖AVL树数据

C++ 正在覆盖AVL树数据,c++,pdf,data-structures,tree,avl-tree,C++,Pdf,Data Structures,Tree,Avl Tree,我正在写一个项目,该项目对多个PDF文件的单词进行索引,并将单词和文件位置一起存储到一个索引中,该索引是一个AVL树。我有一个问题,在解析PDF并将单词添加到AVL树索引的某个点之后,索引会删除/覆盖/执行某些操作,当索引完成时,树中只剩下一个PDF中的数据 下面是AVL树类的insert方法 template<class T> void avlTree<T>::insert(T data, avlNode<T>*& node){ //if w

我正在写一个项目,该项目对多个PDF文件的单词进行索引,并将单词和文件位置一起存储到一个索引中,该索引是一个AVL树。我有一个问题,在解析PDF并将单词添加到AVL树索引的某个点之后,索引会删除/覆盖/执行某些操作,当索引完成时,树中只剩下一个PDF中的数据

下面是AVL树类的insert方法

template<class T>
void avlTree<T>::insert(T data, avlNode<T>*& node){
    //if we are at a nullptr, then insert the new node
    if(node==nullptr){
        node = new avlNode<T>(data);
    }
    //if the data to insert is less than the node we are lookingg at
    //then next compare it to the left node
    else if(data < node->key){
        insert(data, node->left);
        //if left subtree is larger than right, then a
        //case 1 or 2 is present
        if(height(node->left)-height(node->right)==2){
            //if data was inserted to the left
            //LL or Case 1
            if(data<node->left->key){
                leftRotate(node);
            }
            //LR or case 2
            else{
                doubleLeftRotate(node);
            }
        }
    }
    //if greater then compare it to the right node
    else if(data>node->key){
        insert(data, node->right);
        //if right subtree is larger than the left, then
        //a case 3 or 4 is present
        if(height(node->right)-height(node->left)==2){
            //if data was inserted to the left
            //LL or Case 1
            if(data>node->right->key){
                //RR or case 4
                rightRotate(node);
            }
            //RL or case 3
            else{
                doubleRightRotate(node);
            }
        }
    }
    //if nodes are equal case
    else{
        //do nothing
    }

    //update the height of the node
    node->height = 1 + max(height(node->left),height(node->right));
}
AVL树节点包含IndexEntry对象。这是它的构造函数。entry是被索引的单词,entryLocation是单词出现的文件名向量,locCount是每个文件出现次数的向量

//argument constructor
indexEntry::indexEntry(string what, string where){
    entry = what;
    entryLocation.push_back(where);
    locCount.push_back(1);
}
最后,这里调用了一个索引项,将其添加到索引中,其中add是单词,是字符串

    indexEntry entry(add,fileName);
    //check if word is already indexed
    if(!(index->isIn(entry))){
        //if it is not in, add a new entry
        index->addWord(entry);
    }

该程序在大约1.5-2.5个pdf(取决于其大小)中按预期工作,然后整个树只包含一个pdf中的信息。只是想知道是什么导致了这种情况?

可能是旋转代码中的一个边缘情况被破坏了。当错误发生时,树包含多少节点?我认为@paddy可能是正确的。我开始用随机整数而不是对象测试Avl树,通常在大约10个节点之后发生。我可能不得不在纸上写下这棵树,看看哪个旋转是错误的。只是确认了旋转有问题。当必须更改根节点,并且未正确重新分配所有指针时发生。谢谢你的帮助。
    indexEntry entry(add,fileName);
    //check if word is already indexed
    if(!(index->isIn(entry))){
        //if it is not in, add a new entry
        index->addWord(entry);
    }