CSCI 104 Splay Trees Mark Redekopp.

Slides:



Advertisements
Similar presentations
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
Advertisements

5.9 Heaps of optimal complexity
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Splay Trees and B-Trees
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
CSE 326: Data Structures Trees
COMP261 Lecture 23 B Trees.
COMP 53 – Week Fourteen Trees.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
BCA-II Data Structure Using C
CSCE 3110 Data Structures & Algorithm Analysis
Week 7 - Friday CS221.
Self-Adjusting Search trees
Multiway Search Trees Data may not fit into main memory
BST Trees
COSC160: Data Structures Binary Trees
Topics covered (since exam 1):
Splay Trees Binary search trees.
Binary Search Trees.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Data Structures and Analysis (COMP 410)
Splay Trees.
SPLAY TREE Features Binary Search Tree Self adjusting balanced tree
B+-Trees.
B+-Trees.
CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Hashing Exercises.
Source: Muangsin / Weiss
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
Splay Trees Binary search trees.
Red Black Trees.
Lecture 25 Splay Tree Chapter 10 of textbook
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Topics covered (since exam 1):
Topics covered (since exam 1):
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Tree Rotations & Splay Trees
Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete.
SPLAY TREES.
Tree Rotations and AVL Trees
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
CMSC 341 Splay Trees.
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
Topics covered (since exam 1, excluding PQ):
CMSC 341 Splay Trees.
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
CSCI 104 Skip Lists Mark Redekopp.
Basic Data Structures - Trees
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CMSC 341 Splay Trees.
Topics covered (since exam 1):
B-Trees.
B-Trees.
CMSC 341 Splay Trees.
CSE 373 Data Structures Lecture 12
1 Lecture 13 CS2013.
Heaps & Multi-way Search Trees
326 Lecture 9 Henry Kautz Winter Quarter 2002
Splay Trees Binary search trees.
Topic 10 Trees.
More on Randomized Data Structures
Presentation transcript:

CSCI 104 Splay Trees Mark Redekopp

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

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

If we search for or insert T… 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

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

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

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

Find(3) Notice the tree is starting to look at lot more balanced 1 1 3 6 Zig-Zag 6 1 6 4 7 3 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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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