Download presentation
Presentation is loading. Please wait.
Published byMariah Boyd Modified over 9 years ago
1
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000
2
Today’s Outline Extra operations in AVL trees Splaying and Splay Trees
3
AVL Tree Dictionary Data Structure 4 121062 115 8 141379 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
4
Deletion (Hard Case #1) 2092 175 10 303 12 1 100 22 3 0 0 Delete(12)
5
Single Rotation on Deletion 2092 175 10 303 1 10 22 3 0 0 92 205 10 17 3 1 00 21 3 0 0 Something very bad happened!
6
Deletion (Hard Case #2-4) Delete(9) 2092 175 10 303 12 1 220 23 4 0 33 15 13 00 1 0 20 30 12 33 15 13 1 00 11 0 18 0
7
Double Rotation on Deletion 2 3 0 202 173 10 30 12 1 22 23 4 33 15 13 1 00 1 11 0 18 0 2052 173 10 30 12 0 220 13 4 33 15 13 1 00 1 11 0 18 00
8
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 11. 2052 173 10 30 12 0 220 13 4 33 15 13 1 00 1 11 0 18 0
9
Propagated Double Rotation 0 17 12 11 52 3 10 4 23 10 0 0 2052 173 10 30 12 0 220 13 4 33 15 13 1 0 1 11 0 18 0 15 1 0 20 30 33 1 18 0 13 0 2
10
Propagated Single Rotation 0 30 20 17 33 12 15 13 1 0 52 3 10 4 32 121 000 11 0 2052 173 10 30 12 0 220 13 4 33 15 13 1 0 1 11 0 18 0 0
11
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.
12
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
13
AVL buildTree 81015203035405 17 81015520303540 Divide & Conquer –Divide the problem into parts –Solve each part recursively –Merge the parts into a general solution How long does divide & conquer take?
14
BuildTree Example 35 17 15 5 8 10 3 22 10 0 30 1 40 20 0 0 8101520303540517 810155 85 30354020 3020
15
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) = 2 k T(n/2 k ) + let 2 k = n, log n = k T(n) = nT(1) + T(n) = (n)
16
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)
17
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
18
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!
19
Idea 17 10 92 5 3 You’re forced to make a really deep access: Since you’re down there anyway, fix up a lot of deep nodes!
20
Zig-Zig * * I told you it was a technical term! n Z Y p X g W g W X p Y n Z
21
Zig-Zag * g X p Y n Z W * This is just a double rotation n Y g W p ZX
22
Zig p X n Y Z n Z p Y X root
23
Splaying Example 2 1 3 4 5 6 Find(6) 2 1 3 6 5 4 zig-zig
24
Still Splaying 6 zig-zig 2 1 3 6 5 4 1 6 3 25 4
25
Almost There, Stay on Target * zig 1 6 3 25 4 6 1 3 25 4
26
Splay Again Find(4) zig-zag 6 1 3 25 4 6 1 4 35 2
27
Example Splayed Out zig-zag 6 1 4 35 2 61 4 35 2
28
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
29
Coming Up Polish off Splay Trees Talk about Project III Second project due (today) Midterm (Friday)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.