Professional Documents
Culture Documents
AVL Trees - Horowitz Sahni CPP - Lec43
AVL Trees - Horowitz Sahni CPP - Lec43
AVL Trees - Horowitz Sahni CPP - Lec43
Primary Operations:
Get(key) => search
Insert(key, element) => insert
Delete(key) => delete
Additional operations:
Ascend()
Get(index)
Delete(index)
Expected
Hash Table
O(1)
O(n)
O(log n)
O(log n)
Hash Table
Indexed BST
O(n)
Indexed
O(n)
Balanced BST
D is number of buckets
O(n)
O(log n)
AVL Tree
binary tree
for every node x, define its balance factor
balance factor of x = height of left subtree of x
height of right subtree of x
Balance Factors
-1
1
0
-1
0
-1
0
0
Nh , h > 1
L
Fibonacci Numbers
F0 = 0, F1 = 1.
Fi = Fi-1 + Fi-2 , i > 1.
N0 = 0, N1 = 1.
Nh = Nh-1 + Nh-2 + 1, i > 1.
Nh = Fh+2 1.
Fi ~ i/sqrt(5).
sqrt
1
7
40
0
3
0
8
0
5
45
30
-1
20
35
25
-1
0
60
Insert(9)
-1
10
1
7
40
0
3
0
8
0
5
-1
45
30
-1
20
35
25
-1
0
60
Insert(29)
-1
10
1
1
7
40
0
3
0
8
0
5
45
30
-2 -1
20
35
25
60
-1
0
29
-1
Insert(29)
-1
10
1
1
7
40
0
3
0
0
0
35
25
5
0
20
RR rotation.
45
30
29
-1
0
60
Insert
Following insert, retrace path towards root and
adjust balance factors as needed.
Stop when you reach a node whose balance
factor becomes 0, 2, or 2, or when you reach
the root.
The new tree is not an AVL tree only if you
reach a node whose balance factor is either 2 or
2.
In this case, we say the tree has become
unbalanced.
A-Node
Let A be the nearest ancestor of the newly
inserted node whose balance factor
becomes +2 or 2 following the insert.
Balance factor of nodes between new node
and A is 0 before insertion.
Imbalance Types
RR newly inserted node is in the right
subtree of the right subtree of A.
LL left subtree of left subtree of A.
RL left subtree of right subtree of A.
LR right subtree of left subtree of A.
LL Rotation
A 1
B 0
A 2
AR
h
BL
BR
h
h
Before insertion.
B 1
B 0
BL
h+1
AR
h
BL BR
h+1
h
After insertion.
BR
AR
h
h
After rotation.
LR Rotation (case 1)
A 1
B 0
A 2
B -1
C 0
B 0
C 0
Before insertion.
After insertion.
After rotation.
LR Rotation (case 2)
A 2
A 1
B 0
BL
h
CL
h-1
AR
h
B -1
BL
h
CR
h-1
AR
h
1
CL
h
C 0
C
CR
h-1
BL
h
-1
CL CR
h
h-1
A
AR
h
LR Rotation (case 3)
A 2
A 1
B 0
BL
h
CL
h-1
AR
h
B -1
BL
h
CR
h-1
AR
h
-1
CL
h-1
C 0
C
CR
h
BL
h
B
CL CR
h-1 h
A
AR
h
Double
LR and RL
LR is RR followed by LL
RL is LL followed by RR
LR Is RR + LL
A 2
A 2
B -1
BL
h
-1
CL
h-1
AR
h
C
CR
h
After insertion.
BL
h
C 0
AR
h
B CR
h
CL
h-1
After RR rotation.
BL
h
B
CL CR
h-1 h
A
AR
h
After LL rotation.
Delete An Element
-1
10
1
1
7
40
0
3
0
8
0
5
0
-1
20
35
25
Delete 8.
45
30
-1
0
60
Delete An Element
-1
10
2
40
1
3
45
30
0
5
-1
20
35
0
-1
0
60
Imbalance Classification
Let A be the nearest ancestor of the deleted
node whose balance factor has become 2 or 2
following a deletion.
Deletion from left subtree of A => type L.
Deletion from right subtree of A => type R.
Type R => new bf(A) = 2.
So, old bf(A) = 1.
So, A has a left child B.
bf(B) = 0 => R0.
bf(B) = 1 => R1.
bf(B) = 1 => R-1.
R0 Rotation
A 1
B 0
A 2
AR
h
BL
BR
h
h
Before deletion.
Subtree height is unchanged.
No further adjustments to be done.
Similar to LL rotation.
B 0
B -1
AR
h-1
BL
BR
h
h
After deletion.
BL
h
BR
AR
h
h-1
After rotation.
R1 Rotation
A 1
B 1
A 2
AR
h
BL
BR
h
h-1
Before deletion.
B 1
B 0
AR
h-1
BL
BR
h
h-1
After deletion.
BL
h
BR
AR
h-1
h-1
After rotation.
R-1 Rotation
A 2
A 1
B -1
BL
h-1
CL
AR
h
B -1
BL
h-1
CR
CL
C 0
AR
h-1
b C
CR
B
BL
h-1
CL
A
CR
AR
h-1
Rotation Frequency
Insert random numbers.
No rotation 53.4% (approx).
LL/RR 23.3% (approx).
LR/RL 23.2% (approx).