Rotate an AVL Subtree Right
Rotate an AVL Subtree Right
Complete rotateRight(Node y), the move an AVL tree uses to fix a subtree whose left side has grown two levels taller than its right.
Reminder: what a right rotation does. When a node's balance factor reaches 2, its left side is too tall. A right rotation lifts the left child x up into the parent's place and drops the old parent y down to x's right. The subtree in the middle, B in the picture, holds the values between x and y, so it changes parents: it moves from x's right to y's left. Reading the tree in order gives the same sorted list before and after, so it is still a valid BST - only the shape and the heights change.
y x
/ \ / \
x C ---> A y
/ \ / \
A B B COnly three links change, and only y and x can get a new height. y ends up below x, so its height has to be fixed first.
Requirements:
1. rotateRight receives the top of the subtree, y. Its left child is never null when it is called.
2. Make y's left child x the new top: move x's right subtree to y's left, then make y the right child of x.
3. Update the heights with the provided updateHeight(Node), y first and then x.
4. Return x, the new top of the subtree. rotateRootRight() stores what you return in root; insert in this tree never rotates on its own.
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:
30(h3) 20(h2) 10(h1) 20(h2) 10(h1) 30(h1) 10 20 30
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...