CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000
Today’s Outline Extra operations in AVL trees Splaying and Splay Trees
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
Deletion (Hard Case #1) Delete(12)
Single Rotation on Deletion Something very bad happened!
Deletion (Hard Case #2-4) Delete(9)
Double Rotation on Deletion
Deletion with Propagation: Choose Your Own Adventure! We get to choose whether to single or double rotate! If you take the gold and single rotate, flip to Slide 10. If you decide to search further into the scary cavern, continue on to the next slide. If you decide not to rotate at all, jump to Slide
Propagated Double Rotation
Propagated Single Rotation
You Didn’t Rotate Casually walking away from the AVL tree, minding your own business, you fall into a sewer and are eaten by wild Red-Black trees (some people say they grow as big as B-Trees in the sewers!). You die.
AVL Deletion Algorithm Recursive 1. Search downward for node 2. Delete node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, 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, double rotate
AVL buildTree Divide & Conquer –Divide the problem into parts –Solve each part recursively –Merge the parts into a general solution How long does divide & conquer take?
BuildTree Example
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) T(n) = 4(2T(n/8)+1) T(n) = 8T(n/8) T(n) = 2 k T(n/2 k ) + let 2 k = n, log n = k T(n) = nT(1) + T(n) = (n)
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)
Thinking About AVL Observations +Worst case height of an AVL tree is about 1.44 log n +All operations supported in worst case O(log n) +Only one (single or double) rotation needed on insertion –O(log n) rotations needed on deletion –Height fields must be maintained (or 2-bit balance) ?Coding complexity
Splay Trees Problems with AVL Trees –extra storage/complexity for height fields –ugly delete code Solution: splay trees –blind adjusting version of AVL trees –amortized time for all operations is O(log n) –worst case time is O(n) –insert/find always rotates node to the root!
Idea You’re forced to make a really deep access: Since you’re down there anyway, fix up a lot of deep nodes!
Zig-Zig * * I told you it was a technical term! n Z Y p X g W g W X p Y n Z
Zig-Zag * g X p Y n Z W * This is just a double rotation n Y g W p ZX
Zig p X n Y Z n Z p Y X root
Splaying Example Find(6) zig-zig
Still Splaying 6 zig-zig
Almost There, Stay on Target * zig
Splay Again Find(4) zig-zag
Example Splayed Out zig-zag
To Do Turn in Project II Read chapter 4 in the book Do the new worksheet Prepare for the midterm Finish HW#4 –Just kidding
Coming Up Polish off Splay Trees Talk about Project III Second project due (today) Midterm (Friday)