cse 326: data structures lecture #10 balancing act and what avl stands for steve wolfman winter...
Post on 21-Dec-2015
217 views
TRANSCRIPT
CSE 326: Data StructuresLecture #10
Balancing Act andWhat AVL Stands For
Steve Wolfman
Winter Quarter 2000
Today’s Outline
• Double-tailed distributions and the mean• AVL trees • What AVL Stands For• Bonus! (Deletion)
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 onebranch is much longer than the other!
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 log n
AVL Tree Dictionary Data Structure
4
121062
115
8
14137 9
• Binary search tree properties– binary tree property
– search tree property
• Balance property– balance of every node is:
-1 b 1– result:
• depth is (log n)
15
But, How Do We Stay Balanced?
• I need:– the smallest person in the class
– the tallest person in the class
– the averagest (?) person in the class
General Single Rotation
• Height of subtree same as it was before insert!• Height of all ancestors unchanged.
So?
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
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?
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
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
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
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
AVL Algorithm Revisited• Recursive1. Search downward for
spot
2. Insert node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate
b. If imbalance #2,
double rotate
• Iterative1. Search downward for
spot, stacking
parent nodes
2. Insert node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate and
exit
b. If imbalance #2,
double rotate and
exit
Single Rotation Codevoid RotateRight(Node *& root) {
Node * temp = root->right;
root->right = temp->left;
temp->left = root;
root->height = max(root->right->height,
root->left->height) + 1;
temp->height = max(temp->right->height,
temp->left->height) + 1;
root = temp;
}
X
Y
Z
root
temp
Double Rotation Codevoid DoubleRotateRight(Node *& root) {
RotateLeft(root->right);
RotateRight(root);
}
a
Z
b
W
c
XY
a
Z
c
b
X
Y
W
First Rotation
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 Landis
Single Rotation on Deletion
2092
175
10
303
1 10
2 2
3
00
3092
205
10
17
3
1 00
2 1
3
0
0
Something very bad happened!
To Do
• Finish Project II• Read chapter 4 in the book• Understand Zasha’s worksheet• Prepare for the midterm