cse 326 data structures
play

CSE 326: Data Structures For every node x , -1 balance( x ) 1 - PowerPoint PPT Presentation

AVL Trees Revisited Balance condition: CSE 326: Data Structures For every node x , -1 balance( x ) 1 Strong enough : Worst case depth is O(log n ) Splay Trees Easy to maintain : one single or double rotation Guaranteed


  1. AVL Trees Revisited • Balance condition: CSE 326: Data Structures For every node x , -1 ≤ balance( x ) ≤ 1 – Strong enough : Worst case depth is O(log n ) Splay Trees – Easy to maintain : one single or double rotation • Guaranteed O(log n ) running time for Hal Perkins – Find ? Spring 2007 – Insert ? Lecture 13 – Delete ? – buildTree ? 2 AVL Trees Revisited Other Possibilities? • Could use different balance conditions, different ways to • What extra info did we maintain in each node? maintain balance, different guarantees on running time, … • Why aren’t AVL trees perfect? • Where were rotations performed? • Many other balanced BST data structures – Red-Black trees • How did we locate this node? – AA trees – Splay Trees – 2-3 Trees – B-Trees – … 3 4 1

  2. Splay Trees Recall: Amortized Complexity • Blind adjusting version of AVL trees If a sequence of M operations takes O(M f( n )) time, – Why worry about balances? Just rotate anyway! we say the amortized runtime is O(f( n )). • Amortized time per operations is O(log n ) • Worst case time per operation is O( n ) • Worst case time per operation can still be large, say O( n ) – But guaranteed to happen rarely • Worst case time for any sequence of M operations is O(M f( n )) Insert/Find always rotate node to the root ! Average time per operation for any sequence is O(f( n )) SAT/GRE Analogy question: Amortized complexity is worst-case guarantee over AVL is to Splay trees as ___________ is to __________ sequences of operations. 5 6 The Splay Tree Idea Recall: Amortized Complexity • Is amortized guarantee any weaker than worstcase? 10 If you’re forced to make 17 • Is amortized guarantee any stronger than averagecase? a really deep access: Since you’re down there anyway, • Is average case guarantee good enough in practice? fix up a lot of deep nodes! • Is amortized guarantee good enough in practice? 5 2 9 3 7 8 2

  3. Splaying node k to the root: Find/Insert in Splay Trees Need to be careful! 1. Find or insert a node k One option (that we won’t use) is to repeatedly use AVL single rotation until k becomes the root: (see Section 4.5.1 for details) 2. Splay k to the root using: zig-zag, zig-zig, or plain old zig rotation p k q F Why could this be good?? r s p E 1. Helps the new root, k s q D A B F o Great if k is accessed again k r A E 2. And helps many others! o Great if many others on the path are accessed B C C D 9 10 Splaying node k to the root: Splay: Zig-Zag * Need to be careful! What’s bad about this process? k g p g p X k p k q W X Y Z W F s p r E q s A B F Y Z D r k A E * Just like an… Which nodes improve depth? B C C D 11 12 3

  4. Splay: Zig-Zig * Special Case for Root: Zig g k root root k p p p W Z k p Z X k g X Y X Y Y Z Y Z W X Relative depth of p , Y, Z? Relative depth of everyone else? *Is this just two AVL single rotations in a row? Why not drop zig-zig and just zig all the way? 13 14 Splaying Example: Find(6) Still Splaying 6 1 1 1 1 2 2 2 6 ? ? 3 3 3 3 Find( 6 ) 4 6 6 2 5 5 5 5 4 6 4 15 4 16 4

  5. Finally… Another Splay: Find(4) 1 6 6 6 6 1 1 1 ? ? 3 3 3 4 Find( 4 ) 2 5 2 5 2 5 3 5 4 4 4 2 17 18 Example Splayed Out But Wait… What happened here? 6 4 Didn’t two find operations take linear time 1 6 1 instead of logarithmic? ? 3 5 4 What about the amortized O(log n ) guarantee? 2 3 5 2 19 20 5

  6. Why Splaying Helps Practical Benefit of Splaying • If a node n on the access path is at depth d before • No heights to maintain, no imbalance to check for the splay, it’s at about depth d/2 after the splay – Less storage per node, easier to code • Often data that is accessed once, • Overall, nodes which are low on the access path is soon accessed again! tend to move closer to the root – Splaying does implicit caching by bringing it to the root • Splaying gets amortized O(log n) performance. (Maybe not now, but soon, and for the rest of the operations.) 21 22 Splay Operations: Find Splay Operations: Insert • Find the node in normal BST manner • Insert the node in normal BST manner • Splay the node to the root • Splay the node to the root – if node not found, splay what would have been its parent What if we didn’t splay? What if we didn’t splay? 23 24 6

  7. Splay Operations: Remove Join Join(L, R): given two trees such that (stuff in L) < (stuff in R), merge them: L splay k max find( k ) delete k L R R L R L R < k > k Splay on the maximum element in L, then attach R Now what? Does this work to join any two trees? 25 26 Delete Example Splay Tree Summary Delete( 4 ) 6 4 6 • All operations are in amortized O(log n ) time 1 9 1 6 1 9 find( 4 ) • Splaying can be done top-down; this may be better because: – only one pass 9 4 7 2 2 7 – no recursion or parent pointers necessary Find max – we didn’t cover top-down in class 7 2 2 2 • Splay trees are very effective search trees – Relatively simple 1 6 1 6 – No extra fields required 9 9 – Excellent locality properties: frequently accessed keys are cheap to find 7 7 27 28 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend