Download presentation
Presentation is loading. Please wait.
1
CS2420: Lecture 30 Vladimir Kulyukin Computer Science Department Utah State University
2
Outline AVL Search Trees –Deletion
3
Left Unbalance: L0 AL BLBR A B h hh Before Deletion B B A BLBR AL After Deletion hh h-1 BF=0 BF= -1 BF=0 BF= -2
4
Fixing L0: L0 (Single Left) Rotation B A BLBR AL After Deletion, Before Rotation h h h-1 BF=0 BF=-2 BR AL BL After Deletion, After Rotation h h h-1 B ABF=-1 BF=1 Deletion is from the left child of A and the BF of the right child of A is 0, hence L0.
5
L0 Rotation: Height Preservation L0 is the same as the single left rotation at A and rebalances the entire tree. The height of the tree rooted in A is h+2 before deletion and h+2 after the deletion. No further rebalancing is needed.
6
Left Unbalance: L1 AL CL BR A B h h h-1 Before Deletion B B A BR AL After Deletion h-1 BF=1 BF=-1 BF=1 BF= -2 CR C CLCR C h Deletion is from the left child of A, and A’s right child’s BF is 1, hence L1.
7
Fixing L1: L1 (RL) Rotation B A BR AL After Deletion, Before Rotation h-1 BF=1 BF= -2 CLCR C h BRAL CL C AB h-1 BF=0 BF=X BF=? After Deletion, After Rotation
8
L1 Rotation: Restoring the Balance Factors In L1, the BF’s are restored in the same way they are restored in RL. There are 3 cases: –If C’s BF = 0, then A’s BF = B’s BF = 0. –If C’s BF = 1, then A’s BF = 0, B’s BF = -1. –If C’s BF = -1, then A’s BF = 1, B’s BF = 0.
9
L1 Rotation L1 rotation is similar to the RL rotation (right at B and left at A) as far as node pointer manipulation is concerned. The difference is that L1, unlike RL, does not preserve the height of the tree rooted in A: –The height of the tree rooted in A is h+2 before deletion. –The height of the tree rooted in A is h+1 after deletion and rotation. Further rebalancing is needed up the insertion path.
10
Left Unbalance: L-1 AL BR A B h h Before Deletion B B A AL After Deletion h-1 BF= -1 BF= -2 BL h-1 BR h BL h-1 Deletion is from the left child of A and A’s right child’s BF = -1, hence L-1. Note that the name should be read as L MINUS 1, not L DASH 1.
11
Fixing L-1: L-1 (Single Left) Rotation B A AL After Deletion, Before Rotation h-1 BF= -1 BF= -2 BR h BL h-1 BR BLAL B A h h-1 BF=0 After Deletion, After Rotation
12
L-1 Rotation L-1 is similar to the single left rotation at A. The difference is again in the height preservation: –L-1 rotation does not preserve the height of the tree rooted in A. –The height of the tree rooted in A is h+2 before deletion and h+1 after deletion and rotation. Further rebalancing is needed.
13
Right Unbalance: R0 BF=1 AR BRBL A B h h BF=0 h Before Deletion BF=2 BLBR hh AR h-1 After Deletion A BBF=0
14
Fixing R0: R0 (Single Right) Rotation BF=2 BLBR hh AR h-1 After Deletion, Before Rotation A B BF=-1 AR BR BL h h A B h-1 BF=1 After Deletion, After Rotation BF=0 Deletion is from the right child of A; A’s left child’s BF = 0, hence R0.
15
R0 Rotation R0 is the same as the single right rotation at A and rebalances the tree. The height of the tree rooted in A is h+2 before deletion and h+2 after deletion and rotation. No further rebalancing is needed.
16
Right Unbalance: R1 BF=1 AR BR BL A B hh-1 BF=1 h Before Deletion BF=2 BL BR hh-1 AR h-1 After Deletion A B BF=1 Deletion is from A’s right child and A’s left child’s BF is 1, hence R1.
17
Fixing R1: R1 (Single Right) Rotation BF=2 BL BR h h-1 AR h-1 After Deletion, Before Rotation A B BF=0 ARBR BL h h-1 A B BF=0 After Deletion, After Rotation BF=1
18
R1 Rotation R1 is the same as the single right rotation at A but does not rebalance the tree. The height of the tree rooted in A is h+2 before deletion and h+1 after deletion and rotation. Further rebalancing is needed.
19
Right Unbalance: R-1 A B C AR BL CLCR h-1 BF= -1 BF=1 h h A AR h-1 B C BL h-1 CLCR h Before DeletionAfter Deletion BF=2 BF= -1
20
Fixing R-1 Unbalance: R-1 (LR) Rotation A AR h-1 B C BL h-1 CL CR h A AR CR B BL CL C h-1 BF= -1 BF=2 BF=0 BF=X If C’S BF = 0, then B’s BF = 0 = A’s BF If C’s BF = 1, then B’s BF = 0, A’s BF = -1 If C’s BF = -1, then B’s BF = 1, A’s BF = 0 After Deletion, After Rotation BF=?
21
R-1 Rotation R-1 is the same as the LR rotation (left at B and right at A) but it does not preserve the height of the tree rooted in A. The height of the tree is h+2 before deletion and h+1 after deletion. Further rebalancing is needed.
22
AVL Search Tree: Summary Find, Insert, and Delete are O(logN). Performance of the AVL Search Tree is comparable to that of the other balanced binary search trees.
23
Deletion Rotations: Summary Table UnbalanceRotation L0Single Left L1Right Left (RL) L-1 (L Minus 1)Single Left R0Single Right R1Single Right R-1 (R Minus 1)Left Right (LR)
24
AVL Deletion Rotations: Rebalancing When we delete from an AVL Search Tree, we do not need to go up the insertion path only in two cases L0 and R0. All other unbalances require us to go up the insertion path checking for unbalances, possibly until we reach the root.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.