5

ノードの削除後に avl のバランスをどのように調整する必要があるかはご存じのとおりです。まず、子のないノードを削除することを検討しています。

ツリーの例:

        10
      /     \
     5      17
   /  \    /  \ 
   2  9   12  20
    \           \
     3          50

deletevalue(12); としましょう。

次に、ツリーは削除後にある必要があります。

        10
      /     \
     5      17
   /  \       \ 
   2  9       20
    \           \
     3          50

ここで、ツリーがノード 17 でバランスが取れていることがわかります。これは、式により、そのバランス係数 = 高さ (左のサブツリー [左のツリーは null なので -1] ) - 高さ (右のサブツリー) = -2 であるためです。

そのため、右-右ケースか右-左ケースかをチェックして、ツリーのバランスを取ります。

If BalanceFactor(17's right) = -1
    perform SingleLeftRotation(17);
else if BalanceFactor(17's right) = -1
    perform DoubleRightLeftRotation(17);

同様に、Balance Factor 17 が 2 の場合、つまり高のままにすると、それぞれの回転が発生します。// bF(17) = 2 の場合

If BalanceFactor(17's left) = 1
    perform SingleLeftRotation(17);
else if BalanceFactor(17's left) = -1
    perform DoubleLeftRightRotation(17);

バランスを取った後、ツリーは次のようになります。

          10
      /     \
     5      20
   /  \    /  \ 
   2  9   17  50
    \           
     3  

これは私が設計した削除です。

メイン関数から、私は呼び出します

bool deletevalue(WA value)
{
    AvLNode<WA> *temp = search(root, value);    //calling search function to find node which has user-specified data & stored its address in temp pointer
    if(temp!=0) //if temp node is not null then
    {
        if(temp->left==0 && temp->right==0) //if temp node don't have any children
        {   deletewithNochild(root, value); }   //call to respective function
        else if( (temp->left!=0 && temp->right==0) || (temp->left==0 && temp->right!=0) )   //if temp node has any 1 child, left or right
        {   deletewithOneChild(temp);   }   //call to respective function
        else if(temp->left!=0 && temp->right!=0)    //if temp node has 2 children
        {   deletewith2Child(temp);     }   //call to respective function

        return true;    //for prompting respective output message
    }
    else
        return false;   //for prompting respective output message
}

必要なノードには子がないため、次の関数が呼び出されます。

void deletewithNochild(AvLNode<WA> *temp, WA value) //temp is node which is to be deleted
{
    if(value == root->key)  //if temp is root node then
    {
        delete root;    //free memory of root node
        root = 0;   //nullify root
    }
    else    //if temp is some other node 
    {
        if (value < temp->key)
        {
            deletewithNochild(temp->left, value);
        }
        else if (value > temp->key)
        {
            deletewithNochild(temp->right, value);
        }
        else if (value == temp->key)
        {
            AvLNode<WA> *father = findfather(temp, root);   //calling findfather func to find father of temp node & store its address in father node pointer

            if(father->left==temp)  //if temp is left child of its father
            {
                delete temp;    //free memory of temp node
                father->left=0; //nullify father's left
            }
            else if(father->right==temp)    //if temp is right child of its father
            {
                delete temp;    //free memory of temp node
                father->right=0;//nullify father's right
            }
            return;
        }
        cout<<"\nBalancing";
        if ( balancefactor(temp) == 2)  //if temp is left higher, ie. temp's Balance Factor = 2, then
        {
            cout<<"\t2 ";
            if ( balancefactor(temp->left) == 1 ) //if temp's left node has Balance Factor 1 then
            {
                SingleRightRotation(temp);  //send temp node for rotation because temp is unbalance
            }
            else if ( balancefactor(temp->left) == -1 ) //if temp's left node has Balance Factor -1, then
            {
                DoubleLeftRightRotation(temp);  //send temp for double rotation because temp is unbalance
            }
        }
        else if ( balancefactor(temp) == -2 )   //if temp is right higher, ie. temp's Balance Factor = -2, then
        {
            cout<<"\t-2 ";
            if ( balancefactor(temp->right) == -1 ) //if temp's left node has Balance Factor -1 then
            {
                SingleLeftRotation(temp);   //send temp node for rotation because temp is unbalance
            }
            else if ( balancefactor(temp->right) == 1 ) //if temp's right node has Balance Factor 1, then
            {
                DoubleRightLeftRotation(temp);  //send temp for double rotation because temp is unbalance
            }
        }

    }
}

ノードの HEIGHT とノードの BALANCE FACTOR の 2 つの効用関数を次に示します。

int heightofnode(AvLNode<WA> *temp) const
{
    return temp==NULL ? -1 : temp->height;
}


int balancefactor(AvLNode<WA> *temp) const
{
    return ( heightofnode(temp->left) - heightofnode(temp->right) );
}

私の出力は、12 を削除すると (Breadth First Travers) -->> [10] [9] [17]になります。

親切に私を助けてください、再帰に問題はありますか? 何度もドライランしましたが、理解できません。削除は再帰を通じて行う必要があります。そうしないと、バランス ツリーがより大きな地獄になります。 時間を割いていただきありがとうございます。:-)

4

1 に答える 1

1

deletewithNochild() が他の delete* メソッドを呼び出すのはなぜですか? deletewithNochild が呼び出された場合は、削除するノードにいます。それを削除し、その親に移動して、親のバランス係数を確認し、必要に応じて回転させます。ルートに到達するまで、各ノードの親に対してリバランスを繰り返します。

参照が必要な場合は、 Java で AVL ツリーを実装しました。

于 2012-12-12T01:14:55.030 に答える