Download presentation
Presentation is loading. Please wait.
Published byNoelle Farman Modified over 9 years ago
1
AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719
2
AVL Trees Not AVL tree: 27 20 30 187 1625 5 1719
3
AVL Tree Rotations Single rotations: insert 14, 15, 16, 13, 12, 11, 10 14 15 First insert 14 and 15: Now insert 16.
4
AVL Tree Rotations Single rotations: 14 15 Inserting 16 causes AVL violation: Need to rotate. 16
5
AVL Tree Rotations Single rotations: 14 15 Inserting 16 causes AVL violation: Need to rotate. 16
6
AVL Tree Rotations Single rotations: 14 15 Rotation type: 16
7
AVL Tree Rotations Single rotations: 14 15 Rotation restores AVL balance: 16
8
AVL Tree Rotations Single rotations: 14 15 16 Now insert 13 and 12: 13 12 AVL violation - need to rotate.
9
AVL Tree Rotations Single rotations: Rotation type: 14 15 16 13 12
10
AVL Tree Rotations Single rotations: 13 15 16 Now insert 11. 12 14
11
AVL Tree Rotations Single rotations: 13 15 16 AVL violation – need to rotate 12 14 11
12
AVL Tree Rotations Single rotations: Rotation type: 13 15 16 12 14 11
13
AVL Tree Rotations Single rotations: 13 15 16 12 14 11 Now insert 10.
14
AVL Tree Rotations Single rotations: 13 15 16 12 14 11 10 AVL violation – need to rotate
15
AVL Tree Rotations Single rotations: 13 15 16 12 14 11 10 Rotation type:
16
AVL Tree Rotations Single rotations: 13 15 16 11 14 10 12 AVL balance restored.
17
AVL Tree Rotations Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8 First insert 1 and 2: 13 15 16 11 14 10 12
18
AVL Tree Rotations Double rotations: 13 15 16 11 14 10 AVL violation - rotate 1 2 12
19
AVL Tree Rotations Double rotations: 13 15 16 11 14 10 Rotation type: 1 2 12
20
AVL Tree Rotations Double rotations: Now insert 3. AVL balance restored: 1 10 13 15 16 11 14 2 12
21
AVL Tree Rotations Double rotations: AVL violation – rotate: 1 10 13 15 16 11 14 2 12 3
22
AVL Tree Rotations Double rotations: Rotation type: 1 10 13 15 16 11 14 2 12 3
23
AVL Tree Rotations Double rotations: AVL balance restored: 1 3 13 15 16 10 14 2 11 12 Now insert 4.
24
AVL Tree Rotations Double rotations: AVL violation - rotate 1 3 13 15 16 10 14 2 11 12 4
25
AVL Tree Rotations Double rotations: Rotation type: 1 3 13 15 16 10 14 2 11 12 4
26
AVL Tree Rotations Double rotations: 10 13 15 2 11 1 3 4 121416 Now insert 5.
27
AVL Tree Rotations Double rotations: 10 13 15 2 11 1 3 4 121416 AVL violation – rotate. 5
28
AVL Tree Rotations Single rotations: 10 13 15 2 11 1 3 4 121416 5 Rotation type:
29
AVL Tree Rotations Single rotations: 10 13 15 2 11 1 4 5 121416 3 AVL balance restored: Now insert 7.
30
AVL Tree Rotations Single rotations: 10 13 15 2 11 1 4 5 121416 3 AVL violation – rotate. 7
31
AVL Tree Rotations Single rotations: 10 13 15 2 11 1 4 5 121416 3 7 Rotation type:
32
AVL Tree Rotations Double rotations: 10 13 15 4 11 2 5 7 121416 31 AVL balance restored. Now insert 6.
33
AVL Tree Rotations Double rotations: 10 13 15 4 11 2 5 7 121416 31 AVL violation - rotate. 6
34
AVL Tree Rotations Double rotations: 10 13 15 4 11 2 5 7 121416 31 Rotation type: 6
35
AVL Tree Rotations Double rotations: 10 13 15 4 11 2 6 7 121416 31 AVL balance restored. Now insert 9 and 8. 5
36
AVL Tree Rotations Double rotations: 10 13 15 4 11 2 6 7 121416 31 AVL violation - rotate. 9 8 5
37
AVL Tree Rotations Double rotations: 10 13 15 4 11 2 6 7 121416 31 Rotation type: 9 8 5
38
AVL Tree Rotations Final tree: 10 13 15 4 11 2 6 8 121416 31 Tree is almost perfectly balanced 5 97
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.