0
0
Data Structures Theoryknowledge~10 mins

AVL tree rotations in Data Structures Theory - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to perform a single right rotation on a node in an AVL tree.

Data Structures Theory
def right_rotate([1]):
    y = [1].left
    T2 = y.right

    y.right = [1]
    [1].left = T2

    return y
Drag options to blanks, or click blank then click option'
Az
Bx
Cnode
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Forgetting to update the left or right child pointers.
2fill in blank
medium

Complete the code to perform a single left rotation on a node in an AVL tree.

Data Structures Theory
def left_rotate([1]):
    y = [1].right
    T2 = y.left

    y.left = [1]
    [1].right = T2

    return y
Drag options to blanks, or click blank then click option'
Aroot
Bnode
Ctemp
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up left and right child pointers.
Using inconsistent variable names.
3fill in blank
hard

Fix the error in the code that performs a left-right rotation in an AVL tree.

Data Structures Theory
def left_right_rotate([1]):
    [1].left = left_rotate([1].left)
    return right_rotate([1])
Drag options to blanks, or click blank then click option'
Anode
Broot
Ctemp
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the node parameter.
Incorrect order of rotations.
4fill in blank
hard

Fill both blanks to complete the right-left rotation function in an AVL tree.

Data Structures Theory
def right_left_rotate([1]):
    [1].right = [2]([1].right)
    return left_rotate([1])
Drag options to blanks, or click blank then click option'
Anode
Broot
Cleft_rotate
Dright_rotate
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of rotations.
Using incorrect function names for rotations.
5fill in blank
hard

Fill all three blanks to complete the balance factor calculation and rotation decision in an AVL tree node insertion.

Data Structures Theory
def insert([1], key):
    if not [1]:
        return Node(key)

    if key < [1].key:
        [1].left = insert([1].left, key)
    else:
        [1].right = insert([1].right, key)

    balance = height([1].left) - height([1].right)

    if balance > 1 and key < [1].left.key:
        return right_rotate([1])

    if balance < -1 and key > [1].right.key:
        return left_rotate([1])

    if balance > 1 and key > [1].left.key:
        [1].left = left_rotate([1].left)
        return right_rotate([1])

    if balance < -1 and key < [1].right.key:
        [1].right = right_rotate([1].right)
        return left_rotate([1])

    return [1]
Drag options to blanks, or click blank then click option'
Anode
Broot
Ccurrent
Dtemp
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Forgetting to return the updated node after rotations.