AVL Search Tree 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 put(9)
put(9) No imbalance -1 -1 10 1 1 7 40 -1 1 45 3 8 30 -1 60 35 1 9 20 5 1 1 7 40 -1 -1 1 45 3 8 30 Some balance factors on insert path change. -1 60 35 1 9 20 5 25 No imbalance
RR imbalance - put(29) 10 -1 1 1 7 40 -1 1 45 3 8 30 -1 60 35 20 1 20 1 45 3 8 30 -1 60 35 20 1 20 5 25
RR imbalance - put(29) RR imbalance: new node is in right subtree 10 1 -1 7 1 40 3 8 -1 1 45 30 1 5 -1 -2 60 35 20 20 RR imbalance: new node is in right subtree of right subtree of blue node (node with bf = -2) -1 25 29 FIX: Single rotation
RR imbalance - put(29) AFTER ROTATION 10 1 -1 7 1 40 3 8 -1 1 45 30 1 3 8 -1 1 45 30 1 5 60 35 25 20 29 AFTER ROTATION
AVL Rotations RR LL RL LR
LL imbalance - put(1) -1 10 1 7 40 50 3 30 20 35 45 60 20
LL imbalance - put(1) -1 10 2 1 7 40 1 50 3 30 1 20 35 45 60 20 LL imbalance => new node is in left subtree of left subtree of blue node (node with bf = 2) FIX: Single right rotation
LL imbalance - put(1) AFTER rotation -1 10 3 40 50 1 7 30 20 35 45 60 3 40 50 1 7 30 20 35 45 60 20 AFTER rotation
LR imbalance - put(8) 1 10 3 40 2 4
LR imbalance - put(8) 2 1 10 -1 3 40 -1 2 4 8 LR imbalance => new node is in right subtree of left subtree of blue node (node with bf = 2) FIX: Double rotation
LR imbalance - put(8) 2 1 -1 10 3 40 LL ROTATION 2 4 RR ROTATION 8 LR rotation is an RR rotation followed by LL rotation
LR imbalance - put(8) AFTER FIRST ROTATION LL ROTATION 2 1 -1 10 4 40 4 40 LL ROTATION 3 8 2 AFTER FIRST ROTATION
LR imbalance - put(8) 4 1 10 3 8 2 40 AFTER SECOND ROTATION
RL imbalance - put(5) -1 2 8 1 9 7
RL imbalance - put(5) -2 -1 2 1 8 1 1 9 7 5 RL imbalance => new node is in left subtree of right subtree of blue node (node with bf = -2) FIX: Double rotation
RL imbalance - put(5) RR ROTATION LL ROTATION -2 -1 2 1 8 1 RR ROTATION 1 9 7 5 LL ROTATION RL rotation is an LL rotation followed by an RR rotation
RL imbalance - put(5) AFTER FIRST ROTATION RR ROTATION -2 -1 2 -1 7 1 7 1 -1 RR ROTATION 8 5 9 AFTER FIRST ROTATION
RL imbalance - put(5) 7 -1 8 2 9 1 5 AFTER SECOND ROTATION
delete(45) 1 2 10 -1 6 40 45 3 8 Some balance factors on insert path change. 1 5 7 9
delete(45) After R0 rotation -1 6 1 3 10 1 5 40 8 7 9 1 3 10 1 5 40 8 Some balance factors on insert path change. 7 9 After R0 rotation
delete(45) 1 2 10 1 -1 6 40 45 3 8 Some balance factors on insert path change. 1 5
delete(45) After R1 rotation 6 3 10 1 5 40 8 6 3 10 1 5 40 8 Some balance factors on insert path change. After R1 rotation
delete(45) 1 2 10 -1 -1 6 40 45 3 8 Some balance factors on insert path change. 7 9
delete(45) After R-1 rotation 8 6 10 3 7 9 40 8 6 10 3 7 9 40 Some balance factors on insert path change. After R-1 rotation