AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719.

38
AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 18 7 16 25 5 17 19

Transcript of AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719.

Page 1: AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 30 187 1625 5 1719.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

AVL Tree RotationsFinal tree:

10

13

15

4

112 6

812 14 16

31

• Tree is almost perfectly balanced

5

97