2

ツリーのバランス部分に問題が発生しました。再帰的な挿入の後にcheckBalが呼び出されました。5、2、4を追加しようとすると、2のバランスがチェックされ、5まで戻り、右回転の左の部分に移動します。これは正しいことです。しかし、rotateLeft関数は2行目でエラーになります。

この実装の何が問題になっていますか?私はあちこちを検索し、私がしたことを人々がそれがどのように行われたかについて話す方法と比較しました。私はついにすべてが機能するようになりました。最後にNをKに設定するのを忘れていました。

//==============================================================================
//===== Set Balance ============================================================
sNode<T>* checkBal(sNode<T> *locRoot)
{
    // Make sure to check the children are balanced as well.
    if (locRoot->left != NULL)
        locRoot->left = checkBal(locRoot->left);
    if (locRoot->right != NULL)
        locRoot->right = checkBal(locRoot->right);

    if(getHeight(locRoot->left) - getHeight(locRoot->right) > 1)
    {
        if(getHeight(locRoot->left->right) > getHeight(locRoot->left->left))
            locRoot->left = rotateRight(locRoot->left);
        locRoot = rotateLeft(locRoot);
    }
    else if(getHeight(locRoot->right) - getHeight(locRoot->left) > 1)
    {
        if(getHeight(locRoot->right->left) > getHeight(locRoot->right->right))
            locRoot->right = rotateLeft(locRoot->right);
        locRoot = rotateRight(locRoot);
    }
    updateHeights(locRoot);
    return locRoot;
}
    /*
        Extream cases of balancing a tree requires a double rotation
            A
             \
              D
             /
            B

        'A' is the current root
        If right->left (grandchild) is larger then the right->right (grandchild)
        First Right rotate the child then left rotate the parent


        left > right by 2 or more
            left.left < left.right  (Double Right Rotation)
            left.left >= left.right (Single Right Rotation)
        right > left by 2 or more
            right.right < right.left (Double Left Rotation)
            right.right >= right.left (Single Left Rotation)
    */

sNode<T>* rotateRight(sNode<T> *N) const
{
/*
      N           K
     / \         / \
   (a)  K  =>   N  (c)
       / \     / \
     (b) (c) (a) (b)
*/
    // K is going to be our new Parent
    // Move (c) from K->right to N->left
    // Set K->right to be N
    // Return the new parent node to update the one above.
    sNode<T> *K = N->right;
    N->right = K->left;        
    K->left = N;
    return N = K;
}
4

2 に答える 2

2
rotateRight(locRoot->left);

あるべきです、

rotateRight(locRoot->right);

しかし、それでも間違った実装です。=p

ルートの左側と右側で異なる実装が必要です。ウィキペディアのアニメーション
を見てみてください。

于 2012-06-05T04:48:45.127 に答える
0

少しいじってみたら動作しました。私の解決策は以下の通りです。

//==============================================================================
//===== AVL Balance ============================================================
sNode<T>* checkBal(sNode<T> *locRoot)
{
    // Go all the way down to the leaf nodes.
    if (locRoot->left != NULL)
        locRoot->left = checkBal(locRoot->left);
    if (locRoot->right != NULL)
        locRoot->right = checkBal(locRoot->right);

    // Before we do anything lets update the parent/child heights
    updateHeights(locRoot);

    if(getHeight(locRoot->left) - getHeight(locRoot->right) > 1)
    {
        // If it needs a double left rotate first rotate the left child right
        if(getHeight(locRoot->left->right) > getHeight(locRoot->left->left))
            locRoot->left = rotateRight(locRoot->left);
        locRoot = rotateLeft(locRoot);
    }
    else if(getHeight(locRoot->right) - getHeight(locRoot->left) > 1)
    {
        // If it needs a double right rotate first rotate the right child left
        if(getHeight(locRoot->right->left) > getHeight(locRoot->right->right))
            locRoot->right = rotateLeft(locRoot->right);
        locRoot = rotateRight(locRoot);
    }
    // Update the new heights
    updateHeights(locRoot);
    return locRoot;
}
    /*
        Extream cases of balancing a tree requires a double rotation
            A
             \
              D
             /
            B

        'A' is the current root
        If right->left (grandchild) is larger then the right->right (grandchild)
        First Right rotate the child then left rotate the parent


        left > right by 2 or more
            left.left < left.right  (Double Right Rotation)
            left.left >= left.right (Single Right Rotation)
        right > left by 2 or more
            right.right < right.left (Double Left Rotation)
            right.right >= right.left (Single Left Rotation)
    */

sNode<T>* rotateRight(sNode<T> *N) const
{
/*
      N           K
     / \         / \
   (a)  K  =>   N  (c)
       / \     / \
     (b) (c) (a) (b)
*/
    // K is going to be our new Parent
    // Move (c) from K->right to N->left
    // Set K->right to be N
    // Return the new parent node to update the one above.
    sNode<T> *K = N->right;
    N->right = K->left;        
    K->left = N;
    return N = K;
}

sNode<T>* rotateLeft(sNode<T> *N) const
{
/*
         N            K
    / \          / \
       K  (a)  =>  (b)  N
      / \              / \
    (b) (c)          (c) (a)
*/
    sNode<T> *K = N->left;
    N->left = K->right;        
    K->right = N;
    return N = K;
}
于 2012-06-20T20:35:21.370 に答える