6 v CHAPTER 10 AVL TREES 8 3

  • Slides: 15
Download presentation
6 v CHAPTER 10 AVL TREES 8 3 z 4 1 ACKNOWLEDGEMENT: THESE SLIDES

6 v CHAPTER 10 AVL TREES 8 3 z 4 1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM JORY DENNY AND MUKULIKA GHOSH

AVL TREE DEFINITION 44 2 4 3 17 78 32 2 1 50 1

AVL TREE DEFINITION 44 2 4 3 17 78 32 2 1 50 1 48 88 1 1 62 An example of an AVL tree where the heights are shown next to the nodes: 2

INSERTION IN AN AVL TREE 44 44 17 17 78 • Insertion is as

INSERTION IN AN AVL TREE 44 44 17 17 78 • Insertion is as in a binary search tree • Always done by expanding an external node. 32 50 88 • Example insert 54: 48 62 78 32 50 88 48 62 54 Before Insertion After Insertion 3

TRINODE RESTRUCTURING (other two cases are symmetrical) a=z case 2: double rotation (a right

TRINODE RESTRUCTURING (other two cases are symmetrical) a=z case 2: double rotation (a right rotation about c, then a left rotation about a) a=z b=y c=y T 0 c=x b=x T 1 b=y T 2 b=x T 3 a=z case 1: single rotation (a left rotation about a) T 1 c=x T 2 a=z c=y 4 T 0 T 1 T 2 T 3

INSERTION EXAMPLE, CONTINUED unbalanced. . . T 1 44 2 4 x 3 17

INSERTION EXAMPLE, CONTINUED unbalanced. . . T 1 44 2 4 x 3 17 32 2 1 1 48 62 y z 78 50 2 1 1 54 88 T 2 . . . balanced T 0 T 1 5 T 3

RESTRUCTURING SINGLE ROTATIONS 6

RESTRUCTURING SINGLE ROTATIONS 6

RESTRUCTURING DOUBLE ROTATIONS 7

RESTRUCTURING DOUBLE ROTATIONS 7

EXERCISE AVL TREES Insert into an initially empty AVL tree items with the following

EXERCISE AVL TREES Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree 30, 40, 24, 58, 48, 26, 11, 13 8

REMOVAL IN AN AVL TREE remove 32 44 17 62 32 50 48 44

REMOVAL IN AN AVL TREE remove 32 44 17 62 32 50 48 44 17 62 78 54 50 88 48 78 54 88 9 before deletion of 32 after deletion

REBALANCING AFTER A REMOVAL a=z w 62 44 17 50 48 c=x 78 54

REBALANCING AFTER A REMOVAL a=z w 62 44 17 50 48 c=x 78 54 44 b=y 62 88 17 78 50 48 88 54 10

REBALANCING AFTER A REMOVAL a=z w 62 44 17 50 48 c=x 78 54

REBALANCING AFTER A REMOVAL a=z w 62 44 17 50 48 c=x 78 54 44 b=y 62 88 17 78 50 48 88 54 11

EXERCISE AVL TREES Insert into an initially empty AVL tree items with the following

EXERCISE AVL TREES Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree 30, 40, 24, 58, 48, 26, 11, 13 Now, remove the item with key 48. Draw the resulting tree Now, remove the item with key 58. Draw the resulting tree 12

n(2) HEIGHT OF AN AVL TREE 3 4 n(1) 13

n(2) HEIGHT OF AN AVL TREE 3 4 n(1) 13

RUNNING TIMES FOR AVL TREES 14

RUNNING TIMES FOR AVL TREES 14

OTHER TYPES OF SELF-BALANCING TREES 9 2 5 7 10 14 9 4 2

OTHER TYPES OF SELF-BALANCING TREES 9 2 5 7 10 14 9 4 2 15 6 21 12 7 15