avl trees (adelson – velskii – landis) avl tree: 20 30 187 1625 5 1719
TRANSCRIPT
![Page 1: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/1.jpg)
AVL Trees(Adelson – Velskii – Landis)
AVL tree:20
30187
16 25
5 17 19
![Page 2: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/2.jpg)
AVL Trees
Not AVL tree:
27
20
30187
16 25
5 17 19
![Page 3: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/3.jpg)
AVL Tree Rotations
Single rotations: insert 14, 15, 16, 13, 12, 11, 10
14
15
• First insert 14 and 15:
• Now insert 16.
![Page 4: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/4.jpg)
AVL Tree Rotations
Single rotations:
14
15
• Inserting 16 causes AVL violation:
• Need to rotate.
16
![Page 5: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/5.jpg)
AVL Tree Rotations
Single rotations:
14
15
• Inserting 16 causes AVL violation:
• Need to rotate.
16
![Page 6: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/6.jpg)
AVL Tree Rotations
Single rotations:
14
15
• Rotation type:
16
![Page 7: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/7.jpg)
AVL Tree Rotations
Single rotations:
14
15
• Rotation restores AVL balance:
16
![Page 8: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/8.jpg)
AVL Tree Rotations
Single rotations:
14
15
16
• Now insert 13 and 12:
13
12
• AVL violation - need to rotate.
![Page 9: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/9.jpg)
AVL Tree Rotations
Single rotations:
• Rotation type:
14
15
16
13
12
![Page 10: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/10.jpg)
AVL Tree Rotations
Single rotations:
13
15
16
• Now insert 11.
12 14
![Page 11: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/11.jpg)
AVL Tree Rotations
Single rotations:
13
15
16
• AVL violation – need to rotate
12 14
11
![Page 12: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/12.jpg)
AVL Tree Rotations
Single rotations:
• Rotation type:
13
15
16
12 14
11
![Page 13: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/13.jpg)
AVL Tree Rotations
Single rotations:
13
15
16
12
1411
• Now insert 10.
![Page 14: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/14.jpg)
AVL Tree Rotations
Single rotations:
13
15
16
12
1411
10
• AVL violation – need to rotate
![Page 15: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/15.jpg)
AVL Tree Rotations
Single rotations:
13
15
16
12
1411
10
• Rotation type:
![Page 16: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/16.jpg)
AVL Tree Rotations
Single rotations:
13
15
16
11
1410 12
• AVL balance restored.
![Page 17: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/17.jpg)
AVL Tree Rotations
Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8
• First insert 1 and 2:13
15
16
11
1410 12
![Page 18: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/18.jpg)
AVL Tree RotationsDouble rotations:
13
15
16
11
1410
• AVL violation - rotate
1
2
12
![Page 19: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/19.jpg)
AVL Tree RotationsDouble rotations:
13
15
16
11
1410
• Rotation type:
1
2
12
![Page 20: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/20.jpg)
AVL Tree Rotations
Double rotations:
• Now insert 3.
• AVL balance restored:
1 10
13
15
16
11
142 12
![Page 21: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/21.jpg)
AVL Tree Rotations
Double rotations:
• AVL violation – rotate:
1 10
13
15
16
11
142 12
3
![Page 22: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/22.jpg)
AVL Tree Rotations
Double rotations:
• Rotation type:
1 10
13
15
16
11
142 12
3
![Page 23: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/23.jpg)
AVL Tree Rotations
Double rotations:
• AVL balance restored:
1 3
13
15
16
10
142 11
12
• Now insert 4.
![Page 24: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/24.jpg)
AVL Tree Rotations
Double rotations:
• AVL violation - rotate
1 3
13
15
16
10
142 11
12
4
![Page 25: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/25.jpg)
AVL Tree Rotations
Double rotations:
• Rotation type:
1 3
13
15
16
10
142 11
12
4
![Page 26: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/26.jpg)
AVL Tree Rotations
Double rotations:
10
13
15
2
111 3
4 12 14 16
• Now insert 5.
![Page 27: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/27.jpg)
AVL Tree Rotations
Double rotations:
10
13
15
2
111 3
4 12 14 16
• AVL violation – rotate.
5
![Page 28: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/28.jpg)
AVL Tree RotationsSingle rotations:
10
13
15
2
111 3
4 12 14 16
5
• Rotation type:
![Page 29: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/29.jpg)
AVL Tree RotationsSingle rotations:
10
13
15
2
111 4
5 12 14 163
• AVL balance restored:
• Now insert 7.
![Page 30: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/30.jpg)
AVL Tree RotationsSingle rotations:
10
13
15
2
111 4
5 12 14 163
• AVL violation – rotate.
7
![Page 31: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/31.jpg)
AVL Tree RotationsSingle rotations:
10
13
15
2
111 4
5 12 14 163
7
• Rotation type:
![Page 32: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/32.jpg)
AVL Tree RotationsDouble rotations:
10
13
15
4
112 5
7 12 14 1631
• AVL balance restored.
• Now insert 6.
![Page 33: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/33.jpg)
AVL Tree RotationsDouble rotations:
10
13
15
4
112 5
7 12 14 1631
• AVL violation - rotate.
6
![Page 34: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/34.jpg)
AVL Tree RotationsDouble rotations:
10
13
15
4
112 5
7 12 14 1631
• Rotation type:
6
![Page 35: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/35.jpg)
AVL Tree RotationsDouble rotations:
10
13
15
4
112 6
712 14 16
31
• AVL balance restored.
• Now insert 9 and 8.
5
![Page 36: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/36.jpg)
AVL Tree RotationsDouble rotations:
10
13
15
4
112 6
712 14 16
31
• AVL violation - rotate.
9
8
5
![Page 37: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/37.jpg)
AVL Tree RotationsDouble rotations:
10
13
15
4
112 6
712 14 16
31
• Rotation type:
9
8
5
![Page 38: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719](https://reader035.vdocument.in/reader035/viewer/2022062312/551c2975550346a34f8b5f9a/html5/thumbnails/38.jpg)
AVL Tree RotationsFinal tree:
10
13
15
4
112 6
812 14 16
31
• Tree is almost perfectly balanced
5
97