AVL Trees Adelson Velskii Landis AVL tree 20

  • Slides: 38
Download presentation
AVL Trees (Adelson – Velskii – Landis) AVL tree: 20 16 25 7 5

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

AVL Trees Not AVL tree: 20 16 25 7 5 30 18 17 19

AVL Trees Not AVL tree: 20 16 25 7 5 30 18 17 19 27

AVL Tree Rotations Single rotations: insert 14, 15, 16, 13, 12, 11, 10 •

AVL Tree Rotations Single rotations: insert 14, 15, 16, 13, 12, 11, 10 • First insert 14 and 15: 14 15 • Now insert 16.

AVL Tree Rotations Single rotations: • Inserting 16 causes AVL violation: 14 15 16

AVL Tree Rotations Single rotations: • Inserting 16 causes AVL violation: 14 15 16 • Need to rotate.

AVL Tree Rotations Single rotations: • Inserting 16 causes AVL violation: 14 15 16

AVL Tree Rotations Single rotations: • Inserting 16 causes AVL violation: 14 15 16 • Need to rotate.

AVL Tree Rotations Single rotations: • Rotation type: 14 15 16

AVL Tree Rotations Single rotations: • Rotation type: 14 15 16

AVL Tree Rotations Single rotations: • Rotation restores AVL balance: 15 14 16

AVL Tree Rotations Single rotations: • Rotation restores AVL balance: 15 14 16

AVL Tree Rotations Single rotations: • Now insert 13 and 12: 15 14 13

AVL Tree Rotations Single rotations: • Now insert 13 and 12: 15 14 13 12 • AVL violation - need to rotate. 16

AVL Tree Rotations Single rotations: • Rotation type: 15 14 13 12 16

AVL Tree Rotations Single rotations: • Rotation type: 15 14 13 12 16

AVL Tree Rotations Single rotations: 15 13 12 • Now insert 11. 16 14

AVL Tree Rotations Single rotations: 15 13 12 • Now insert 11. 16 14

AVL Tree Rotations Single rotations: 15 13 12 16 14 11 • AVL violation

AVL Tree Rotations Single rotations: 15 13 12 16 14 11 • AVL violation – need to rotate

AVL Tree Rotations Single rotations: 15 • Rotation type: 13 12 11 16 14

AVL Tree Rotations Single rotations: 15 • Rotation type: 13 12 11 16 14

AVL Tree Rotations Single rotations: 13 15 12 11 • Now insert 10. 14

AVL Tree Rotations Single rotations: 13 15 12 11 • Now insert 10. 14 16

AVL Tree Rotations Single rotations: 13 15 12 11 14 10 • AVL violation

AVL Tree Rotations Single rotations: 13 15 12 11 14 10 • AVL violation – need to rotate 16

AVL Tree Rotations Single rotations: • Rotation type: 13 15 12 11 10 14

AVL Tree Rotations Single rotations: • Rotation type: 13 15 12 11 10 14 16

AVL Tree Rotations Single rotations: 13 15 11 10 • AVL balance restored. 12

AVL Tree Rotations Single rotations: 13 15 11 10 • AVL balance restored. 12 14 16

AVL Tree Rotations Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9,

AVL Tree Rotations Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8 • First insert 1 and 2: 13 15 11 10 12 14 16

AVL Tree Rotations Double rotations: • AVL violation - rotate 13 15 11 10

AVL Tree Rotations Double rotations: • AVL violation - rotate 13 15 11 10 1 2 12 14 16

AVL Tree Rotations Double rotations: • Rotation type: 13 15 11 10 1 2

AVL Tree Rotations Double rotations: • Rotation type: 13 15 11 10 1 2 12 14 16

AVL Tree Rotations Double rotations: • AVL balance restored: 13 15 11 2 1

AVL Tree Rotations Double rotations: • AVL balance restored: 13 15 11 2 1 • Now insert 3. 12 10 14 16

AVL Tree Rotations Double rotations: • AVL violation – rotate: 13 15 11 2

AVL Tree Rotations Double rotations: • AVL violation – rotate: 13 15 11 2 12 1 10 3 14 16

AVL Tree Rotations Double rotations: • Rotation type: 13 15 11 2 12 1

AVL Tree Rotations Double rotations: • Rotation type: 13 15 11 2 12 1 10 3 14 16

AVL Tree Rotations Double rotations: • AVL balance restored: 13 15 10 2 1

AVL Tree Rotations Double rotations: • AVL balance restored: 13 15 10 2 1 • Now insert 4. 11 3 14 12 16

AVL Tree Rotations Double rotations: • AVL violation - rotate 13 15 10 2

AVL Tree Rotations Double rotations: • AVL violation - rotate 13 15 10 2 1 11 14 12 3 4 16

AVL Tree Rotations Double rotations: • Rotation type: 13 15 10 2 1 11

AVL Tree Rotations Double rotations: • Rotation type: 13 15 10 2 1 11 14 12 3 4 16

AVL Tree Rotations Double rotations: 10 13 2 1 4 • Now insert 5.

AVL Tree Rotations Double rotations: 10 13 2 1 4 • Now insert 5. 15 11 3 12 14 16

AVL Tree Rotations Double rotations: 10 13 2 1 15 11 3 12 4

AVL Tree Rotations Double rotations: 10 13 2 1 15 11 3 12 4 5 • AVL violation – rotate. 14 16

AVL Tree Rotations Single rotations: • Rotation type: 10 13 2 1 15 11

AVL Tree Rotations Single rotations: • Rotation type: 10 13 2 1 15 11 3 12 4 5 14 16

AVL Tree Rotations Single rotations: • AVL balance restored: 10 13 2 1 3

AVL Tree Rotations Single rotations: • AVL balance restored: 10 13 2 1 3 • Now insert 7. 15 11 4 5 12 14 16

AVL Tree Rotations Single rotations: • AVL violation – rotate. 10 13 2 1

AVL Tree Rotations Single rotations: • AVL violation – rotate. 10 13 2 1 3 15 11 4 12 5 7 14 16

AVL Tree Rotations Single rotations: • Rotation type: 10 13 2 1 3 15

AVL Tree Rotations Single rotations: • Rotation type: 10 13 2 1 3 15 11 4 12 5 7 14 16

AVL Tree Rotations Double rotations: • AVL balance restored. 10 13 4 2 1

AVL Tree Rotations Double rotations: • AVL balance restored. 10 13 4 2 1 3 • Now insert 6. 15 11 5 7 12 14 16

AVL Tree Rotations Double rotations: • AVL violation - rotate. 10 13 4 2

AVL Tree Rotations Double rotations: • AVL violation - rotate. 10 13 4 2 1 7 3 6 15 11 5 12 14 16

AVL Tree Rotations Double rotations: • Rotation type: 10 13 4 2 1 7

AVL Tree Rotations Double rotations: • Rotation type: 10 13 4 2 1 7 3 6 15 11 5 12 14 16

AVL Tree Rotations Double rotations: • AVL balance restored. 10 13 4 2 1

AVL Tree Rotations Double rotations: • AVL balance restored. 10 13 4 2 1 3 5 15 11 6 7 • Now insert 9 and 8. 12 14 16

AVL Tree Rotations Double rotations: • AVL violation - rotate. 10 13 4 2

AVL Tree Rotations Double rotations: • AVL violation - rotate. 10 13 4 2 1 3 5 15 11 6 12 7 9 8 14 16

AVL Tree Rotations Double rotations: • Rotation type: 10 13 4 2 1 3

AVL Tree Rotations Double rotations: • Rotation type: 10 13 4 2 1 3 5 15 11 6 12 7 9 8 14 16

AVL Tree Rotations Final tree: • Tree is almost perfectly balanced 10 13 4

AVL Tree Rotations Final tree: • Tree is almost perfectly balanced 10 13 4 2 1 3 15 11 6 5 12 8 7 9 14 16