Professional Documents
Culture Documents
Searching
Finding min/max
Insertion
Deletion
AVL Tree
AVL Tree is a binary search tree that satisfies
For each node, the height of the left and right subtrees can differ
by at most 1
Recall that the height of a node is defined as the length of the
8 13 8 13 8 18
6 15 6 18 15 6 10 15
1 18 1 1
no no yes
(this is not a BST)
AVL Tree - Operations
8 18 8 18
Insert 7
6 10 15 6 10 15
1 1 7
Insertion in AVL Tree
13 13
8 18 8 18
Insert 14
6 10 15 6 10 15
1 7 1 7 14
13 13
8 18
Insert 14 8 18
15 21 15 21
14
Which nodes may violate AVL property?
h
violated if the height of T1 and
T2 differ by 1 originally, and an
insertion is occurred at the taller
subtree to make it even taller
Restore AVL property (Case 1)
T3
h
T3
+
h
1
+
h
2
T1 T2
h
T1 T2
+
h
1
h
Restore AVL property (Case 1)
T3
+
h
1
+
h
2
T2 T3
h
T1 T2
+
h
1
h
Restore AVL property (Case 4)
... ...
x Single Left Rotation
y
y x
h
T1
T3
+
h
1
+
h
2
+
h
T2 T3 1 T1 T2
h
+
h
1
h
Restore AVL property (Case 2)
y x
h
T3 T1
h
+
h
2
+
h
2
T2 T3
+
h
1
h
T1 T2
h
+
h
1
Restore AVL property (Case 2)
...
... x
x
y
h
T3
y z
+
h
2
h
T3 T1
h
+
h
2
+
h
1
T2a T2b
T1 T2
h
+
h
1
Restore AVL property (Case 2)
... ...
x Double Left Rotation
z
y
h
T3 y x
z
+
h
2
T1
h
+
h
1
h
1
+
h
1
h o h
h o h
T1 T2a T2b T3
h
h
T2a T2b
-1 r
-1 r
Restore AVL property (Case 3)
z x y
+
h
2
T3
h
+
h
1
h
1
+
h
1
h o h
h o h
T2a T2b T1 T2a T2b T3
h
h
-1 r
-1 r
Why it is called “Double” rotation?
h
T3
z
+
h
2
T2b
T1
h
T1 T2a
+
h
1
T2a T2b
Single Right Rotation(x)
...
y x
Double Left Rotation(x)
+
+
h
1
h
1
h o h
h o h
T1 T2a T2b T3
h
h
-1 r
-1 r
Insertion in AVL Tree – Time
Complexity
Time complexity
Find the location to insert
O(log n)
Checking and rotation (if needed) for a node
O(1)
Checking and rotation for all ancestors of the
inserted node
O(log n)
Overall, time complexity of insertion is O(log n)
Continue to examine its ancestors?
T1
y
T2
x
h
v
T3
h
...
T4
h o h h
+ r + +
3 2 1
,
h o h
+ r + h
4 3 +
3
h h
+ +
2 2
h
+
1 h
+
1
T1
T1
y
h
T2
x
T2
h
x
T3
v
v
Continue to examine its ancestors?
h
T3
...
...
h
T4
T4
h o h h h o h h
+ r + + + r + +
3 2 1 3 2 1
, ,
Continue to examine its ancestors?
In node v
No violation of AVL property in node v after
rotation AND
Height of node v before and after insertion (with
tree to x.parent
Update the height of nodes involved in rotations
Review: Deletion in BST
r +
h o h
1
at most 1 after deletion
+
h
1
T1 T2
AVL property can only be violated if
the height of T2 is less than that of T1 ...
by 1 originally, and a node is deleted
from T2 to make T2 even shorter by 1 x
Symmetrically, similar case for T1
+
h
2
T1 T2
+
h
1
Restore AVL property (Case 1)
... ...
x x
delete
y y
+
T3 T3
h
1
h
+
+
h
2
h
2
h o h
h o h
T1 T2 T1 T2
+
+
h
1
h
1
+ r
+ r
1
1
Restore AVL property (Case 1)
...
...
x y
Single Right Rotation
y x
+ r +
h o h
1
T3
h
T1
+
h
1
+
h
2
h o h
2
h o h
T2 T3
h
T1 T2
+
h
1
+ r
+ r
1
1
Restore AVL property (Case 4)
y x
+ r +
h o h
1
h
T1
T3
+
h
1
+
h
2
h o h
h o h
2
T1 T2
+
T2 T3
h
h
1
+ r
+ r
1
1
Rotating Nodes
M(1)
S(0)
T(0) V(0)
M(1)
S(-1)
Q(0) U(0)
Q(0)
M(1)
Q(0)
N(0)
M(1)
Q(0)
R(0)
...
...
Single Right Rotation y
x
x
y
T3 h T1
h
+
h
2
+
h
2
T2 T3
+
h
1
h
T1 T2
+
h
h
1
Restore AVL property (Case 2)
...
x
...
x y
h
T3
z
+
h
2
y
T1
h
T3
h
+
h
1
+
h
2
T1 T2 T2a T2b
+
h
h
1
Restore AVL property (Case 2)
...
...
x
Double Left Rotation
z
y
y x
h
T3
z
+
h
2
T1
h
+
h
1
h
1
+
h
1
h o h
h o h
T1 T2a T2b T3
h
h
-1 r
-1 r
T2a T2b
Restore AVL property (Case 3)
z x y
+
h
2
T3
h
+
h
1
h
1
+
h
1
h o h
h o h
T2a T2b T1 T2a T2b T3
h
h
-1 r
-1 r
Summary: Restore AVL property
Time complexity
Find the location to delete
O(log n)
Checking and rotation (if needed) for a node
O(1)
Checking and rotation for all ancestors of the last
deleted node
O(log n)
Overall, time complexity of deletion is O(log n)
Continue to examine its ancestors?
... ...
v
Single Right Rotation(x) v
x y
T4
+
h
4
T4
+
h
4
y x
+
h
2
+
h
3
T1
+
h
1
h
T3
+
h
2
T2 T3
h
T1 T2
+
h
1
h
Continue examine its ancestors?
tree to x.parent
Update the height of nodes involved in rotations