Splay Trees are Binary Search Trees (20,Z) all the keys in the blue note that two keys of region are  20 equal value may be well- separated (10,A) (35,R) Splay Trees BST Rules:  entries stored only at (14,J) internal nodes (7,T) (21,O) (37,P) 6  keys stored at nodes in the v left subtree of v are less (1,Q) (8,N) (36,L) (40,X) 3 8 than or equal to the key z stored at v (1,) 4  keys stored at nodes in the (5,H) (7,P) (10,U) right subtree of v are greater than or equal to the all the keys in the yellow key stored at v (2,R) (5,G) region are  20 An inorder traversal will return the keys in order (5,I) (6,Y)

© 2004 Goodrich, Tamassia, Dickerson Splay Trees 1 © 2004 Goodrich, Tamassia, Dickerson Splay Trees 2 Searching in a Splay : Example Searching in a BST, Starts the Same as in a BST continued (20,Z) (20,Z) Search proceeds down search for key 8, ends at the tree to found item (10,A) (35,R) an internal node. (10,A) (35,R) or an external node. Example: Search for (14,J) (14,J) (7,T) (21,O) (37,P) (7,T) (21,O) (37,P) time with key 11. (1,Q) (8,N) (36,L) (40,X) (1,Q) (8,N) (36,L) (40,X)

(1,C) (5,H) (7,P) (10,U) (1,C) (5,H) (7,P) (10,U)

(2,R) (5,G) (2,R) (5,G)

(5,I) (6,Y) (5,I) (6,Y)

© 2004 Goodrich, Tamassia, Dickerson Splay Trees 3 © 2004 Goodrich, Tamassia, Dickerson Splay Trees 4  “x is a left-left grandchild” means x is a left child of its Splay Trees do Rotations after Splaying: parent, which is itself a left child of its parent start with  p is x’s parent; g is p’s parent Every Operation (Even Search) node x new operation: splay is x a left-left is x the  splaying moves a node to the root using rotations yes stop grandchild? zig-zig root?  right rotation  left rotation yes right-rotate about g,  makes the left child x of a node y into  makes the right child y of a node x no right-rotate about p y’s parent; y becomes the right child into x’s parent; x becomes the left is x a right-right of x child of y zig-zig is x a child of no grandchild? the root? y x yes left-rotate about g, a right rotation about y a left rotation about x left-rotate about p yes x y is x a right-left x y grandchild? zig-zag T T3 1 is x the left no left-rotate about p, child of the yes right-rotate about g y x root? T T T T 1 2 T 2 3 1 T3 is x a left-right yes zig zig grandchild? zig-zag (structure of tree above y (structure of tree above x right-rotate left-rotate about yes right-rotate about p, is not modified) T2 T3 is not modified) T1 T2 about the root the root left-rotate about g © 2004 Goodrich, Tamassia, Dickerson Splay Trees 5 © 2004 Goodrich, Tamassia, Dickerson Splay Trees 6 Visualizing the Splaying Example (20,Z) (10,A) (35,R) let x = (8,N) g (14,J) 1. Splaying Cases  x is the right child of its parent, p (7,T) (21,O) (37,P) zig-zag (before z x which is the left child of the z grandparent (1,Q) (8,N) (36,L) (40,X) rotating) z x y y  left-rotate around p, then right- (1,C) y rotate around g (5,H) (7,P) (10,U)

T4 x T1 (2,R) (5,G) x T T T T T4 1 2 3 4 (20,Z) (5,I) (6,Y) T3 (20,Z) g (10,A) (35,R) zig-zig T2 T3 T1 T2 x (8,N) (14,J) x (8,N) (35,R) y (21,O) (37,P) x zig g p (7,T) (10,U) (36,L) (40,X) p (7,T) (10,A) (21,O) (37,P) x y x (1,Q) (7,P) (1,Q) (7,P) (14,J) (36,L) (40,X) T1 T4 z w w y 2. (1,C) (5,H) (1,C) (5,H) (10,U) (after first rotation) 3. T T 2 3 (after second (2,R) (5,G) (2,R) (5,G) rotation) T1 T2 T3 T4 T3 T4 (6,Y) T1 T2 (5,I) (6,Y) (5,I) x is not yet the root, so we splay again © 2004 Goodrich, Tamassia, Dickerson Splay Trees 7 © 2004 Goodrich, Tamassia, Dickerson Splay Trees 8 Example Result (20,Z) Splaying Example, Continued (10,A) (35,R) before (14,J) of Splaying (7,T) (21,O) (37,P) tree might not be more balanced now x is the left child of the root (1,Q) (8,N) (36,L) (40,X) e.g. splay (40,X) (20,Z)  right-rotate around root  before, the depth of the shallowest leaf is(1,C) (5,H) (7,P) (10,U) 3 and the deepest is 7 x (8,N) (35,R)  after, the depth of shallowest leaf is 1 (2,R) (5,G) and deepest is 8 (40,X) (7,T) (10,A) (21,O) (37,P) (5,I) (6,Y) (20,Z) (20,Z) (1,Q) (7,P) (14,J) (36,L) (40,X) x (8,N) 2. (10,A) (37,P) (10,A) (40,X) (10,U) (1,C) (5,H) (7,T) (20,Z) (after rotation) (14,J) (35,R) 1. (14,J) (37,P) (7,T) (1,Q) (7,P) (7,T) (2,R) (5,G) (before applying (10,A) (35,R) (1,Q) (8,N) rotation) (1,Q) (8,N) (35,R) (21,O) (36,L) (5,I) (6,Y) (1,C) (5,H) (14,J) (21,O) (37,P) (1,C) (5,H) (7,P) (10,U) (1,C) (5,H) (7,P) (10,U) (21,O) (36,L) (10,U) (36,L) (40,X) (2,R) (5,G)

(2,R) (5,G) (5,I) (6,Y) (2,R) (5,G) after first splay after second (5,I) (6,Y) splay x is the root, so stop (5,I) (6,Y) © 2004 Goodrich, Tamassia, Dickerson Splay Trees 9 © 2004 Goodrich, Tamassia, Dickerson Splay Trees 10 Splay Trees & Ordered Definition Dictionaries which nodes are splayed after each operation? a splay tree is a binary where a node is splayed after it is accessed (for a method splay node search or update) if key found, use that node get(k)  deepest internal node accessed is splayed if key not found, use parent of ending external node  splaying costs O(h), where h is height of the tree – which is still O(n) worst-case put(k,v) use the new node containing the entry inserted  O(h) rotations, each of which is O(1) use the parent of the internal node that was actually removed from the tree (the parent of the node that the remove(k) removed item was swapped with)

© 2004 Goodrich, Tamassia, Dickerson Splay Trees 11 © 2004 Goodrich, Tamassia, Dickerson Splay Trees 12 of Splay Trees Cost per zig

Running time of each operation is proportional to time y zig

for splaying. x x T Define rank(v) as the logarithm (base 2) of the number 4 w w y of nodes in subtree rooted at v. T3 Costs: zig = $1, zig-zig = $2, zig-zag = $2. T1 T2 T3 T4 T T Thus, cost for playing a node at depth d = $d. 1 2 Imagine that we store rank(v) cyber-dollars at each node v of the splay tree (just for the sake of analysis). Doing a zig at x costs at most rank’(x) - rank(x):  cost = rank’(x) + rank’(y) - rank(y) - rank(x) < rank’(x) - rank(x).

© 2004 Goodrich, Tamassia, Dickerson Splay Trees 13 © 2004 Goodrich, Tamassia, Dickerson Splay Trees 14

Cost per zig-zig and zig-zag Cost of Splaying z x

y zig-zig y Cost of splaying a node x at depth d of a tree T 4 T1 x z rooted at r: T3 T2

T1 T2  at most 3(rank(r) - rank(x)) - d + 2: T3 T4  Proof: Splaying x takes d/2 splaying substeps: Doing a zig-zig or zig-zag at x costs at most 3(rank’(x) - rank(x)) - 2 d / 2 cost  cost i i1 d / 2 z zig-zag x  (3(ranki (x)  ranki1(x))  2)  2 y z y i1

T x 1  3(rank(r)  rank 0 (x))  2(d / d)  2 T4 T1 T2 T3 T4

T2 T3  3(rank(r)  rank(x))  d  2.

© 2004 Goodrich, Tamassia, Dickerson Splay Trees 15 © 2004 Goodrich, Tamassia, Dickerson Splay Trees 16 Performance of Splay Trees

Recall: rank of a node is logarithm of its size. Thus, amortized cost of any splay operation is O(log n) In fact, the analysis goes through for any reasonable definition of rank(x) This implies that splay trees can actually adapt to perform searches on frequently- requested items much faster than O(log n) in some cases

© 2004 Goodrich, Tamassia, Dickerson Splay Trees 17