avl trees (adelson – velskii – landis) avl tree: 20 30 187 1625 5 1719

Post on 01-Apr-2015

227 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

AVL Trees(Adelson – Velskii – Landis)

AVL tree:20

30187

16 25

5 17 19

AVL Trees

Not AVL tree:

27

20

30187

16 25

5 17 19

AVL Tree Rotations

Single rotations: insert 14, 15, 16, 13, 12, 11, 10

14

15

• First insert 14 and 15:

• Now insert 16.

AVL Tree Rotations

Single rotations:

14

15

• Inserting 16 causes AVL violation:

• Need to rotate.

16

AVL Tree Rotations

Single rotations:

14

15

• Inserting 16 causes AVL violation:

• Need to rotate.

16

AVL Tree Rotations

Single rotations:

14

15

• Rotation type:

16

AVL Tree Rotations

Single rotations:

14

15

• Rotation restores AVL balance:

16

AVL Tree Rotations

Single rotations:

14

15

16

• Now insert 13 and 12:

13

12

• AVL violation - need to rotate.

AVL Tree Rotations

Single rotations:

• Rotation type:

14

15

16

13

12

AVL Tree Rotations

Single rotations:

13

15

16

• Now insert 11.

12 14

AVL Tree Rotations

Single rotations:

13

15

16

• AVL violation – need to rotate

12 14

11

AVL Tree Rotations

Single rotations:

• Rotation type:

13

15

16

12 14

11

AVL Tree Rotations

Single rotations:

13

15

16

12

1411

• Now insert 10.

AVL Tree Rotations

Single rotations:

13

15

16

12

1411

10

• AVL violation – need to rotate

AVL Tree Rotations

Single rotations:

13

15

16

12

1411

10

• Rotation type:

AVL Tree Rotations

Single rotations:

13

15

16

11

1410 12

• AVL balance restored.

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

AVL Tree RotationsDouble rotations:

13

15

16

11

1410

• AVL violation - rotate

1

2

12

AVL Tree RotationsDouble rotations:

13

15

16

11

1410

• Rotation type:

1

2

12

AVL Tree Rotations

Double rotations:

• Now insert 3.

• AVL balance restored:

1 10

13

15

16

11

142 12

AVL Tree Rotations

Double rotations:

• AVL violation – rotate:

1 10

13

15

16

11

142 12

3

AVL Tree Rotations

Double rotations:

• Rotation type:

1 10

13

15

16

11

142 12

3

AVL Tree Rotations

Double rotations:

• AVL balance restored:

1 3

13

15

16

10

142 11

12

• Now insert 4.

AVL Tree Rotations

Double rotations:

• AVL violation - rotate

1 3

13

15

16

10

142 11

12

4

AVL Tree Rotations

Double rotations:

• Rotation type:

1 3

13

15

16

10

142 11

12

4

AVL Tree Rotations

Double rotations:

10

13

15

2

111 3

4 12 14 16

• Now insert 5.

AVL Tree Rotations

Double rotations:

10

13

15

2

111 3

4 12 14 16

• AVL violation – rotate.

5

AVL Tree RotationsSingle rotations:

10

13

15

2

111 3

4 12 14 16

5

• Rotation type:

AVL Tree RotationsSingle rotations:

10

13

15

2

111 4

5 12 14 163

• AVL balance restored:

• Now insert 7.

AVL Tree RotationsSingle rotations:

10

13

15

2

111 4

5 12 14 163

• AVL violation – rotate.

7

AVL Tree RotationsSingle rotations:

10

13

15

2

111 4

5 12 14 163

7

• Rotation type:

AVL Tree RotationsDouble rotations:

10

13

15

4

112 5

7 12 14 1631

• AVL balance restored.

• Now insert 6.

AVL Tree RotationsDouble rotations:

10

13

15

4

112 5

7 12 14 1631

• AVL violation - rotate.

6

AVL Tree RotationsDouble rotations:

10

13

15

4

112 5

7 12 14 1631

• Rotation type:

6

AVL Tree RotationsDouble rotations:

10

13

15

4

112 6

712 14 16

31

• AVL balance restored.

• Now insert 9 and 8.

5

AVL Tree RotationsDouble rotations:

10

13

15

4

112 6

712 14 16

31

• AVL violation - rotate.

9

8

5

AVL Tree RotationsDouble rotations:

10

13

15

4

112 6

712 14 16

31

• Rotation type:

9

8

5

AVL Tree RotationsFinal tree:

10

13

15

4

112 6

812 14 16

31

• Tree is almost perfectly balanced

5

97

top related