Upload
others
View
14
Download
0
Embed Size (px)
Citation preview
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Deletion - Example 2
Double rotation
Right most child of left subtree = I
Ok here! Lecture 14 ECE250 21
Deletion - Example 2 (Cont’d)
New case
Lecture 14 ECE250 22