AVL search Tree L-Deletions

12
L CATEGORY DELETIONS IN AVL SEARCH TREES

Transcript of AVL search Tree L-Deletions

Page 1: AVL search Tree L-Deletions

L CATEGORY DELETIONS IN AVL SEARCH TREES

Page 2: AVL search Tree L-Deletions

AVL TREE DELETION

• Similar to insertion but more complex

– Rotations and double rotations needed to rebalance

– Imbalance may propagate upward so that many rotations may be needed.

Page 3: AVL search Tree L-Deletions

TYPES OF DELETION

There are two types of deletion, Rotation free deletions Deletion with rotations

L CATEGORY R CATEGORY L0 R0

L1 R1 L-1 R-1

Page 4: AVL search Tree L-Deletions

L CATEGORY ROTATIONS

Let A be the node whose balance factor is affected

• For the L classification, If bf(A) = -2 (where as in R classification bf(A) = +2) then

it should have been -1 before deletion and A should have a right subtree with root B.

Based on bf(B) being either 0 or +1 or -1, classify

the L rotations as L0, L1, L-1.

Page 5: AVL search Tree L-Deletions

GENERIC REPRESENTATION OF L0 ROTATION

h h

AL

BL BR

A

B

A

B

h-1

h

AL

BL BR

(-1)

(0)

(-2)

(0)

B

A

h

hh-1

BR

BL

AL

(+1)

(-1)

Balanced tree before deletion

Unbalanced tree after deletion

Balanced tree after deletion

Page 6: AVL search Tree L-Deletions

EXAMPLE

30

25 35

20 40

45

15

10

DELETE 20(-1)

(0)

(-1)

(0)

(1)

(0)(1)

(0)

(-2)

(0)

(1)

(-1)

(0)

(

(-1)

(0)

BEFORE DELETION

UNBALANCE AFTER DELETION

R0 ROTATION

BALANCED TREE

Page 7: AVL search Tree L-Deletions

Balanced tree after deletionBalanced tree before

deletionUnbalanced tree after

deletion

GENERIC REPRESENTATION OF L1 ROTATION

A

B

Ch- 1

h -1

h-1

(-1)

(+1)

(0)

CL CR

BR

AL

A

B

C

h-1

h-1

h-1

CL CR

(-2)

(+1)

(0)

C

A B

(0)

(0)(0)

h-1h-1

CL CR

AL

BR

AL BR

Page 8: AVL search Tree L-Deletions

EXAMPLE

50

45 55

53 56

51 60

49

(-1)

(1)

(0)

(-1)

(0)

(0) (0)

(0)

(-2)

(0)

(0)

(0)

(0)

(0)

BEFORE DELETIONDELETE 49

UNBALANCED TREE AFTER DELETION

L1 ROTATION

BALANCED TREE

Page 9: AVL search Tree L-Deletions

GENERIC REPRESENTATION OF L-1 ROTATION

Balanced tree after deletion

Balanced tree before deletion

h h

AL

BL BR

A

B

A

B

h-1

h

AL

Unbalanced tree after deletion

(-1)

(-1)

(-2)

(-1)

B

A

h

h- 1h-1

BRBLAL

(0)

(0)

h-1 h- 1

BL BR

Page 10: AVL search Tree L-Deletions

EXAMPLE

75

60 80

72 76 90

85

(-1)

(-1)

(+1)

(-1)

(0) (0)

(0)

(-2)

(0)

(0)

(+1)

(0)(0)(0)

BEFORE DELETION

UNBALANCE AFTER DELETION

L-1 ROTATION

BALANCED TREE

DELETE 72

Page 11: AVL search Tree L-Deletions

ARGUMENTS FOR AVL TREES

• Search is O(log N) since AVL trees are always

balanced.• Insertion and deletions are also O(logn)• The height balancing adds no more than a

constant factor to the speed of insertion.

Page 12: AVL search Tree L-Deletions

ARGUMENTS AGAINST USING AVL TREES

• Difficult to program & debug; more space for balance factor.

• Asymptotically faster but rebalancing costs time.• Most large searches are done in database systems on

disk and use other structures (e.g. B-trees).• May be OK to have O(N) for a single operation if total

run time for many consecutive operations is fast (e.g. Splay trees).