CSE 326 Data Structures Lecture 10 Balancing Act
CSE 326: Data Structures Lecture #10 Balancing Act and What 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 except the one level fringe (like a heap) – anything else? What matters here? Problems occur when one branch is much longer than the other!
Balance • Balance t 5 – height(left subtree) - height(right subtree) – zero everywhere perfectly balanced – small everywhere balanced enough Balance between -1 and 1 everywhere maximum height of 1. 44 log n 7
AVL Tree Dictionary Data Structure • Binary search tree properties 8 – binary tree property – search tree property 5 11 • Balance property – balance of every node is: -1 b 1 – result: • depth is (log n) 2 6 4 10 7 9 12 13 14 15
Testing the Balance Property 10 5 15 2 9 7 NULLs have height -1 20 17 30
An AVL Tree 10 3 3 10 2 0 5 20 1 2 9 0 7 1 30 0 17 height children 2 0 15 data
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
Beautiful Balance Insert(middle) Insert(small) Insert(tall) 1 0 0
Bad Case #1 Insert(small) Insert(middle) Insert(tall) 2 1 0
Single Rotation 2 1 1 0 0 0
General Single Rotation a h+1 h+2 b h-1 b h a h X h h-1 Y h-1 Z Y Z • Height of subtree same as it was before insert! • Height of all ancestors unchanged. So? X
Bad Case #2 Insert(small) Insert(tall) Insert(middle) 2 1 0
Double Rotation 2 2 1 1 0 0 0
General Double Rotation a h+2 h+1 b h-1 c W h - 1? c h-1 h Z a h-1 X X Y h Y W Z h - 1? • Height of subtree still the same as it was before insert! • Height of all ancestors unchanged.
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
Easy Insert 3 10 Insert(3) 1 0 2 2 5 15 9 0 0 1 12 20 0 17 0 30
Hard Insert (Bad Case #1) 3 10 Insert(33) 2 2 5 15 1 2 0 9 3 0 0 1 12 20 0 17 0 30
Single Rotation 3 3 10 2 3 5 0 9 3 2 15 1 2 10 0 0 20 0 17 5 20 1 2 12 2 2 0 1 30 0 33 9 3 0 1 1 15 0 12 17 30 0 0 33
Hard Insert (Bad Case #2) 3 10 Insert(18) 2 2 5 15 1 2 0 9 3 0 0 1 12 20 0 17 0 30
Single Rotation (oops!) 3 3 10 2 3 5 0 9 3 2 15 1 2 10 0 0 20 1 2 0 17 30 0 18 5 20 1 2 12 3 0 9 3 0 2 0 15 0 12 30 17 1 0 18
Double Rotation (Step #1) 3 3 10 2 3 5 0 9 3 2 15 1 2 10 0 0 20 1 2 0 17 5 15 1 2 12 3 30 0 9 0 0 2 12 17 1 3 20 0 0 18 18 Look familiar? 0 30
Double Rotation (Step #2) 3 3 10 2 3 5 0 2 15 1 2 10 9 0 0 17 2 0 1 3 20 0 18 5 17 1 2 12 2 0 30 9 3 0 1 1 15 0 12 20 0 0 18 30
AVL Algorithm Revisited • Recursive • Iterative 1. Search downward for spot 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate 1. 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 Code void Rotate. Right(Node *& root) { Node * temp = root->right; root->right = temp->left; X 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; } root temp Y Z
Double Rotation Code void Double. Rotate. Right(Node *& root) { Rotate. Left(root->right); Rotate. Right(root); } First a a b Z c Y Rotation X W b Y X W
Double Rotation Completed Second Rotation First Rotation c a c Z b a b Y Y Z X W
AVL • • Automatically Virtually Leveled Architecture for in. Visible Leveling (the “in” is in. Visible) All Very Low Articulating Various Lines Amortizing? Very Lousy! Absolut Vodka Logarithms Amazingly Vexing Letters Adelson-Velskii Landis
Bonus: Deletion (Easy Case) 3 10 Delete(15) 2 2 5 15 1 2 0 9 3 0 0 1 12 20 0 17 0 30
Deletion (Hard Case #1) 3 10 Delete(12) 2 2 5 17 1 2 0 9 3 0 0 12 1 20 0 30
Single Rotation on Deletion 3 3 10 2 2 5 0 9 3 2 17 1 2 10 0 1 5 20 1 1 20 2 0 30 0 9 0 0 17 0 30 3 Something very bad happened!
To Do • • Finish Project II Read chapter 4 in the book Understand Zasha’s worksheet Prepare for the midterm
Coming Up • • • A bit more AVL trees Splay trees Another worksheet and midterm study guide Second project due (January 31 st) Midterm (February 4 th)
- Slides: 33