CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1.
-
Upload
natalie-johnson -
Category
Documents
-
view
218 -
download
1
Transcript of CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1.
CPSC 221: Algorithms and Data Structures
Lecture #6Balancing Act
Steve Wolfman
2014W1
1
Today’s Outline
• Addressing one of our problems• Single and Double Rotations• AVL Tree Implementation
2
Beauty is Only (log n) Deep
• Binary Search Trees are fast if they’re shallow:– perfectly complete– perfectly complete except the one level fringe (like a heap)– anything else?
What matters here?Problems occur when onesubtree is much taller than the other! 3
Balance
• Balance– height(left subtree) - height(right subtree)– zero everywhere perfectly balanced– small everywhere balanced enough
t
57
Balance between -1 and 1 everywhere maximum height of ~1.44 lg n 4
AVL Tree Dictionary Data Structure
4
121062
115
8
14137 9
• Binary search tree properties– binary tree invariant– search tree invariant
• Balance invariant– balance of every node is:
-1 b 1– result:
• depth is (log n)
15
5Note that (statically… ignoring how it updates) an AVL tree is a BST.
Testing the Balance Property
2092
155
10
30177
NULLs have height -1
6FIRST calculate heightsTHEN calculate balances
An AVL Tree
20
92 15
5
10
30
177
0
0 0
011
2 2
3 10
3
data
height
children
7
Today’s Outline
• Addressing one of our problems• Single and Double Rotations• AVL Tree Implementation
8
But, How Do We Stay Balanced?
9
Need: three volunteers proud of their very diverse height.
Beautiful Balance (SIMPLEST version)
Insert(middle)
Insert(small)
Insert(tall)
00
1
10But… BSTs are under-constrained in unfortunate ways;
ours may not look like this.
Bad Case #1(SIMPLEST version)
Insert(small)
Insert(middle)
Insert(tall)
0
1
2
11How do we fix the bad case?
How do we transition among different possible trees?
So you say you want a revolution?
Scenario: k is one of the 1%; we want a revolution to depose k and give m root privileges.
What do we do?
Well, first…
12
k
Z
m
Y
X
Well.. what do we know?
Scenario: k is one of the 1%; we want a revolution to depose k and give m root privileges.
What’s everything weknow about nodes k and m and subtrees X,Y, and Z?
13
k
Z
m
Y
X
A real solution?
Scenario: k is one of the 1%; we want a revolution to depose k and give m root privileges.
Now, how can we makem the root?
14
k
Z
m
Y
X
Single Rotation(SIMPLEST version)
0
1
2
00
1
15
General Single Rotation
• After rotation, subtree’s height same as before insert!• Height of all ancestors unchanged.
a
X
Y
b
Z
a
XY
b
Zh h - 1
h + 1 h - 1
h + 2
h
h - 1
h
h - 1
h + 1
An insert made this BAD!
16Why couldn’t the bad insert be in X?Why does it matter that ancestors stay the same?
Bad Case #2(SIMPLEST version)
Insert(small)
Insert(tall)
Insert(middle)
0
1
2
17
Double Rotation(SIMPLEST version)
00
1
0
1
2
0
1
2
18
General Double Rotation
• Height of subtree still the same as it was before insert!• Height of all ancestors unchanged.
a
Z
b
W
c
X Y
a
Z
b
W
c
X Y
h
h - 1?
h - 1
h - 1
h + 2
h + 1
h - 1h - 1
h
h + 1
h
h - 1?
19
Today’s Outline
• Addressing one of our problems• Single and Double Rotations• AVL Tree Implementation
20
Insert Algorithm
• Find spot for value• Hang new node• Search back up for imbalance• If there is an imbalance:
case #1: Perform single rotation and exit
case #2: Perform double rotation and exit
Mirrored cases also possible
21
Easy Insert
2092
155
10
3017
Insert(3)
120
0
100
1 2
3
0
22
Hard Insert (Bad Case #1)
2092
155
10
3017
Insert(33)
3
121
0
100
2 2
3
00
23
Single Rotation
2092
155
10
30173
12
33
1
0
200
2 3
3
10
0
3092
205
10
333
151
0
110
2 2
3
001712
0
24
Hard Insert (Bad Case #2)
Insert(18)
2092
155
10
30173
121
0
100
2 2
3
00
25
Single Rotation (oops!)
2092
155
10
30173
121
1
200
2 3
3
00
3092
205
10
3
151
1
020
2 3
3
01712
0
180
180
26
Double Rotation (Step #1)
2092
155
10
30173
121
1
200
2 3
3
00
180
1792
155
10
203
121 200
2 3
3
10
300
Look familiar?18
0
27
Double Rotation (Step #2)
1792
155
10
203
121 200
2 3
3
10
300
180
2092
175
10
303
151
0
110
2 2
3
0012
018
28
AVL Algorithm Revisited• Recursive1. Search downward for spot2. Insert node3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate
• Iterative1. Search downward for spot, stacking parent nodes2. Insert node3. Unwind stack, correcting heights a. If imbalance #1, single rotate and exit b. If imbalance #2, double rotate and exit 29
Single Rotation Codevoid RotateLeft(Node *& root) { Node * temp = root->right; root->right = temp->left; temp->left = root; root->height = max(height(root->right), height(root->left)) + 1; temp->height = max(height(temp->right), height(temp->left) + 1; root = temp;}
X
Y
Z
root
temp
(The “height” function returns -1 for a NULL subtreeor the “height” field of a non-NULL subtree.)
30
Double Rotation Codevoid DoubleRotateLeft(Node *& root) { RotateRight(root->right); RotateLeft(root);}
a
Z
b
W
c
XY
a
Z
c
b
X
Y
W
First Rotation
31
Double Rotation Completed
a
Z
c
b
X
Y
W
a
Z
c
b
XY
W
First Rotation Second Rotation
32
AVL
• Automatically Virtually Leveled• Architecture for inVisible Leveling (the “in” is inVisible)
• All Very Low• Articulating Various Lines• Amortizing? Very Lousy!• Absolut Vodka Logarithms• Amazingly Vexing Letters
Adelson-Velskii Landis33
To Do
• Posted readings on priority queues and sorts, but de-emphasizing heaps
34
Coming Up
• Quick Jump Back to Priority Queue ADT– A very familiar data structure gives us O(lg n)
performance!
• On to sorts• Then forward again!
35