【问题标题】:C++ AVLtree deletion methodC++ AVLtree删除方法
【发布时间】:2013-11-24 00:00:43
【问题描述】:

我真的很纠结如何在 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;
       }

【问题讨论】:

  • 提示:总是在叶级删除。
  • 你是指将所有节点向上移动,以便删除的节点在底部?
  • 不,关键是你不应该洗牌。我本来可以更清楚一点,就最大或最小而言,这将是一片叶子,但这并不容易解释。 The link supplied by strigon33 做得更好。
  • 那好吧。这种方法会破坏树的平衡吗?
  • 可以的。一一阅读说明——“7.Retrace path back up the tree(从节点Z的父节点开始)到根,根据需要调整平衡因子。”

标签: c++ avl-tree


【解决方案1】:

互联网上有 many 抽象解释此类算法的示例。

没有源代码很难提供具体的帮助,因为我不知道你的方法叫什么。

【讨论】:

    猜你喜欢
    • 2013-12-08
    • 2021-03-03
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2017-02-21
    • 2011-03-03
    • 2021-06-14
    • 2012-10-05
    相关资源
    最近更新 更多