cs2420: lecture 30 vladimir kulyukin computer science department utah state university
Post on 20-Dec-2015
214 views
TRANSCRIPT
Left Unbalance: L0
AL
BL BR
A
Bh
h h
Before Deletion
BB
A
BL BR
AL
After Deletion
h h
h-1BF=0
BF= -1
BF=0
BF= -2
Fixing L0: L0 (Single Left) Rotation
B
A
BL BR
AL
After Deletion, Before Rotation
h h
h-1BF=0
BF=-2
BR
ALBL
After Deletion, After Rotation
h
hh-1
B
ABF=-1
BF=1
Deletion is from the left child of A and the BF of the rightchild of A is 0, hence L0.
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.
Left Unbalance: L1
AL
CL
BR
A
Bh
h
h-1
Before Deletion
BB
A
BR
AL
After Deletion
h-1
h-1BF=1
BF=-1
BF=1
BF= -2
CR
C
CL CR
C
h
Deletion is from the left child of A, and A’s right child’s BF is 1, hence L1.
Fixing L1: L1 (RL) Rotation
B
A
BR
AL
After Deletion, Before Rotation
h-1
h-1 BF=1
BF= -2
CL CR
C
h CRBRAL
CL
C
A B
h-1 h-1
BF=0
BF=X
BF=? BF=?
After Deletion, After Rotation
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.
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.
Left Unbalance: L-1
AL
BR
A
Bh
h
Before Deletion
BB
A
AL
After Deletion
h-1BF= -1
BF= -1
BF= -1
BF= -2
BLh-1BR
hBL
h-1
Deletion is from the left child of A and A’s right child’s BF = -1, henceL-1. Note that the name should be read as L MINUS 1, not L DASH 1.
Fixing L-1: L-1 (Single Left) Rotation
B
A
AL
After Deletion, Before Rotation
h-1BF= -1
BF= -2
BR
hBL
h-1
BR
BLAL
B
A
h
h-1h-1
BF=0
BF=0
After Deletion, After Rotation
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.
Right Unbalance: R0
BF=1
AR
BRBL
A
B
h h
BF=0
h
Before Deletion
BF=2
BL BR
h h
ARh-1
After Deletion
A
BBF=0
Fixing R0: R0 (Single Right) Rotation
BF=2
BL BR
h h
AR
h-1
After Deletion, Before Rotation
A
B
BF=-1
ARBR
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.
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.
Right Unbalance: R1
BF=1
AR
BRBL
A
B
h h-1
BF=1
h
Before Deletion
BF=2
BL BRh h-1
ARh-1
After Deletion
A
BBF=1
Deletion is from A’s right child and A’s left child’s BF is 1, hence R1.
Fixing R1: R1 (Single Right) Rotation
BF=2
BL BRh h-1
AR
h-1
After Deletion, Before Rotation
A
B
BF=0
ARBR
BL
h
h-1
A
B
h-1
BF=0
After Deletion, After Rotation
BF=1
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.
Right Unbalance: R-1
A
B
C
AR
BL
CL CR
h-1
BF= -1
BF=1
h
h
A
ARh-1
B
C
BLh-1
CL CR
h
Before Deletion After Deletion
BF=2
BF= -1
Fixing R-1 Unbalance: R-1 (LR) Rotation
A
AR
h-1B
C
BL
h-1
CL CR
h
A
ARCR
B
BLCL
C
h-1h-1
BF= -1
BF=2 BF=0
BF=X
If C’S BF = 0, then B’s BF = 0 = A’s BFIf C’s BF = 1, then B’s BF = 0, A’s BF = -1If C’s BF = -1, then B’s BF = 1, A’s BF = 0
After Deletion, After Rotation After Deletion, After Rotation
BF=? BF=?
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.
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.
Deletion Rotations: Summary Table
Unbalance Rotation
L0 Single Left
L1 Right Left (RL)
L-1 (L Minus 1) Single Left
R0 Single Right
R1 Single Right
R-1 (R Minus 1) Left Right (LR)