C++AVLtree删除方法

C++ AVLtree deletion method

本文关键字:方法 删除 C++AVLtree      更新时间:2023-10-16

我真的很纠结于如何在C++中实现avltree的删除方法。我真的不知道从哪里开始。我有找到节点的方法、旋转方法和插入方法,但我真的不确定如何启动这种删除方法。我知道这是一个模糊的,网上有资源,但我发现他们不是很清楚。有人能解释一下这个过程吗?

如果你想看到任何代码,请询问:)

如有任何建议,我们将不胜感激。非常感谢!

-----编辑-----

查找节点方法:

Node* AVLtree::findNode(string cityID){
       Node *thisNode = this->rootNode;
       while(thisNode!=0){            
             int location = thisNode->getCity()->getName().compare(cityID);
             if(location==0){return thisNode;
             }else if(location<0){//cout<<"NODE: " << thisNode->getCity()->getName()<<endl;
             thisNode = thisNode->getChildR();
             }else{thisNode = thisNode->getChildL();}  
       }
       return thisNode; 
       }

向左旋转:

  Node* AVLtree::rotateLeft(Node* node){
       Node* tempParent= node->getParent();
       Node* tempNode = node->getChildL();
       node->setChildL(tempNode->getChildR());
       tempNode->setChildR(node);
       if(tempParent==0){tempNode->removeParent();this->rootNode=tempNode;tempNode->getNewHeight();}
       else{tempParent->setChildR(tempNode);
            }
       return tempNode;
       }

向右旋转:

  Node* AVLtree::rotateRight(Node* node){
       Node* tempParent = node->getParent();
       Node* tempNode = node->getChildR();
       node->setChildR(tempNode->getChildL());
       tempNode->setChildL(node);
       if(tempParent==0){tempNode->removeParent();this->rootNode=tempNode;tempNode->getNewHeight();}
       else{tempParent->setChildR(tempNode);}
       return tempNode;
       }

双重旋转,用于插入不平衡节点右侧子项的左侧子树:

  Node* AVLtree::rotateTwoRights(Node* node){
        node = rotateLeft(node->getChildR());
        node = rotateRight(node->getParent());
        return node;
        }

左右双旋转条件:

  Node* AVLtree::rotateTwoLefts(Node* node){
        node = rotateRight(node->getChildL());
        node = rotateLeft(node->getParent());
        return node;
        }

插入方式:

Node* AVLtree::insertNode(Node* parent, Node* node, City *city, int side){
       if(node == 0){
               if(side==0){
                     node = parent->setChildL(new Node(city));
               }else if(side==1){
                     node = parent->setChildR(new Node(city));
               } 
       }else if(node->getCity()->getName().compare(city->getName())<0){ //Right case
             parent = node;
             node = insertNode(parent, node->getChildR(), city, 1);
             parent->getNewHeight();
             if(parent->getBalance()==2){
                   if(parent->getChildR()->getCity()->getName().compare(city->getName())<0){
                         node = rotateRight(parent);
                   }else{
                         node = rotateTwoRights(parent);
                   }                                
             }
       }else if(node->getCity()->getName().compare(city->getName())>0){ //Left case
             parent = node;
             node = insertNode(parent, node->getChildL(), city, 0);
             parent->getNewHeight();
             if(parent->getBalance()==-2){
                   if(parent->getChildL()->getCity()->getName().compare(city->getName())>0){
                         node = rotateLeft(parent);
                   }else{
                         node = rotateTwoLefts(parent);
                   }
             }    
       }else{
             node=0;
       }
       return node;
       }

互联网上有许多抽象解释的此类算法的例子。

如果没有源代码,很难提供特定的帮助,因为我不知道你称你的方法为什么。