Splay Trees – It Will Cover Everything Through Today, Maybe Part of Monday’S Lecture

Splay Trees – It Will Cover Everything Through Today, Maybe Part of Monday’S Lecture

Announcements (4/25/08) • No homework assigned this week CSE 326: Data Structures • Midterm next Friday – Closed notes, book Splay Trees – It will cover everything through today, maybe part of Monday’s lecture. – It will not be a test of your Java knowledge. Brian Curless – It will not have hard proofs. Spring 2008 – I will say more next week. • Reading for this lecture is in Weiss, Chapter 4. 2 Pros and Cons of AVL Trees Splay Trees Arguments for AVL trees: • Blind adjusting version of AVL trees – Why worry about balances? Just rotate anyway! 1. Search is O(log n) since AVL trees are always well balanced. 2. The height balancing adds no more than a constant factor to • Worst case time for an operation is O(n) the speed of insertion and deletion; thus both are O(log n). • Amortized time per operation is O(log n) Arguments against using AVL trees: 1. Must store and track height info. SAT/GRE Analogy question: 2. May be fine to have O(n) for a single operation if amortized cost can be O(log n) (e.g., Splay trees). 3. Very large searches are done in database systems and need AVL tree : Splay tree :: Leftist heap : efficient strategies based on disk access costs (e.g., B-trees). 3 4 Webster’s Dictionary Definition The Splay Tree Idea Main Entry: 10 3splay Function: If you’re forced to make 17 a really deep access: adjective Date: Since you’re down there anyway, 1548 fix up a lot of deep nodes! 1 : turned outward <splay knees> 5 2 : awkward, ungainly 2 9 3 5 6 Find/Insert in Splay Trees Do it all with AVL single rotations? Consider the ordered “list tree” at left. Now do find(1) and 1. Find or insert a node k splay it to the root with only AVL single rotations: 2. Percolate k to the root with rotations. Instead of “percolate k to the root with rotations” we 6 6 6 6 6 1 will often just say “splay k to the root”. 5 5 5 5 1 6 Why could this be good?? 4 4 4 1 5 5 3 3 1 4 4 4 1. Helps the new root, k o Great if k is accessed again soon 2 1 3 3 3 3 1 2 2 2 2 2 2. And helps many others on the path to k! o Great if others on the path are accessed soon 7 8 Do it all with AVL single rotations? Splay: Zig-Zag find(1) find(2) find(3) find(4)find(5) find(6) g k 1 2 3 4 6 6 p 6 1 6 2 6 3 6 4 5 5 p g W 5 5 1 5 2 5 3 4 4 4 4 1 2 3 k X X Y Z W 1 3 3 2 2 1 Y Z Cost of sequence: find(1), find(2), … find(n)? Relationship to AVL rotations? Single rotations can help, but they are not enough… 9 10 Splay: Zig-Zig Splay: Zig-Zig g k g k p p W X p p W X k g Z Y k g p Z Y X Y Z W k g X Y Z W X Y Z W 11 Relationship to AVL rotations? 12 Special Case for Root: Zig Splaying Example: Find(1) 6 root p k root 5 k p ? Z X 4 Find(1) 3 X Y Y Z 2 Relationship to AVL rotations? 1 13 14 Find(1) continued… Find(1) finished 6 6 5 1 ? ? 4 4 1 2 5 2 3 3 15 16 Another Splay: Find(2) Find(2) finished 1 1 6 2 ? ? 4 4 Find(2) 3 6 2 5 5 3 17 18 Another Splay: Find(3) A bigger, badder example Consider a “list tree” from 1-32. Doing find(1) gives: 2 ? 1 4 Find(3) 3 6 5 It took n operations to bring that node to the root, but the height of the tree 19 was cut almost in half! 20 A bigger, badder example A bigger, badder example Now do find(2): Now do find(3): Now it was n/2 operations The height is cut in ~half again. to splay the node…and the tree height is cut in ~half again! 21 22 A bigger, badder example Why Splaying Helps • If a node x on the access path is at depth d before the splay, it’s typically at about depth d/2 after the splay • Overall, nodes which are low on the access path tend to move closer to the root • Splaying gets amortized O(log n) performance. 23 24 Practical Benefit of Splaying Splay Operations: Find, Insert • No heights to maintain, no imbalance to check for Find(x): – Less storage per node, easier to code • Find the node in normal BST manner • Splay the node to the root • Data accessed once, is often soon accessed again – if node not found, splay what would have been its parent – Splaying does implicit caching by bringing it to the root Insert(x): • Insert the node in normal BST manner • Splay the node to the root 25 26 Splay Operations: Remove Join Join(L, R): given two trees such that (stuff in L) < (stuff in R), merge them: L k splay find(k) delete k findMax(L) L R R L R L R < k > k Splay on the maximum element in L, then attach R Now what? 27 28 Delete Example Delete(4) Splay Tree Summary 6 All operations are in amortized O(log n) time 1 9 find(4) Splaying can be done top-down; this may be better because: 4 7 – only one pass – no recursion or parent pointers necessary 2 – (we didn’t cover top-down in class) 3 Splay trees are very effective search trees – Relatively simple – No extra fields required – Excellent locality properties in the following sense: 29 frequently accessed keys are cheap to find 30.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us