440 likes | 450 Views
Learn about AVL trees, their balance properties, insertion cases, and rotation operations for maintaining balance. Includes easy and hard insertion cases with step-by-step examples.
E N D
CSE 326AVL Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001
Balance Balance == 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 t 5 7 CSE 326 Autumn 2001 2
Binary search tree properties binary tree property search tree property Balance property balance of every node is: -1b 1 result: depth is (log n) AVL Tree Dictionary Data Structure 8 5 11 2 6 10 12 4 7 9 13 14 15 CSE 326 Autumn 2001 3
An AVL Tree 10 data 3 3 height 10 children 1 2 5 15 0 0 1 0 12 20 2 9 0 0 17 30 CSE 326 Autumn 2001 4
Not AVL Trees 3 0-2 = -2 (-1)-1 = -2 2 10 10 0 2 1 5 15 15 0 1 0 12 20 20 0 0 17 30 Note: height(empty tree) == -1 CSE 326 Autumn 2001 5
Good Insert Case: Balance Preserved Good case: insert middle, then small,then tall Insert(middle) Insert(small) Insert(tall) 1 M 0 0 S T CSE 326 Autumn 2001 6
Bad Insert Case #1: Left-Left or Right-Right Imbalance Insert(small) Insert(middle) Insert(tall) 2 S 1 • BC#1 Imbalance caused by either: • Insert into left child’s left subtree • Insert into right child’s right subtree M 0 T CSE 326 Autumn 2001 7
Single Rotation 2 1 S M 1 M 0 0 S T 0 T Basic operation used in AVL trees: A right child could legally have its parent as its left child. CSE 326 Autumn 2001 8
General Bad Case #1 h + 2 a a h + 1 h - 1 h - 1 h h + 1 b X b X h h-1 h - 1 h - 1 Z Y Z Y Note: imbalance is left-left CSE 326 Autumn 2001 9
b a Z Y X Single RotationFixes Case #1 Imbalance • Height of left subtree same as it was before insert! • Height of all ancestors unchanged • We can stop here! a h + 1 h + 2 h h - 1 h + 1 b X h h - 1 h - 1 h h - 1 Z Y CSE 326 Autumn 2001 10
Bad Insert Case #2: Left-Right or Right-Left Imbalance Insert(small) Insert(tall) Insert(middle) 2 S 1 • BC#2 Imbalance caused by either: • Insert into left child’s right subtree • Insert into right child’s left subtree T 0 M Will a single rotation fix this? CSE 326 Autumn 2001 11
Double Rotation 2 2 S S 1 M 1 1 M T 0 0 0 S T 0 T M CSE 326 Autumn 2001 12
General Bad Case #2 h + 2 a a h + 1 h - 1 h - 1 h h + 1 b X b X h-1 h-1 h - 1 h Z Y Z Y Note: imbalance is left-right CSE 326 Autumn 2001 13
Double RotationFixes Case #2 Imbalance a h + 2 h + 1 h + 1 c h - 1 b Z h h b a h - 1 W h h - 1 h - 1 c X Y W Z X Y h - 1? h - 1? Initially: insert into either X or Y unbalances tree (root balance goes to 2 or -2) “Zig zag” to pull up c – restores root height to h+1, left subtree height to h CSE 326 Autumn 2001 14
AVL Insert Algorithm • Find spot for value • Hang new node • Search back up looking for imbalance • If there is an imbalance: case #1: Perform single rotation case #2: Perform double rotation • Done! (There can only be one imbalance!) CSE 326 Autumn 2001 15
Easy Insert 3 Insert(3) 10 1 2 5 15 0 0 1 0 12 2 9 20 0 0 17 30 CSE 326 Autumn 2001 16
3 2 2 1 0 0 1 0 0 0 Hard Insert (Bad Case #1) Insert(33) 10 5 15 12 2 9 20 3 17 30 CSE 326 Autumn 2001 17
3 3 2 2 3 2 1 1 0 0 0 1 1 2 0 0 0 1 0 0 Single Rotation 10 10 5 15 5 20 12 15 2 9 20 2 9 30 0 12 17 3 17 30 3 33 0 33 CSE 326 Autumn 2001 18
3 2 2 1 0 0 1 0 0 0 Hard Insert (Bad Case #2) Insert(18) 10 5 15 12 2 9 20 3 17 30 CSE 326 Autumn 2001 19
3 3 2 2 3 3 1 1 0 0 0 2 2 0 0 0 0 1 1 Single Rotation (oops!) 10 10 5 15 5 20 12 15 2 9 20 2 9 30 0 12 17 3 17 30 3 0 0 18 18 CSE 326 Autumn 2001 20
3 3 2 2 3 3 1 1 0 0 0 0 2 2 0 0 1 0 1 Double Rotation (Step #1) 10 10 5 15 5 15 12 12 2 9 20 2 9 17 3 17 30 3 20 0 0 0 18 Look familiar? 18 30 CSE 326 Autumn 2001 21
3 3 2 2 3 2 1 1 0 0 0 1 1 2 0 0 1 0 0 Double Rotation (Step #2) 10 10 5 15 5 17 12 15 2 9 17 2 9 20 0 12 18 3 20 3 30 0 0 18 30 CSE 326 Autumn 2001 22
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 AVL Insert Algorithm Revisited CSE 326 Autumn 2001 23
root temp X Y Z 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; } CSE 326 Autumn 2001 24
a a b Z c Z W Y c b X Y X W Double Rotation Code void DoubleRotateRight(Node *& root) { RotateLeft(root->right); RotateRight(root); } First Rotation CSE 326 Autumn 2001 25
a c Z Y b X W Double Rotation Completed Second Rotation First Rotation c b a X W Y Z CSE 326 Autumn 2001 26
3 2 2 1 0 0 1 0 0 0 Deletion: Really Easy Case Delete(17) 10 5 15 12 2 9 20 3 17 30 CSE 326 Autumn 2001 27
3 2 2 1 0 0 1 0 0 0 Deletion: Pretty Easy Case Delete(15) 10 5 15 12 2 9 20 3 17 30 CSE 326 Autumn 2001 28
Deletion: Pretty Easy Case (cont.) 3 Delete(15) 10 2 2 5 17 1 0 0 1 12 2 9 20 0 0 3 30 CSE 326 Autumn 2001 29
3 2 2 1 0 0 1 0 0 Deletion (Hard Case #1) Delete(12) 10 5 17 12 2 9 20 3 30 CSE 326 Autumn 2001 30
3 3 10 10 2 2 2 1 5 17 5 20 1 1 0 0 1 0 0 2 9 20 2 9 17 30 0 0 0 3 30 3 Single Rotation on Deletion Deletion can differ from insertion – How? CSE 326 Autumn 2001 31
4 2 3 1 0 2 2 0 Deletion (Hard Case) Delete(9) 10 5 17 12 12 2 9 20 20 0 1 0 1 3 30 30 11 15 15 18 0 0 0 0 0 33 33 13 13 CSE 326 Autumn 2001 32
4 4 2 1 3 3 0 1 0 2 2 2 2 Double Rotation on Deletion Not finished! 10 10 5 17 3 17 12 12 2 2 20 2 5 20 0 0 1 0 1 0 1 0 1 3 30 30 11 15 18 11 15 18 0 0 0 0 0 33 33 13 13 CSE 326 Autumn 2001 33
4 1 3 0 0 2 2 Deletion with Propagation 10 What different about this case? 3 17 12 2 5 20 We get to choose whether to single or double rotate! 0 1 0 1 30 11 15 18 0 0 33 13 CSE 326 Autumn 2001 34
4 1 3 0 0 2 2 Propagated Single Rotation 4 10 17 3 2 3 17 10 20 1 2 0 1 18 12 2 5 20 12 3 30 0 1 0 1 0 0 0 1 0 30 11 15 18 2 5 11 33 15 0 0 0 33 13 13 CSE 326 Autumn 2001 35
4 1 3 0 0 2 2 Propagated Double Rotation 4 10 12 2 3 3 17 10 17 1 0 1 2 15 20 12 2 5 20 11 3 0 1 0 1 0 1 0 0 0 30 18 30 11 15 18 2 5 13 0 0 0 33 33 13 CSE 326 Autumn 2001 36
Recursive Search downward for node Delete node Unwind, correcting heights as we go a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care), double rotate Iterative 1. Search downward for node, stacking parent nodes 2. Delete node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care) double rotate AVL Deletion Algorithm CSE 326 Autumn 2001 37
Building an AVL Tree Input: sequence of n keys (unordered) 19 3 4 18 7 Insert each into initially empty AVL tree But, suppose input is already sorted … 3 4 7 18 19 Can we do better than O(n log n)? CSE 326 Autumn 2001 38
Divide & Conquer Divide the problem into parts Solve each part recursively Merge the parts into a general solution 5 8 10 15 20 30 35 40 AVL BuildTree 5 8 10 15 17 20 30 35 40 17 How long does divide & conquer take? CSE 326 Autumn 2001 39
BuildTree Example 5 8 10 15 17 20 30 35 40 3 17 5 8 10 15 2 2 20 30 35 40 10 35 20 30 1 0 0 5 8 1 30 40 15 8 0 0 5 20 CSE 326 Autumn 2001 40
BuildTree Analysis(Approximate) T(1) = 1 T(n) = 2T(n/2) + 1 T(n) = 2(2T(n/4)+1) + 1 T(n) = 4T(n/4) + 2 + 1 T(n) = 4(2T(n/8)+1) + 2 + 1 T(n) = 8T(n/8) + 4 + 2 + 1 T(n) = 2kT(n/2k) + let 2k = n, log n = k T(n) = nT(1) + T(n) = (n) CSE 326 Autumn 2001 41
BuildTree Analysis (Exact) Precise Analysis: T(0) = b T(n) = T( ) + T( ) + c By induction on n: T(n) = (b+c)n + b Base case: T(0) = b = (b+c)0 + b Induction step: T(n) = (b+c) + b + (b+c) + b + c = (b+c)n + b QED: T(n) = (b+c)n + b = (n) CSE 326 Autumn 2001 42
Thinking About AVL Observations + Worst case height of an AVL tree is about 1.44 log n + Insert, Find, Delete in worst case O(log n) + Only one (single or double) rotation needed on insertion + Compatible with lazy deletion - O(log n) rotations needed on deletion - Height fields must be maintained (or 2-bit balance) Coding complexity? CSE 326 Autumn 2001 43
Alternatives to AVL Trees • Weight balanced trees • keep about the same number of nodes in each subtree • not nearly as nice • Splay trees • “blind” adjusting version of AVL trees • no height information maintained! • insert/find always rotates node to the root! • worst case time is O(n) • amortized time for all operations is O(log n) • mysterious, but often faster than AVL trees in practice (better low-order terms) CSE 326 Autumn 2001 44