Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Search Tree 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 put(9)

Similar presentations


Presentation on theme: "AVL Search Tree 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 put(9)"— Presentation transcript:

1 AVL Search Tree 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 put(9)

2 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

3 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

4 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

5 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

6 AVL Rotations RR LL RL LR

7 LL imbalance - put(1) -1 10 1 7 40 50 3 30 20 35 45 60 20

8 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

9 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

10 LR imbalance - put(8) 1 10 3 40 2 4

11 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

12 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

13 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

14 LR imbalance - put(8) 4 1 10 3 8 2 40 AFTER SECOND ROTATION

15 RL imbalance - put(5) -1 2 8 1 9 7

16 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

17 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

18 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

19 RL imbalance - put(5) 7 -1 8 2 9 1 5 AFTER SECOND ROTATION

20 delete(45) 1 2 10 -1 6 40 45 3 8 Some balance factors on insert path change. 1 5 7 9

21 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

22 delete(45) 1 2 10 1 -1 6 40 45 3 8 Some balance factors on insert path change. 1 5

23 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

24 delete(45) 1 2 10 -1 -1 6 40 45 3 8 Some balance factors on insert path change. 7 9

25 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


Download ppt "AVL Search Tree 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 put(9)"

Similar presentations


Ads by Google