CJCoding With Joseph
15free / daySign inCreate account

Rotate an AVL Subtree Left

Complete rotateLeft(Node x) so this AVL tree can fix itself when values keep arriving on the right.

Reminder: what a left rotation does. A left rotation is the mirror image of a right rotation. When a node's balance factor reaches -2 its right side is too tall, so its right child y is lifted into its place and the old parent x drops down to y's left. The middle subtree, B in the picture, holds the values between x and y, so it moves from y's left to x's right. The in-order order does not change, only the shape and the heights.

    x                       y
   / \                     / \
  A   y        --->       x   C
     / \                 / \
    B   C               A   B

This tree's insert already calls rotateLeft for the right-right case: a value went into the right subtree of a node's right child and that node's balance factor dropped below -1. The other three cases come later, in the question that builds the full AVL insert.

Requirements: 1. rotateLeft receives the top of the subtree, x. Its right child is never null when it is called. 2. Make x's right child y the new top: move y's left subtree to x's right, then make x the left child of y. 3. Update the heights with the provided updateHeight(Node), x first because it is now lower, then y. 4. Return y, the new top. Do not change insert.

The provided preOrder() lists every node in pre-order as its value followed by its height, for example 30(h2).

The Node class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class.

Do NOT write a main method.

Expected Output:

20(h2) 10(h1) 30(h1)
20(h3) 10(h1) 40(h2) 30(h1) 50(h1)
10 20 30 40 50

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsAVL Trees
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.