Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Read the following code and fill in blank #1. TreeNode* Balance(TreeNode* T) //

ID: 3900856 • Letter: R

Question

Read the following code and fill in blank #1.

TreeNode* Balance(TreeNode* T)

// Checks and balances the subtree T.

{

int balanceFactor = Difference(T);

if (balanceFactor > 1) {

    if (Difference(T->left) > 1)

      return RotateRight(T);        // #1

    else

      return RotateLeftRight(T);    // #2

}

else if (balanceFactor < -1) {

    if (Difference(T->right) < 0)

      return RotateLeft(T);         // #3

    else

      return RotateRightLeft(T);    // #4

}

else

    return T;

}

[1] RotateLeft(T)

[2] RotateLeftright(T)

[3] RotateRight(T)

[4] RotateRightLeft(T)

Explanation / Answer

int balanceFactor = Difference(T); // It check the height difference of tree

if (Difference(T->left) > 1) // (T->left) > 1) it refers to the tree is un balanced tree and left tree height is more

return RotateRight(T);   // It indicates that the tree need to balance for that RotateRight(T) Operation is performed

Option 3 correct