Splay Trees: Introduction, Operations, and Benefits

slide1 n.w
1 / 15
Embed
Share

Explore the world of Splay Trees, including their operations, benefits, and how they optimize search efficiency by bringing recently accessed items to the top of the tree. Understand the principles of locality and the various cases involved in Splaying.

  • Splay Trees
  • Data Structures
  • Optimization
  • Locality Principle

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 1 CSCI 104 Splay Trees Mark Redekopp Aaron Cote

  2. 2 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 AVLTree 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 time is O(m*log(n)) [i.e. this is something called amortized analysis] 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

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

  4. 4 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

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

  6. 6 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

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

  8. 8 Worst Case Suppose you want to make the average runtime 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)

  9. 9 Insert(11) 20 20 20 30 30 30 12 12 12 15 15 15 25 25 25 5 5 11 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

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

  11. 11 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

  12. 12 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) Perform BST Remove(x), then splay the parent of the deleted node to the top.

  13. 13 FindMin() / DeleteMin() 3 FindMin() 20 20 20 3 30 30 12 5 30 5 25 15 25 5 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 25 12 25 12 15 10 15 10 8 Resulting Tree 8

  14. 14 Remove(3) 0 0 0 6 6 Zig-Zag 6 4 7 4 7 2 7 1 5 1 5 1 4 Zig-Zag 2 2 5 3 2 0 6 1 4 7 5

  15. 15 Summary Splay trees don't enforce balance but are self- adjusting to 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

More Related Content