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 –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 –height(left subtree) - height(right subtree) –zero everywhere perfectly balanced –small everywhere balanced enough t 5 7 Balance between -1 and 1 everywhere maximum height of 1.44 log n
AVL Tree Dictionary Data Structure 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
Testing the Balance Property NULL s have height -1
An AVL Tree data height children
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) 00 1
Bad Case #1 Insert(small) Insert(middle) Insert(tall) 0 1 2
Single Rotation
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 Z h h - 1 h + 1 h - 1 h + 2 h h - 1 h h + 1
Bad Case #2 Insert(small) Insert(tall) Insert(middle) 0 1 2
Double Rotation
General Double Rotation Height of subtree still the same as it was before insert! Height of all ancestors unchanged. a Z b W c XY a Z b W c XY h h - 1? h - 1 h + 2 h + 1 h - 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
Easy Insert Insert(3)
Hard Insert (Bad Case #1) Insert(33)
Single Rotation
Hard Insert (Bad Case #2) Insert(18)
Single Rotation (oops!)
Double Rotation (Step #1) Look familiar? 18 0
Double Rotation (Step #2)
AVL Algorithm Revisited Recursive 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 Iterative 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 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 Code void DoubleRotateRight(Node *& root) { RotateLeft(root->right); RotateRight(root); } a Z b W c XY a Z c b X Y W First Rotation
Double Rotation Completed a Z c b X Y W a Z c b X Y W First Rotation Second 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
Bonus: Deletion (Easy Case) Delete(15)
Deletion (Hard Case #1) Delete(12)
Single Rotation on Deletion 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 )