You are on page 1of 6

Right-Left Rotation (Inside Case)

The situation of Right-Left Rotation will arise when we insert a node in the
AVL Tree as given below: Insertion of node Y cause the balance factor node Z to

become equal to 2, so double rotation is required to restore


Z

AVL-ness property, since single rotation is not going to restore


AVL-ness property.(as discussed in case of Left-Right rotation)
so right-left rotation must be performed to make it AVL tree.

As Balance factor of Node X is 2 and right sub-tree of node X is left heavy.


So we need to perform Right-Left Rotation. First perform Right rotation at
right child of node X. (i.e. perform Right rotation at node Z) and then Left
Rotation at Node X.
X

X
Right Rotation
at node Z

Left Rotation
at node X

X
Z

Right Left Rotation in detail

X
Z

Y
NULL

Right Rotation
at Node Z

X
Left Rotation
at Node X

Y
NULL

X
Z

Example

Let us perform Right-Left rotation with the help of an example.

30

30
Right Rotation

40

35

35
Left Rotation

35

30
40

35

Insert 45

40

30

40

45

Right Left Rotation

Insert 42

35

35

35
Left Rotation

42
2

30

30

40

30

40
Right Rotation

40

42

45

45
45

Right Left Rotation

42

Insert 41

35

35
30

42
40

Right Rotation

45
41

30

40
40

Left Rotation

42

35

42
30
41

45
Right Left Rotation

41

45

Let us take a more complex scenario of (Double) Right-Left rotation.


BF = 0 -2 = -2

Insertion of node C in the AVL tree leave behind

the tree unbalanced. We can also see from balance


Y

A
X

factor of node B (BF = -2), and right sub-tree of


Z

node B is left heavy, so Double Right-Left rotation


is required to restore its AVL-ness property.

C
Unbalanced Tree

B
A

Right Rotation
at Node Y

Left Rotation
at Node B

Right Left Rotation

OR
B

B
A

Y
C

Right Rotation
at Node Y

C
Left Rotation
at Node B

C
Y

A
X

Z
Right Left Rotation

Double Right-Left Rotation Algorithm


Perform left
rotation around X

P
Perform right
rotation around Z

X
Y

P->right

Right Rotation

Left Rotation

Y
Null

node double_right_left_rotate(node P)
{
node Q;
P->right = single_right_rotate(p->right);

Q = single_left_rotate(P);

return Q;

/* perform right rotation around p->right


and store new root node returned by
function single_right_rotate in p->right. */

/* perform left rotation around node P, and


store new root node returned by function
single_left_rotate in Q, and return or announce
Q as a new root node of the tree. */

Perform right
rotation around Y

Perform right
rotation around B

P->right

Y
X

Left Rotation

X
C

Right Rotation

Y
A

Z
Right Left Rotation

Example:BF = 2

20

30

20
40

10

Insert 35

10

40

30

50

30

20

Right-Left Rotation

40

10

50
35

Insert 22, 29,


and 25.

30

30

30

50

35

BF = 2

20

40

Right-Left Rotation

40

20

Insert 23

40

20
BF = 2

35

25

10
22

10

50

29

22

10

50

35

25

22

35

50

29

29
25

23

Right-Left Rotation

30

25

30
Right-Left Rotation

BF = 2

40

22
20
10

35

25
23

28

50

20
10

22

40

22

Insert 27

35

25
23

28
27

20

50
10

30
28

23
27

40
35

50

You might also like