You are on page 1of 84

Lecture 31-32

AVL TREES

Dr. Poonam Rani


Assistant professor
Computer Science and Engineering
NSUT
7/1/21 AVL Tree 1
Contents
Drawback of BST

How BST can be improved

What is AVL tree

Rotations in AVL tree

How to create AVL tree


7/1/21 AVL Tree 2
Drawback of BST
•  Left skewed
•  Right skewed

7/1/21 AVL Tree 3


How to improve this ?
Constructs BST with keys – 40, 30, 20

7/1/21 AVL Tree 4


Solution – Do rotations
40, 30, 20 40, 20, 30 20, 40, 30 20, 30, 40

40 40 20 20

2 30 20 40 30

20 30 30 40

30, 20, 40 or 30, 40, 20

30
1

20 40

7/1/21 AVL Tree 5


Solution – Do Rotations
40, 30, 20 40, 20, 30 20, 40, 30 20, 30, 40

40 20 20
40

30 20 40 30

20 30 30 40

30, 20, 40 or 30, 40, 20

30

20 40

7/1/21 AVL Tree 6


Introduction
❖  AVL Tree is invented by GM Adelson - Velsky and EM Landis in
1962.
❖  Height balanced binary search tree in which each node is
associated with a balance factor
❖  Tree is said to be balanced if balance factor of each node is in
between -1 to 1, otherwise, the tree will be unbalanced and need
to be balanced

7
BALANCING FACTOR (k)
Balance Factor (k) =
height (left(k)) - height (right(k))
OR
Balance Factor (k) =
height (right(k)) - height (left(k))

8
AVL tree

1.  Sub-trees of each


node can differ by
at most 1 in their
height
2.  Every sub-trees is
an AVL tree
AVL TREE ?
.

10
EXAMPLE

11
AVL tree?
AVL Trees?
10 10

5 20 5 20

3 3 43
1
2
1 3
AVL Trees?
12

8 16

4 10 14

2 6
AVL Tree
AVL TREE ROTATIONS

①  Rotation (LL Rotation)


②  Two Right Rotation (RR Rotation)
③  Left Right Rotation (LR Rotation)
④  Right Left Rotation (RL Rotation)

16
LL PROBLEM

17
SOLUTION OF L-L PROBLEM

18
RR PROBLEM
An example of R-R problem is :

19
SOLUTION OF R-R PROBLEM

20
L-R PROBLEM

21
L-R PROBLEM -soln

L
L

L
L

R L

R L

22
LR -ROTATION
P
R
L

Q PR
R Q P

QL R
QL PR
RL RR
RL RR
R-L PROBLEM

24
SOLUTION OF R-L PROBLEM

R R

R
L

RL - RR
25
OPERATIONS ON AVL TREES
1.  Insertion
2.  Deletion

26
①  INSERTION
Steps to follow for insertion

Let the newly inserted node be w

❖  Perform standard BST insert for w.


❖  Starting from w, travel up and find the first unbalanced node. Let z be the first
unbalanced node, y be the child of z that comes on the path from w to z and x be
the grandchild of z that comes on the path from w to z.
❖  Re-balance the tree by performing appropriate rotations on the subtree rooted
with z. There can be 4 possible cases that needs to be handled as x, y and z can be
arranged in 4 ways. Following are the possible 4 arrangements:
➔  y is left child of z and x is left child of y (Left Left Case)
➔  y is left child of z and x is right child of y (Left Right Case)
➔  y is right child of z and x is right child of y (Right Right Case)
➔  y is right child of z and x is left child of y (Right Left Case)

27
INSERTION EXAMPLE
Insert 3

28
.

29
Insertion

Insert 6
Insertion

Insert 6

Imbalance at 8
Perform rotation with 7
Construct AVL tree by inserting the following-

14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

32
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

2-0=2
11 17
L

1-0 = 0 53
7
L

4 0

33
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

7 17

4 11 53

34
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

7 17

4 53
11

13

12

35
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

7 17

-2
4 53
11
R
1
13
L

0
12

36
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

7 17

4 11 53

12

13

37
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

7 17

4 11 53

12
R

13

38
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

7 17

4 12 53

13
11

39
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
3-2=1
14

0-1= -1
1-2= -1
7 17
0-1= -1
0-0= 0 1-1= 0
4 12 53

0-0= 0
0-0= 0
13
11

40
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

7 17

4 12 53

13
11

41
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

1-3= -2
7 17

0-0= 0 2-1= 1
4 12 53

0-0= 0
1-0= 1
13
11

0-0= 0
8

42
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

1-3= -2
7 17
R
0-0= 0 2-1= 1
4 12 53
L
0-0= 0
1-0= 1
13
11

0-0= 0
8

43
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 17

7 12 53

4 8 13

44
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
1
14
-1
0
11 17

7 0 12 -1 53

0 0
0
4 8 13

45
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
1
14
-1
0
11 17

7 0 12 -1 53

0 0
0
4 8 13

46
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 17

7 12 53

4 8 13 60

47
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14
-2

11 17

-1

7 12 53

0
4 8 13 60

48
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14
-2

11 17
R
-1

7 12 53
R
0
4 8 13 60

49
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 17
R

53
7 12
R
0
4 8 13 60

50
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
14

11 53

7 12 17 60

4 8 13

51
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 53

7 12 17 60

4 8 13 19

52
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 53

7 12 17 60

4 8 13 16 19

53
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 53

7 12 17 60

4 8 13 16 19

20

54
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 53
L
-1
7 12 17 60
R
0
4 -1
8 13 16 19

0
20

55
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 53
L
-1
7 12 17 60
R
0
4 -1
8 13 16 19

0
20

56
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 53
L
-1
7 12 17 60
R
0
4 -1
8 13 16 19

0
20

57
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

11 19

7 12 17 53

20 60
4 8 13 16

58
Assignments
-Construct AVL tree by inserting the following

Q1. 40, 20,10,25,30,22, 50


Q2. 15, 16, 12, 5, 50, 2, 17, 13, 9, 80, 67, 14, 33
Q3. 43, 69, 36, 5, 72, 26, 79, 59
Q4. 21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7
Q5. 1, 2, 3, 4, 5, 6, 7, 8,9, 10, 11,12,13,14,15
Q6. 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55
Deletion X in AVL Trees
●  Deletion:
■  Case 1: if X is a leaf, delete X
■  Case 2: if X has 1 child, use it to replace X
■  Case 3: if X has 2 children, replace X with its
inorder, predecessor (and recursively delete
it)
●  Rebalancing
DELETION EXAMPLE
Delete 30 from this tree:
(case 1)

61
.

62
Deletion (case 1) Delete 4
Deletion

Delete 4
Deletion

LL
Imbalance at 3
Perform rotation with 2

Imbalance at 5
Perform rotation with 8
Deletion

RR

Imbalance at 5
Perform rotation with 8
Deletion

Delete 4

Imbalance at 3
Perform rotation with 2

Imbalance at 5
Perform rotation with 8
Delete 55 (case 1)
60

20 70

10 40 65 85

5 15 30 50 80 90

55
Delete 55 (case 1)
60

20 70

10 40 65 85

5 15 30 50 80 90

55
Delete 50 (case 2)
60

20 70

10 40 65 85

5 15 30 50 80 90

55
Delete 50 (case 2)
60

20 70

10 40 65 85

5 15 30 50 80 90

55
Delete 60 (case 3)
60

20 70

10 40 65 85

5 15 30 50 prev 80 90

55
Delete 60 (case 3)
55

20 70

10 40 65 85

5 15 30 50 80 90
Delete 55 (case 3)
55

20 prev 70

10 40 65 85

5 15 30 50 80 90
Delete 55 (case 3)
50

20 70

10 40 65 85

5 15 30 80 90
Delete 50 (case 3)
50

20 prev 70

10 40 65 85

5 15 30 80 90
Delete 50 (case 3)
40

20 70

10 30 65 85

5 15 80 90
Delete 40 (case 3)
40

20 prev 70

10 30 65 85

5 15 80 90
Delete 40 : Rebalancing
30

20 70

10 Case ? 65 85

5 15 80 90

LL
Delete 40: after rebalancing
30

10 70

5 20 65 85

15 80 90

Single rotation is preferred!


Q1. Delete 10,15,17,27,19,12

17

12
21

10 15
19 27

14 16

81
Assignment

Q1. Create AVL from following


11 70 60 4 16 88 19 20 77 100 205 200
Q2. Delete 19 88 20 11 205 from above AVL

82
TIME COMPLEXITY
Time complexity of both insertion and deletion is O(logn)
in AVL Tree.

83
●  Doubts ???

84

You might also like