Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2011W2 Today’s Outline • Addressing one of our problems • Single and Double Rotations • AVL Tree Implementation 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 • 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 lg 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 3 10 2 20 1 0 2 9 0 1 0 15 30 0 7 17 data 3 height children 2 5 10 Today’s Outline • Addressing one of our problems • Single and Double Rotations • AVL Tree Implementation But, How Do We Stay Balanced? • Need: three volunteers proud of their very diverse height. Beautiful Balance (SIMPLEST version) Insert(middle) Insert(small) Insert(tall) 1 0 0 Bad Case #1 (SIMPLEST version) Insert(small) Insert(middle) Insert(tall) 2 1 0 Single Rotation (SIMPLEST version) 2 1 1 0 0 0 General Single Rotation h+1 h+2 a b h h-1 h+1 b h a X h h-1 Y Z h-1 h-1 Z Y An insert made this BAD! • After rotation, subtree’s height same as before insert! • Height of all ancestors unchanged. So? X Bad Case #2 (SIMPLEST version) Insert(small) Insert(tall) Insert(middle) 2 1 0 Double Rotation (SIMPLEST version) 2 2 1 1 0 1 0 0 0 General Double Rotation a h+2 h+1 h+1 b h c h - 1? h b h-1 W c h-1 h Z a h-1 h-1 X X Y Y W Z h - 1? • Height of subtree still the same as it was before insert! • Height of all ancestors unchanged. Today’s Outline • Addressing one of our problems • Single and Double Rotations • AVL Tree Implementation 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 Easy Insert 3 10 Insert(3) 1 2 5 15 0 0 2 9 0 1 12 20 0 0 17 30 Hard Insert (Bad Case #1) 3 10 Insert(33) 2 2 5 15 1 0 2 9 0 0 1 12 20 0 3 0 17 30 Single Rotation 3 3 10 10 2 3 5 15 1 0 2 9 0 0 12 2 5 20 1 2 20 0 3 2 0 2 17 30 0 3 0 33 1 15 9 0 1 1 30 0 12 17 0 33 Hard Insert (Bad Case #2) 3 10 Insert(18) 2 2 5 15 1 0 2 9 0 0 1 12 20 0 3 0 17 30 Single Rotation (oops!) 3 3 10 10 2 3 5 15 1 0 2 9 0 2 5 0 20 3 17 30 0 18 0 2 0 1 20 1 2 12 3 2 15 9 0 0 30 0 3 1 12 17 0 18 Double Rotation (Step #1) 3 3 10 10 2 3 5 15 1 0 2 9 0 2 5 0 20 3 0 2 0 1 15 1 2 12 3 17 30 9 0 2 12 17 0 1 3 20 0 0 18 18 Look familiar? 0 30 Double Rotation (Step #2) 3 3 10 10 2 3 5 15 1 0 2 2 9 2 5 0 1 2 12 17 0 18 30 1 15 0 3 0 1 9 0 20 0 0 2 1 3 17 20 0 12 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 RotateLeft(Node *& root) { Node * temp = root->right; root->right = temp->left; X 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; } root temp Y Z (The “height” function returns -1 for a NULL subtree or the “height” field of a non-NULL subtree.) Double Rotation Code void DoubleRotateLeft(Node *& root) { RotateRight(root->right); RotateLeft(root); } First a a b Z c Z c Y Rotation X W b Y X W Double Rotation Completed Second Rotation First Rotation c a c b a Z b Y Y Z X W X W 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 To Do • Read KW 11.1-11.2, 11.5 Coming Up • Another approach to balancing, which leads to… • Huge Search Tree Data Structure