splay trees
play

Splay Trees Mark Redekopp 2 Sources / Reading Material for these - PowerPoint PPT Presentation

1 CSCI 104 Splay Trees Mark Redekopp 2 Sources / Reading Material for these slides was derived from the following sources https://www.cs.cmu.edu/~sleator/papers/self- adjusting.pdf


  1. 1 CSCI 104 Splay Trees Mark Redekopp

  2. 2 Sources / Reading • Material for these slides was derived from the following sources – https://www.cs.cmu.edu/~sleator/papers/self- adjusting.pdf – http://digital.cs.usu.edu/~allan/DS/Notes/Ch22.pdf – http://www.cs.umd.edu/~meesh/420/Notes/MountNotes /lecture10-splay.pdf • Nice Visualization Tool – https://www.cs.usfca.edu/~galles/visualization/SplayTree. html

  3. 3 Splay Tree Intro • Another map/set implementation (storing keys or key/value pairs) – Insert, Remove, Find • Recall…To do m inserts/finds/removes on an RBTree w/ n elements would cost? – O(m*log(n)) • Splay trees have a worst case find, insert, delete time of… – O( n ) • However, they guarantee that if you do m operations on a splay tree with n elements that the total ("amortized"…uh -oh) time is – O( m * log(n) ) • They have a further benefit that recently accessed elements will be near the top of the tree – In fact, the most recently accessed item is always at the top of the tree

  4. 4 Splay Operation R • Splay means "spread" • As you search for an item or after you insert an item we will perform a T series of splay operations If we search for or insert T… • These operations will cause the desired node to always end up at the T top of the tree R – A desirable side-effect is that accessing a key multiple times within a short time window will yield fast searches because …T will end up as the it will be near the top root node with the old – See next slide on principle of locality root in the top level or two

  5. 5 Principle of Locality • 2 dimensions of this principle: space & time • Spatial Locality – Future accesses will likely cluster near current accesses – Instructions and data arrays are sequential (they are all one after the next) • Temporal Locality – Future accesses will likely be to recently accessed items – Same code and data are repeatedly accessed (loops, subroutines, if(x > y) x++; – 90/10 rule: Analysis shows that usually 10% of the written instructions account for 90% of the executed instructions • Splay trees help exploit temporal locality by guaranteeing recently accessed items near the top of the tree

  6. 6 Splay Cases 1. 3. Root/Zig Case G X (Single Rotation) Zig-Zig P a P d R R c X b G a X c X a b c d c b b a Leftrotate of X,R Right rotate of X,R 2. Zig-Zag G X G X 2 2 a P d P P G G P 1 1 a a b c d a b c d X X d b c b c

  7. 7 Find(1) 6 6 Zig 6 5 5 7 7 1 7 4 4 4 Zig-Zig 1 3 2 5 2 2 Zig-Zig 3 1 3 1 6 Resulting 4 7 Tree 2 5 3

  8. 8 Find(3) 1 1 3 Zig-Zag 6 6 1 6 4 3 7 7 2 4 7 2 5 2 4 5 Zig-Zag 3 5 Resulting Tree • Notice the tree is starting to look at lot more balanced

  9. 9 Worst Case • Suppose you want to make the amortized time (averaged time over multiple calls to find/insert/remove) look bad, you might try to always access the ______________ node in the tree – Deepest • But splay trees have a property that as we keep accessing deep nodes the tree starts to balance and thus access to deep nodes start by costing O(n) but soon start costing O(log n)

  10. 10 Insert(11) 20 20 20 30 30 30 12 12 12 15 15 15 5 25 5 25 11 25 3 3 10 10 10 5 8 8 11 3 8 Zig-Zig Zig-Zig 11 12 10 5 20 3 8 15 30 25 Resulting Tree

  11. 11 Insert(4) 20 20 20 30 12 30 30 12 12 15 4 25 15 15 5 25 5 25 3 5 3 3 10 10 10 8 4 8 8 Zig-Zag Zig-Zig 4 3 12 20 5 10 15 30 Resulting Tree 8 25

  12. 12 Activity • Go to – https://www.cs.usfca.edu/~galles/visualization/SplayTree. html – Try to be an adversary by inserting and finding elements that would cause O(n) each time

  13. 13 Splay Tree Supported Operations • Insert(x) – Normal BST insert, then splay x • Find(x) – Attempt normal BST find(x) and splay last node visited • If x is in the tree, then we splay x • If x is not in the tree we splay the leaf node where our search ended • FindMin(), FindMax() – Walk to far left or right of tree, return that node's value and then splay that node • DeleteMin(), DeleteMax() – Perform FindMin(), FindMax() [which splays the min/max to the root] then delete that node and set root to be the non-NULL child of the min/max • Remove(x) – Find(x) splaying it to the top, then overwrite its value with is successor/predecessor, deleting the successor/predecessor node

  14. 14 FindMin() / DeleteMin() 3 FindMin() 20 20 20 3 30 30 12 5 30 5 25 15 5 25 25 12 12 3 10 15 10 15 10 8 8 8 Zig-Zig Zig Resulting Tree DeleteMin() 3 20 20 5 30 5 30 12 25 12 25 15 10 15 10 8 Resulting Tree 8

  15. 15 Remove(3) 1 1 3 Zig-Zag 6 6 1 6 4 3 7 7 2 4 7 2 5 2 4 5 Zig-Zag 3 5 4 3 Resulting 1 1 6 6 Tree 2 2 5 4 7 7 5 Delete successor Copy successor or (Remove node or predecessor to root reattach single child)

  16. 16 Top Down Splaying • Rather than walking down the tree to first find the value then splaying back up, we can splay on the way down • We will be "pruning" the big tree into two smaller trees as we walk, cutting off the unused pathways

  17. 17 Top-Down Splaying 1. Zig (If Target is in 2 nd level) T Root a T b R L a R L Root b 2. Final Step (when T reach Target) T a b L R L R a b

  18. 18 Top-Down Splaying 3. Zig-Zig Z X X Z R c L a Y a Y c L R R Y Y L R L b Z b Z X X a c c b a b 4. Zig-Zag X X Z Z a Y Y c R R L L b b L L R R c Z a Z X Y Y X b b a c a c

  19. 19 Find(3) L-Tree R-Tree L-Tree R-Tree R-Tree L-Tree - - 1 4 6 1 3 6 1 Steps taken on 2 5 6 our journey to 7 2 4 7 find 3 4 3 7 5 2 5 3 Zig-Zag X X Z Z a Y Y c R R L L b b L L R R c Z a Z X Y Y X b b a c a c

  20. 20 Find(3) L-Tree R-Tree 3 3 3 1 6 1 6 6 1 2 4 7 2 4 7 2 7 4 5 5 5 Resulting tree after find Resulting tree from bottom-up approach 2. Final Step (when T reach Target) T a b L R L R a b

  21. 21 Insert(11) L-Tree R-Tree L-Tree R-Tree - - - 5 12 20 3 20 10 30 12 8 15 30 15 5 25 25 3 10 8 L-Tree R-Tree 11 12 10 11 5 20 10 12 3 8 15 30 5 20 3 8 15 30 25 25 Original Resulting Tree from Bottom- up approach

  22. 22 Summary • Splay trees don't enforce balance but are self- adjusting to attempt yield a balanced tree • Splay trees provide efficient amortized time operations – A single operation may take O(n) – m operations on tree with n elements => O(m(log n)) • Uses rotations to attempt balance • Provides fast access to recently used keys

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