unit-testing
unit-testing
Any problem?
Insertion may imbalance the tree. Heights of two
children of a node may differ by 2 after an
insertion.
Let U be the node nearest to the inserted one which has an imbalance.
insertion in the left subtree of the left child of U
insertion in the right subtree of the left child of U
insertion in the left subtree of the right child of U
insertion in the right subtree of the right child of U
Insertion in left child of left
subtree
Single Rotation
V
U
U
V
Z X Z
Y
Y
X After Rotation
Before Rotation
5 5 U
3 8 3
8 Z
V
1 4 4
1 Y
AVL Tree X
0.8
3 Insert 0.8
1
5 After Rotation
4 8
0.8
Double Rotation
Suppose, imbalance is due to an insertion in the left subtree of
right child
Single Rotation does not work!
U Before Rotation W
After Rotation
V V U
D
W
A A B C D
B C
5 5 U
D
V 8
3 3
8
1 4 A 1 4 W
Insert 3.5
4
3
5 After Rotation
8
3.5
0.8
Extended Example
Insert 3,2,1,4,5,6,7, 16,15,14
3 2
3
3
2 1
Fig 1 2 3
Fig 4
Fig 2
2 1 2
Fig 3
1
1 3
3
Fig 5 Fig 6 4
4
5
Rings a bell! Fibonacci numbers
FN N
S(h) h
h is O(log N)