C ++ AVLtree删除方法

时间:2013-11-24 00:00:44

标签: c++ avl-tree

我真的坚持如何在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;
       }

1 个答案:

答案 0 :(得分:0)

互联网上有many这类算法的抽象例子。

如果没有源代码,很难提供具体的帮助,因为我不知道你所谓的方法。