Operations On An AVL Tree
Operations On An AVL Tree
Left Rotate
In left-rotation, the arrangement of the nodes on the right is transformed into the arrangements on the
left node.
Algorithm
Right Rotate
In left-rotation, the arrangement of the nodes on the left is transformed into the arrangements on the
right node.
Else if y is the right child of its parent p, make x as the right child of p.
Else assign x as the left child of p. Assign the parent of y as the parent
of x.
In left-right rotation, the arrangements are first shifted to the left and then to the right.
Do left rotation on x-y.
In right-left rotation, the arrangements are first shifted to the right and then to the left.
Go to the appropriate leaf node to insert a newNode using the following recursive steps.
Compare newKey with rootKey of the current tree.
If newKey < rootKey, call insertion algorithm on the left subtree of the current node until the leaf node is
reached.
Else if newKey > rootKey, call insertion algorithm on the right subtree of current node until the leaf node
is reached.
If balanceFactor > 1, it means the height of the left subtree is greater than that of the right subtree. So,
do a right rotation or left-right rotation
If balanceFactor < -1, it means the height of the right subtree is greater than that of the left subtree. So,
do right rotation or right-left rotation