0% found this document useful (0 votes)
140 views13 pages

Deletion in AVL Tree

This document discusses deletion in AVL trees. It explains that deleting a node can cause balance factors to change and rotations may be needed to rebalance the tree. There are R rotations applied if the deleted node is in the right subtree of an unbalanced node A, and L rotations if in the left subtree. The types of rotations - R0, R1, R-1 and L0, L1, L-1 - depend on the balance factor of A's descendant B. Fixing node A may not fully rebalance the tree, and ancestors of A may also require fixing. An example deletion is shown.

Uploaded by

nighat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views13 pages

Deletion in AVL Tree

This document discusses deletion in AVL trees. It explains that deleting a node can cause balance factors to change and rotations may be needed to rebalance the tree. There are R rotations applied if the deleted node is in the right subtree of an unbalanced node A, and L rotations if in the left subtree. The types of rotations - R0, R1, R-1 and L0, L1, L-1 - depend on the balance factor of A's descendant B. Fixing node A may not fully rebalance the tree, and ancestors of A may also require fixing. An example deletion is shown.

Uploaded by

nighat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Data Structures

Topic: Deletion in AVL Tree

By
Ravi Kant Sahu
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• R0, R1 and R-1 Rotation
• L0, L1 and L-1 Rotation

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Introduction
• An element can be deleted from AVL tree which may
change the BF of a node such that it results in unbalanced
tree.

• Some rotations will be applied on AVL tree to balance it.

• R rotation is applied if the deleted node is in the right


subtree of node A (A is the node with balance factor other
than 0, 1 and -1).

• L rotation is applied if the deleted node is in the left


subtree of node A.
Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India
Introduction
• Suppose we have deleted node X from the tree.

• A is the closest ancestor node on the path from X to the


root node, with a balance factor -2 or +2.

• B is the desendent of node A on the opposite subtree of


deleted node i.e. if the deleted node is on left side the B is
the desendent on the right subtree of A or the root of right
subtree of A.

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Delete 5

1
20 2
-1

10 0 30
1 -1

5 25 40
0 0 0

35 45
0 0

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


1
20 A 2
-2

10 0 30 B
0 -1

25 40
0 0

35 45
0 0

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


R Rotation
• R Rotation is applied when the deleted node is in the right subtree
of node A.
• There are three different types of rotations based on the balanced
factor of node B.

• R0 Rotation: When the balance Factor of node B is 0.


• Apply LL Rotation on node A.
• R1 Rotation: When the balance Factor of node B is +1.
• Apply LL Rotation on node A.
• R-1 Rotation: When the balance Factor of node B is -1.
• Apply LR Rotation(RR rotation on B and LL rotation on node A).

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Work Space

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


L Rotation
• L Rotation is applied when the deleted node is in the left subtree of
node A.
• There are three different types of rotations based on the balanced
factor of node B.

• L0 Rotation: When the balance Factor of node B is 0.


• Apply RR Rotation on node A.
• L-1 Rotation: When the balance Factor of node B is +1.
• Apply RR Rotation on node A.
• L1 Rotation: When the balance Factor of node B is -1.
• Apply RL Rotation(LL rotation on B and RR rotation on node A).

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Work Space

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Important

• Unlike insertion, fixing the node A won’t


fix the complete AVL tree.

• After fixing A, we may have to fix


ancestors of A as well.

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Important
Given the sequential representation of an AVL Tree:
K, G, P, C, H, L, S, A, F, _, I,_, M, _, _, _, _, D
[Where ‘_’ represents NULL tree]
How many LL and RR rotations will be required if we
Delete S?
A. 1 LL and 1 RR
B. 2 LL and 1 RR
C. 1 LL and 2 RR
D. 2 LL and 2 RR

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Questions

You might also like