algorithms and data structures - star - homeece250/materials/notes/lecture14-av… · avl tree v...

22
ECE250: Algorithms and Data Structures AVL Trees (Part B) Materials from Weiss: Chapter 4.4.2 Ladan Tahvildari, PEng, SMIEEE Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University of Waterloo

Upload: others

Post on 08-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

ECE250: Algorithms and Data Structures

AVL Trees (Part B)

Materials from Weiss: Chapter 4.4.2

Ladan Tahvildari, PEng, SMIEEE Professor

Software Technologies Applied Research (STAR) Group

Dept. of Elect. & Comp. Eng.

University of Waterloo

Page 2: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Acknowledgements

v The following resources have been used to prepare materials for this course: Ø  MIT OpenCourseWare Ø  Introduction To Algorithms (CLRS Book) Ø  Data Structures and Algorithm Analysis in C++ (M. Wiess) Ø  Data Structures and Algorithms in C++ (M. Goodrich)

v Thanks to many people for pointing out mistakes, providing suggestions, or helping to improve the quality of this course over the last ten years: Ø  http://www.stargroup.uwaterloo.ca/~ece250/acknowledgment/

Lecture 14 ECE250 2

Page 3: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

AVL Tree v  AVL tree is the first balanced binary search tree (name after its

discovers, Adelson-Velskii and Landis). v  An AVL tree is a BST in which

Ø  for every node in the tree, the height of the left and right subtrees differ by at most 1.

v  Height of subtree: Max # of edges to a leaf v  Height of an empty subtree: -1

Ø  Height of one node: 0

AVL property violated here

AVL tree

Lecture 14 ECE250 3

Page 4: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Review of Rotations

When the AVL property is lost we can rebalance the tree via rotations v Single Right Rotation (SRR)

Ø Performed when A is unbalanced to the left (the left subtree is 2 higher than the right subtree) and B is left-heavy (the left subtree of B is 1 higher than the right subtree of B).

SRR at A A B T3

T1 T2

B A

T3 T1

T2 Lecture 14 ECE250 4

Page 5: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Review of Rotations

v Single Left Rotation (SLR) Ø Performed when A is unbalanced to the right (the

right subtree is 2 higher than the left subtree) and B is right-heavy (the right subtree of B is 1 higher than the left subtree of B).

SLR at A A T1 B

T2 T3

B A T3

T1 T2

Lecture 14 ECE250 5

Page 6: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Different Cases for Rebalance v Denote the node that must be rebalanced α

Ø Case 1: an insertion into the left subtree of the left child of α

Ø Case 2: an insertion into the right subtree of the left child of α

Ø Case 3: an insertion into the left subtree of the right child of α

Ø Case 4: an insertion into the right subtree of the right child of α

Lecture 14 ECE250 6

Page 7: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Rotations

v Double Left Rotation (DLR) Ø  Performed when C is unbalanced to the left (the left subtree

is 2 higher than the right subtree), A is right-heavy (the right subtree of A is 1 higher than the left subtree of A)

Ø  Consists of a single left rotation at node A, followed by a single right at node C

SLR at A C A T4

T1 B

T2 T3

C B T4

A T3

T1 T2

SRR at C B A C

T1 T2 T3 T4 DLR = SLR + SRR

Note: this is case 1

Lecture 14 7 ECE250

Page 8: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Rotations v Double Right Rotation (DRR)

Ø  Performed when A is unbalanced to the right (the right subtree is 2 higher than the left subtree), C is left heavy (the left subtree of C is 1 higher than the right subtree of C)

Ø  Consists of a single right rotation at node C, followed by a single left rotation at node A

SRR at C A T1 C

B T4

T2 T3

A T1 B

T2 C

T3 T4

SLR at A B A C

T1 T2 T3 T4 DRR = SRR + SLR

Note: this is case 4

Lecture 14 8 ECE250

Page 9: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Recall Cases 2&3

v Single rotation fails to fix case 2&3 v Take case 2 as an example (case 3 is a symmetric to it )

Ø  The problem is that the subtree Y is too deep Ø  Single rotation doesn’t make Y any less deep…

Single rotation fails Case 2: violation in k2 because of insertion in subtree Y

Lecture 14 ECE250 9

Page 10: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Double Rotation

v Facts Ø  The new key is inserted in the subtree B or C Ø  The AVL-property is violated at k3

Ø  k3-k1-k2 forms a zig-zag shape: LR case

v Solution Ø  place k2 as the new root

Double rotation to fix case 2

Lecture 14 ECE250 10

Page 11: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Double Rotation to fix Case 3 (right-left)

v Facts Ø The new key is inserted in the subtree B or C Ø The AVL-property is violated at k1

Ø k1-k3-k2 forms a zig-zag shape

v Case 3 is a symmetric case to case 2

Double rotation to fix case 3

Lecture 14 ECE250 11

Page 12: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Restart our example We’ve inserted 3, 2, 1, 4, 5, 6, 7, 16

We’ll insert 15, 14, 13, 12, 11, 10, 8, 9

4

2 6

7 3 1 5

16

15

Insert 16, fine Insert 15

violation at node 7

k1

k3

k2

4

2 6

15 3 1 5

16 7 Double rotation

k2

k1 k3

Lecture 14 ECE250 12

Page 13: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

4

2 6

15 3 1 5

16 7

14 Insert 14

k1

k3

k2

4

2 7

15 3 1 6

16 14

Double rotation

k2

k3

5

k1

A

C

D

4

2 7

15 3 1 6

16 14 5 Insert 13

13

7

4 15

16 6 2 14

13 5 3 1 Single rotation

k1

k2

Z

X

Y

Page 14: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

7

4 15

16 6 2 14

13 5 3 1

12 Insert 12

7

4 15

16 6 2 13

12 5 3 1 14

Single rotation

7

4 15

16 6 2 13

12 5 3 1 14

11 Insert 11

7

4 13

15 6 2 12

11 5 3 1 16

Single rotation

14

Page 15: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

9

7

4 13

15 6 2 12

11 5 3 1 16 14

Insert 10 10

7

4 13

15 6 2 11

10 5 3 1 16 14

Single rotation

12

7

4 13

15 6 2 11

10 5 3 1 16 14 12

8 Insert 8, fine then insert 9

7

4 13

15 6 2 11

9 5 3 1 16 14 12

8

Double rotation

10

Page 16: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Insertion Analysis v Insert the new key as a new leaf just as in

ordinary binary search tree: O(logN) v Then trace the path from the new leaf towards

the root, for each node x encountered: O(logN) Ø Check height difference: O(1) Ø  If satisfies AVL property, proceed to next node: O(1) Ø  If not, perform a rotation: O(1)

v The insertion stops when Ø A rotation is performed Ø Or, we’ve checked all nodes in the path

v Time complexity for insertion O(logN)

logN

Lecture 14 ECE250 16

Page 17: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Deletion from AVL Tree

v Delete a node x as in ordinary binary search tree Ø  Note that the last (deepest) node in a tree deleted is a leaf or a

node with one child

v Then, trace the path from the new leaf towards the root

v  For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. Ø  If yes, proceed to parent(x) Ø  If no, perform an appropriate rotation at x Continue to trace the path until we reach the root

Lecture 14 ECE250 17

Page 18: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Deletion - Example 1

Delete 5, Node 10 is unbalanced

20

10 35

40 15 5 25

18 45 38 30

50 50 Single Rotation

20

15 35

40 18 10 25

45 38 30

Lecture 14 ECE250 18

Page 19: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Deletion – Example 1 (Cont’d)

For deletion, after rotation, we need to continue tracing upward to see if AVL-tree property is violated at other node.

20

15 35

40 18 10 25

45 38 30

50 Continue to check parents Oops!! Node 20 is unbalanced!!

20

15

35

40

18 10

25 45 38

30 50

Single Rotation

Lecture 14 ECE250 19

Page 20: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Rotation in Deletion v  The rotation strategies (single or double) we learned can be reused here v  Except for one new case: two subtrees of y are of the same height è

in that case, a single rotation is ok

rotate with right child

rotate with left child

Lecture 14 ECE250 20

Page 21: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Deletion - Example 2

Double rotation

Right most child of left subtree = I

Ok here! Lecture 14 ECE250 21

Page 22: Algorithms and Data Structures - STAR - Homeece250/materials/notes/Lecture14-AV… · AVL Tree v AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii

Deletion - Example 2 (Cont’d)

New case

Lecture 14 ECE250 22