20. Deletion in AVL Tree.pptx

shafijavid 48 views 13 slides Nov 14, 2022
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

dsa ka ppt


Slide Content

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 lef t 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

20 10 30 25 1 40 5 2 35 1 45 -1 -1 Delete 5 Ravi Kant Sahu , Asst. Professor @ LPU Phagwara (Punjab) India

20 10 30 25 1 40 2 35 45 -2 -1 A B Ravi Kant Sahu , Asst. Professor @ LPU Phagwara (Punjab) India

R Rotation R Rotation is applied whe n the deleted node is in the right subtree of node A. There are three diff e rent 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

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

L Rotation L Rotation is applied whe n the deleted node is in the left subtree of node A. There are three diff e rent types of rotations based on the balanced factor of node B. L 0 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

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

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? 1 LL and 1 RR 2 LL and 1 RR 1 LL and 2 RR 2 LL and 2 RR Ravi Kant Sahu , Asst. Professor @ LPU Phagwara (Punjab) India

Questions