Professional Documents
Culture Documents
AVL TREES
40 40 20 20
2 30 20 40 30
20 30 30 40
30
1
20 40
40 20 20
40
30 20 40 30
20 30 30 40
30
20 40
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
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
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
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-
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
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
17
12
21
10 15
19 27
14 16
81
Assignment
82
TIME COMPLEXITY
Time complexity of both insertion and deletion is O(logn)
in AVL Tree.
83
● Doubts ???
84