Вы находитесь на странице: 1из 4

Right Rotation (Single Rotation)

The case of single right rotation occur when Insertion of node takes place
into left sub-tree of left child of any node (lets say node z).
So After the Insert, move toward the root, node by node, updating heights,
because only nodes on the path from insertion point toward the root node
have possibly changed in height.
Let the node that needs rebalancing be node Z.
BF = 1 - (-1) = 2

Y
Right Rotation

Example

Insert number 8 7 6 5 4 into an empty AVL Tree.


BF = 1- (-1) = 2

8
Insert 8

Insert 7

Insert 6

Right Rotation

7
6

Insert 5

BF = 2

5
4

Right Rotation

6
4

Insert 4

6
5

Let us take a more complex scenario of single Right Rotation.


BF = 2 - 0 = 2

Y
C

Insertion of node A in the AVL tree leave


Z

behind the tree unbalanced. We can also


see from balance factor of node Y (BF = 2),

so single right rotation around node y is


required to restore its AVL-ness property.

A
Unbalanced Tree

Y
C
C

Right Rotation

B
X

A
A

Example:-

3
BF = 3 -1 = 2

Right Rotation

6
5
4

5
4

8
7

Single Right Rotation Algorithm

Z
Q

Y
Null

Right Rotation

Null

node Single_Right_rotate(node P) //pass node with BF = 2 to this function


{
node Q = P->left;
P->left=Q->right;
Q->right= P;
P = update height;
// update height of node pointed by P
Q=update height;
// update height of node pointed by Q
return Q;
// return new root node
}

Y
Q

C
Z

Right Rotation

B
B
A

X
A

Вам также может понравиться