Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Splay Tree and the Dynamic Tree Problem

Splay Tree and the Dynamic Tree Problem

A summary presentation of the remarkable paper
"Self Adjusting Binary Search Tree" (D. Sleator, R. Tarjan, 1985).

Note: Animation available if it is seen offline with Adobe Reader

Jongwook Choi

June 07, 2011
Tweet

More Decks by Jongwook Choi

Other Decks in Technology

Transcript

  1. Introduction Operation Time Analysis Applications Conclusion Splay Tree and the

    Dynamic Tree Problem Self Adjusting Binary Search Tree (D. Sleator, R. Tarjan, 1985) Jongwook Choi and Taihyun Hwang Seoul National University 4541.561 Advanced Computation Theory Term Project June 7, 2011
  2. Introduction Operation Time Analysis Applications Conclusion Splay Tree : Structure

    Splay Tree is a simple BST with Splay operation. The Splay operation is a kind of restructuring heuristic, applying tree rotations to move the last-accessed node to the root. Due to the Splay operation, costs of operations are reduced to O (log n ) . Furthermore, it performs especially well on special access patterns.
  3. Introduction Operation Time Analysis Applications Conclusion Supported Operations In the

    following, T, T0 are splay trees and x are nodes. Splay( T, x ) move the node x of T to the root. Find( T, k ) find the node x with key k. Insert( T, k ) insert a new node with key k into T. Delete( T, x ) delete the node x of T.
  4. Introduction Operation Time Analysis Applications Conclusion Supported Operations In the

    following, T, T0 are splay trees and x are nodes. Splay( T, x ) move the node x of T to the root. Find( T, k ) find the node x with key k. Insert( T, k ) insert a new node with key k into T. Delete( T, x ) delete the node x of T. Split( T, k ) Join( T 1 , T 2) . . . All the cost of above operations are O (log n ) amortized, O ( n ) worst.
  5. Introduction Operation Time Analysis Applications Conclusion Notations and Definitions root

    ( T ) the root node of T Nil the null node key ( x ) the key value of node x Tx the subtree rooted at node x x 2 T x is a node contained in T p ( x ) the parent node of x left ( x ) the left children node of x (if any) right ( x ) the right children node of x (if any) x the predecessor of x x+ the successor of x Min( T ) the node with minimum key in T Max( T ) the node with maximum key in T h ( T ) the height of tree T
  6. Introduction Operation Time Analysis Applications Conclusion The Splay Operation Splay(

    T, x ) : moves the node x to the root of T. If x = root ( T ) , nothing to do. We will denote x’s parent by y and x’s grandparent by z. Figure: Splay (T, a)
  7. Introduction Operation Time Analysis Applications Conclusion Case A : Zig

    Step (Zig) : The basis step, where y = root ( T ) A trivial tree rotation between x and y. 1 Rotate the edge ( x, y ) . Figure: Zig
  8. Introduction Operation Time Analysis Applications Conclusion Case B : Zig-zig

    Step (Zig-zig) : if x and y are either both left or both right childrens 1 Let z be the parent of y. 2 Rotate the edge ( y, z ) , making y a root. 3 Rotate the edge ( x, y ) , making x a root. Figure: Zig-zig
  9. Introduction Operation Time Analysis Applications Conclusion Case C : Zig-zag

    Step (Zig-zag) : if x is a right child and y is a left child, or vice versa 1 Let z be the parent of y. 2 Rotate the edge ( x, y ) , making x a child of z. 3 Rotate the edge ( x, z ) , making x a root. Figure: Zig-zag
  10. Introduction Operation Time Analysis Applications Conclusion Find Find( T, k

    ) : Find the node in T with key value k. 1 Find the node x with key k, as with traditional BST. 2 If x is found, Splay( T, x ) and return x. 3 If no such key, splay at the last non- Nil node, and return Nil . Cost : O ( h ) . Unfortunately, the worst case is O ( n ) . Figure: Find (T, 20 ) Figure: Find (T, 80 )
  11. Introduction Operation Time Analysis Applications Conclusion Split Split( T, k

    ) : Split the tree T and return two trees T 1 , T 2 such that key ( x )  k for all x in T 1 and key ( y ) > k for all y in T 1 . 1 Find( T, k ) . Then the root becomes a successor, a precedessor, or of key k. 2 If root ( T )  k, break the right child and return two trees. 3 If root ( T ) > k, break the left child and return two trees. Cost : O ( h ) . Figure: Split (T, 40 )
  12. Introduction Operation Time Analysis Applications Conclusion Join Join( T 1

    , T 2) : Merge T 1 and T 2 to obtain a tree T. Assumed key ( x ) < key ( y ) for all x 2 T 1 , y 2 T 2 . 1 Splay(Max( T 1)) . 2 Now the root of T 1 has only one (left) child. Let the root ( T 2) be the right child of root ( T 1) . 3 Or, alternatively, splay Min( T 2) and concatenate. Cost : O (min {h ( T 1) , h ( T 2) } ) . The worst case is linear. Figure: Join (T1 , T2 )
  13. Introduction Operation Time Analysis Applications Conclusion Insert (Using Split) Insert(

    T, k ) : insert a new (single) node with key k into T. 1 Do Split( T, k ) . Let T 1 , T 2 be the returned trees. 2 Replace T by a new node x with key k, letting T 1 and T 2 be its two subtree. (Handle separately the case key ( root ( T 1)) = key ( x ) ) Figure: Insert (T, 45 )
  14. Introduction Operation Time Analysis Applications Conclusion Insert (Alternative) Insert( T,

    x ) : insert a new node x into T. 1 Insert x as with traditional BST. 2 Do Splay( T, x ) . Figure: Insert 0(T, 80 )
  15. Introduction Operation Time Analysis Applications Conclusion Delete (Using Join) Delete(

    T, x ) : delete the node x from T. 1 Do Splay( T, x ) . Now x = root ( T ) . 2 If x has less than two children, we are done. 3 If there are two children, let T 1 , T 2 be the two subtrees. 4 Do Join( T 1 , T 2) . Figure: Delete (T, 40 )
  16. Introduction Operation Time Analysis Applications Conclusion Delete (Alternative) Delete( T,

    x ) : delete the node x from T. 1 If x has less than two children: we can easily delete x. 2 If x has two children: join x’s two child subtrees, and replace x by the resulting joined tree. Figure: Delete 0(T, 30 )
  17. Introduction Operation Time Analysis Applications Conclusion A Potential Analysis Model

    weight w ( x ) : a fixed value assigned for each node assigned by theorem-provers: NOT a property of tree
  18. Introduction Operation Time Analysis Applications Conclusion A Potential Analysis Model

    weight w ( x ) : a fixed value assigned for each node assigned by theorem-provers: NOT a property of tree size s ( x ) : the sum of all weights in the subtree Tx rooted at x that is, s ( x ) = X i 2 Tx w ( i )
  19. Introduction Operation Time Analysis Applications Conclusion A Potential Analysis Model

    weight w ( x ) : a fixed value assigned for each node assigned by theorem-provers: NOT a property of tree size s ( x ) : the sum of all weights in the subtree Tx rooted at x that is, s ( x ) = X i 2 Tx w ( i ) rank r ( x ) := log s ( x )
  20. Introduction Operation Time Analysis Applications Conclusion A Potential Analysis Model

    weight w ( x ) : a fixed value assigned for each node assigned by theorem-provers: NOT a property of tree size s ( x ) : the sum of all weights in the subtree Tx rooted at x that is, s ( x ) = X i 2 Tx w ( i ) rank r ( x ) := log s ( x ) potential ( T ) : the sum of all ranks in T that is, ( T ) = X i 2 T r ( i ) Note that the amortized cost is (actual cost) +
  21. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma Lemma (Access Lemma) Let t = root ( T ) . The amortized cost ↵ of Splay( T, x ) is at most 3( r ( t ) r ( x )) + 1 = O ✓ log s ( t ) s ( x ) ◆ .
  22. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma Lemma (Access Lemma) Let t = root ( T ) . The amortized cost ↵ of Splay( T, x ) is at most 3( r ( t ) r ( x )) + 1 = O ✓ log s ( t ) s ( x ) ◆ . Proof . For each rotation operation, let s and t (s0 and t0) denote the size and rank functions just before (just after) the operation, respectively.
  23. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma Lemma (Access Lemma) Let t = root ( T ) . The amortized cost ↵ of Splay( T, x ) is at most 3( r ( t ) r ( x )) + 1 = O ✓ log s ( t ) s ( x ) ◆ . Proof . For each rotation operation, let s and t (s0 and t0) denote the size and rank functions just before (just after) the operation, respectively. We will show: (Zig Step) ↵  3( r0 ( x ) r ( x )) + 1 (Zig-zig, Zig-zag Step) ↵  3( r0 ( x ) r ( x )) .
  24. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1
  25. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . x y A B C x y A B C
  26. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . The actual cost is 1 (only one rotation). Therefore ↵ = 1 + 0 ( T ) ( T )
  27. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . The actual cost is 1 (only one rotation). Therefore ↵ = 1 + 0 ( T ) ( T ) = 1 + r0 ( x ) + r0 ( y ) r ( x ) r ( y )
  28. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . The actual cost is 1 (only one rotation). Therefore ↵ = 1 + 0 ( T ) ( T ) = 1 + r0 ( x ) + r0 ( y ) r ( x ) r ( y )  1 + r0 ( x ) r ( x )
  29. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . The actual cost is 1 (only one rotation). Therefore ↵ = 1 + 0 ( T ) ( T ) = 1 + r0 ( x ) + r0 ( y ) r ( x ) r ( y )  1 + r0 ( x ) r ( x )  1 + 3( r0 ( x ) r ( x )) .
  30. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x ))
  31. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) x y z A B C D x y z A B C D
  32. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) There are 2 tree rotations. ↵ = 2 + 0 ( T ) ( T )
  33. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) There are 2 tree rotations. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z )
  34. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) There are 2 tree rotations. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z ) = 2 + r0 ( y ) + r0 ( z ) r ( x ) r ( y )
  35. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) There are 2 tree rotations. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z ) = 2 + r0 ( y ) + r0 ( z ) r ( x ) r ( y )  2 + r0 ( x ) + r0 ( z ) 2 r ( x )
  36. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x ))
  37. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Since log ↵ + log  2 whenever ↵ +  1 , r ( x ) + r0 ( z ) 2 r0 ( x ) = ( r ( x ) r0 ( x )) + ( r0 ( z ) r0 ( x )) = log ✓ s ( x ) s0 ( x ) ◆ + log ✓ s0 ( z ) s0 ( x ) ◆  2 . Note that s ( x ) + s0 ( z )  s0 ( x ) .
  38. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Since log ↵ + log  2 whenever ↵ +  1 , r ( x ) + r0 ( z ) 2 r0 ( x ) = ( r ( x ) r0 ( x )) + ( r0 ( z ) r0 ( x )) = log ✓ s ( x ) s0 ( x ) ◆ + log ✓ s0 ( z ) s0 ( x ) ◆  2 . Note that s ( x ) + s0 ( z )  s0 ( x ) . Therefore ↵  2 + r0 ( x ) + r0 ( z ) 2 r ( x )  ( r ( x ) r0 ( z ) + 2 r0 ( x )) + ( r0 ( x ) + r0 ( z ) 2 r ( x ))  3( r0 ( x ) r ( x )) .
  39. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zag Step) Claim : amortized cost  3( r0 ( x ) r ( x ))
  40. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zag Step) Claim : amortized cost  3( r0 ( x ) r ( x )) We observe that r0 ( x ) = r ( z ) and r ( x )  r ( y ) . x y z A B C D x y z A B C D
  41. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zag Step) Claim : amortized cost  3( r0 ( x ) r ( x )) We observe that r0 ( x ) = r ( z ) and r ( x )  r ( y ) . Also 2 tree rotation is needed. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z )  2 + r0 ( y ) + r0 ( z ) 2 r ( x )
  42. Introduction Operation Time Analysis Applications Conclusion A Key Lemma :

    Access Lemma (Zig-zag Step) Claim : amortized cost  3( r0 ( x ) r ( x )) We observe that r0 ( x ) = r ( z ) and r ( x )  r ( y ) . Also 2 tree rotation is needed. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z )  2 + r0 ( y ) + r0 ( z ) 2 r ( x ) Similarily as (Zig-zig step), one can easily show ↵  3 r0 ( x ) r ( x ) . Therefore the lemma is proved.
  43. Introduction Operation Time Analysis Applications Conclusion Balance Theorem Theorem (Balance

    Theorem) The total access time of m accesses on an n-node splay tree is O (( m + n ) log n + m ) .
  44. Introduction Operation Time Analysis Applications Conclusion Balance Theorem Theorem (Balance

    Theorem) The total access time of m accesses on an n-node splay tree is O (( m + n ) log n + m ) . Proof . Assign w ( x ) = 1 /n for each x 2 T. The amortized cost of accessing each node is at most 3 log n + 1 . The net potential drop is at most P x log( W/w ( x )) , where W = P x w ( x ) , which is n log n (W = 1 ). Therefore the total time is O ( m (log n + 1)) + O ( n log n ) .
  45. Introduction Operation Time Analysis Applications Conclusion Balance Theorem Theorem (Balance

    Theorem) The total access time of m accesses on an n-node splay tree is O (( m + n ) log n + m ) . Yet, we didn’t count up for insert/delete operations. Implication : Accessing a splay tree is as e cient as any uniformly balanced binary tree.
  46. Introduction Operation Time Analysis Applications Conclusion Static Optimality Theorem Theorem

    (Static Optimality Theorem) Let f ( x ) be the access frequency (the # of access) of node x, where f ( x ) 1 for all x. Then the total access time is O m + X x2T f ( x ) log ✓ m f ( x ) ◆! .
  47. Introduction Operation Time Analysis Applications Conclusion Static Optimality Theorem Theorem

    (Static Optimality Theorem) Let f ( x ) be the access frequency (the # of access) of node x, where f ( x ) 1 for all x. Then the total access time is O m + X x2T f ( x ) log ✓ m f ( x ) ◆! . Proof . Assign w ( x ) = f ( x ) /m for each x 2 T. Clearly W = 1 and the amortized cost of accessing each node is O (log( m/f ( x ))) . The net poential drop is at most P x2T log( m/f ( x )) .
  48. Introduction Operation Time Analysis Applications Conclusion Static Optimality Theorem Theorem

    (Static Optimality Theorem) Let f ( x ) be the access frequency (the # of access) of node x, where f ( x ) 1 for all x. Then the total access time is O m + X x2T f ( x ) log ✓ m f ( x ) ◆! . Implication : Accessing a splay tree is statically optimal. It is as e cient as any fixed search tree, given the access frequency. Specially, as e cient as an optimal one (e.z. Hu↵man Tree).
  49. Introduction Operation Time Analysis Applications Conclusion Working Set Theorem Theorem

    (Working Set Theorem) Let tj be the number of distinct elements accessed, between j-th access and the previous time the element was accessed (or the beginning). Then the total access time of m access is O 0 @m + m X j =1 log( tj + 1) 1 A .
  50. Introduction Operation Time Analysis Applications Conclusion Working Set Theorem Theorem

    (Working Set Theorem) Let tj be the number of distinct elements accessed, between j-th access and the previous time the element was accessed (or the beginning). Then the total access time of m access is O 0 @m + m X j =1 log( tj + 1) 1 A . Sketch of Proof . Assign the weights 1 , 1 / 4 , · · · , 1 /n2 to the items, in order by their first access. After j-th access on node x with w ( x ) = 1 /k2, reassign w ( x ) = 1 and w ( y ) = 1 / ( k0 + 1) 2 for all y such that w ( y ) = 1 / ( k0 ) 2 > 1 /k2. Since P n k =1 1 /k2 = ⇡2/ 6 = O (1) , the amortized time for j-th access is O (log( t j + 1)) . The weight reassignment can only decrease the potential, hence non-positive amortized time for weight reassignment. The net potential drop is O ( n log n ) .
  51. Introduction Operation Time Analysis Applications Conclusion Working Set Theorem Theorem

    (Working Set Theorem) Let tj be the number of distinct elements accessed, between j-th access and the previous time the element was accessed (or the beginning). Then the total access time of m access is O 0 @m + m X j =1 log( tj + 1) 1 A . Implication : The most recently accessed items are the easiest to access. The tree work as a “working set”.
  52. Introduction Operation Time Analysis Applications Conclusion Another Key Lemma :

    Update Lemma Lemma (Update Lemma) Let W be the total weight of the nodes in the tree(s) involved in the operation. Then the amortized times of the splay tree operations have the following upper bounds: Find( T, k ) : ( 3 log( W/w ( x )) + 1 if T has a node x with key k 3 log( W/w ( x0 )) + 1 otherwise Join( T1 , T2) : 3 log( W/w ( x )) + O (1) , where x = Max( T1) . Split( T, x ) : ( 3 log( W/w ( x )) + O (1) if T has a node x with key k 3 log( W/w ( x0 )) + O (1) otherwise Here x0 is either x or x+ , such that w ( x0 ) = min {w ( x ) , w ( x+) }.
  53. Introduction Operation Time Analysis Applications Conclusion Update Lemma : Proof

    Proof. Find( T, k ) : ( 3 log( W/w ( x )) + 1 if x 2 T, key ( x ) = k 3 log( W/w ( x0 )) + 1 if x 62 T where x0 2 {x , x+} with w ( x0 ) = min {w ( x ) , w ( x+) }. Let v be the node at which the tree is splayed. Access Lemma says the amortized time is  3 log( s ( t ) /s ( v )) + 1 . In case of successful search, v = x and s ( v ) w ( x ) . In case of failed search, s ( v ) min {w ( x ) , w ( x+ ) } because either of x or x+ was in T v (before splaying). It is obvious s ( t ) = W. We are done.
  54. Introduction Operation Time Analysis Applications Conclusion Update Lemma : Proof

    Split( T, k ) : ( 3 log( W/w ( x )) + O (1) if x 2 T, key ( x ) = k 3 log( W/w ( x0 )) + O (1) otherwise Immediate from the result of Access . Join( T 1 , T 2) : 3 log( W/w ( x )) + O (1) , where x = Max( T 1) . A successful search of Max( T1) in T1 : bounded by 3 log( s ( t1) /w ( x )) + 1 . Potential increase by joining is log ✓ s ( t1) + s ( t2) s ( t1) ◆  3 log ✓ W s ( t1) ◆ . Hence the total cost is at most 3 log ✓ s ( t1) w ( x ) · W s ( t1) ◆ + 1 = 3 log ✓ W w ( x ) ◆ + O (1) .
  55. Introduction Operation Time Analysis Applications Conclusion Update Lemma (Insert, Delete)

    Lemma (Update Lemma (cont’d) ) Let W be the total weight of the nodes in the tree(s) involved in the operation. Then the amortized times of the splay tree operations have the following upper bounds: Insert( T, x ) : 3 log ⇣ W w ( x ) w ( x 0) ⌘ + log ⇣ W w ( x ) ⌘ + O (1) Delete( T, x ) : 3 log ⇣ W w ( x ) ⌘ + 3 log ⇣ W w ( x ) w ( x ) ⌘ + O (1) Insert( T, x ) alternative : 2 log ⇣ W w ( x ) w ( x 0) ⌘ + log ⇣ W w ( x ) ⌘ + O (1) Delete( T, x ) alternative : 2 log ⇣ W w ( x ) w ( x 0) ⌘ + O (1) Here x0 is either x or x+ , such that w ( x0 ) = min {w ( x ) , w ( x+) }. Proof . Straightforward from the bounds shown above.
  56. Introduction Operation Time Analysis Applications Conclusion Balance Theorem with Updates

    Theorem (Balance Theorem with Updates) A sequence of m arbitrary operations on a splay tree T, starting from the empty one, takes O ( m + Pm j =1 log nj ) , where nj is the number of nodes in the tree(s) involved in j-th operation. Proof . Assign w ( x ) = 1 for each x 2 T, then the theorem is immediate from the lemma.
  57. Introduction Operation Time Analysis Applications Conclusion Balance Theorem with Updates

    Theorem (Balance Theorem with Updates) A sequence of m arbitrary operations on a splay tree T, starting from the empty one, takes O ( m + Pm j =1 log nj ) , where nj is the number of nodes in the tree(s) involved in j-th operation. Proof . Assign w ( x ) = 1 for each x 2 T, then the theorem is immediate from the lemma. Implication : Amortized costs for basic BST operations of a splay tree is O (log n ) .
  58. Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem

    : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree.
  59. Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem

    : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. That is, a disjoint-set data structure with cut operation.
  60. Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem

    : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. That is, a disjoint-set data structure with cut operation. Sleator and Tarjan had invented Link-Cut Tree which supports the above operations in amortized O (log n ) time.
  61. Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem

    : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. That is, a disjoint-set data structure with cut operation. Sleator and Tarjan had invented Link-Cut Tree which supports the above operations in amortized O (log n ) time. Using splay tree, we can implement a dynamic tree in rather simple manner, in amortized O (log n ) time.
  62. Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem

    : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. Cost( v ) returns the cost of node v. Min( v ) returns the node of minimum cost on the path from v to Root( v ) . Add( v, c ) adds c to the cost of every node on the path from v to Root( v ) .
  63. Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Amortized

    O (log n ) is possible. Using this, for example, Maximum Flow is solvable in O ( nm log n ) instead of O ( n2 m ) . Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. Cost( v ) returns the cost of node v. Min( v ) returns the node of minimum cost on the path from v to Root( v ) . Add( v, c ) adds c to the cost of every node on the path from v to Root( v ) .
  64. Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree

    Each node has at most one child (solid child) connected with solid edge. The others are connected with dashed edge. This heuristic exploits an locality of access, hence O (log n ) time. a b c d e f g h i j k l m n o p q r s t u v w access s !
  65. Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree

    Each node has at most one child (solid child) connected with solid edge. The others are connected with dashed edge. This heuristic exploits an locality of access, hence O (log n ) time. a b c d e f g h i j k l m n o p q r s t u v w access s ! a b c d e f g h i j k l m n o p q r s t u v w
  66. Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree

    : Virtual Tree Nodes on each maximal solid path forms a solid subtree (splay tree), where higher node in the original tree has greater key. a b c d e f g h i j k l m n o p q r s t u v w
  67. Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree

    : Virtual Tree Nodes on each maximal solid path forms a solid subtree (splay tree), where higher node in the original tree has greater key. a b c d e f g h i j k l m n o p q r s t u v w Virtual Tree V f l b q p i c a j r g d m v w t u s h k e o n
  68. Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree

    : Virtual Tree Each dashed edge ( x, p ) in T corresponds to an edge ( r, p ) in V , where r is the root of a solid subtree containing x. a b c d e f g h i j k l m n o p q r s t u v w Virtual Tree V f l b q p i c a j r g d m v w t u s h k e o n
  69. Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access

    ) We have to restruct the virtual tree V , as if v was accessed in the original tree T. Note that solid paths and solid subtrees change. Access operation in Dynamic Tree, which encapsulates Splay operation in (a subtree of) Virtual tree
  70. Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access

    ) After accessing x, the subpath (of the solid path containing x) below x is separated. d g j m r s t u v w access s ! d g j m r s t u v w
  71. Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access

    ) After accessing x, the subpath (of the solid path containing x) below x is separated. Motivation : In the virtual tree, if x is a root of a solid subtree, we just cut the left subtree of x. d g j m r s t u v w access s ! d g j m r s t u v w Virtual Tree j r g d m v w t u s
  72. Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access

    ) After accessing x, the subpath (of the solid path containing x) below x is separated. Motivation : In the virtual tree, if x is a root of a solid subtree, we just cut the left subtree of x. Splay and d g j m r s t u v w access s ! d g j m r s t u v w Splayed at s j r g d m s t v w u
  73. Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access

    ) After accessing x, the subpath (of the solid path containing x) below x is separated. Motivation : In the virtual tree, if x is a root of a solid subtree, we just cut the left subtree of x. Splay and cut the left child! d g j m r s t u v w access s ! d g j m r s t u v w Splayed at s j r g d m s t v w u
  74. Introduction Operation Time Analysis Applications Conclusion Operation: Access Remark :

    Access in dynamic tree is just doing Splay within the entire virtual tree. Please do not confuse ‘splaying’ in a virtual tree with ‘splaying’ in a solid subtree, which is just a subroutine.
  75. Introduction Operation Time Analysis Applications Conclusion Operation: Access Splay( V,

    x ) 1 Splay at x within its solid subtree, and move up to the parent of x via a dahsed edge, splay again, move up, and so on · · · , until we reach the root. 2 Walk again from x up to the root, making x ’s each of ancestors a left child. Here, solid edges are changed. 3 Finally splay at x within its new solid subtree. Figure: Splay Step1
  76. Introduction Operation Time Analysis Applications Conclusion Operation: Access Splay( V,

    x ) 1 Splay at x within its solid subtree, and move up to the parent of x via a dahsed edge, splay again, move up, and so on · · · , until we reach the root. 2 Walk again from x up to the root, making x ’s each of ancestors a left child. Here, solid edges are changed. 3 Finally splay at x within its new solid subtree. Figure: Splay Step2
  77. Introduction Operation Time Analysis Applications Conclusion Operation: Access Splay( V,

    x ) 1 Splay at x within its solid subtree, and move up to the parent of x via a dahsed edge, splay again, move up, and so on · · · , until we reach the root. 2 Walk again from x up to the root, making x ’s each of ancestors a left child. Here, solid edges are changed. 3 Finally splay at x within its new solid subtree. Figure: Splay Step3
  78. Introduction Operation Time Analysis Applications Conclusion Operation: Access in one-pass

    Splay( V, x ) 1 Splay at x within its solid-subtree. 2 Cut the left-subtree of x . 3 Until x becomes the root of V : 1 p the parent of x’s solid-subtree (via dashed edge) 2 Splay at p within p’s solid-subtree. 3 Switch p’s solid child as x. 4 x p and repeat! 4 Splay at x .
  79. Introduction Operation Time Analysis Applications Conclusion Operation: Root Root( v

    ) returns the root of the tree that contains v. 1 Splay at v. Now v is the root of the entire virtual tree V . 2 Find the maximum-key node r in v’s solid tree, following right-links. 3 Splay at r, in its solid tree, and return r. a b c e f h i k l n o p q Virtual Tree V f l b q p i c a h k e o n
  80. Introduction Operation Time Analysis Applications Conclusion Operation: Root Root( v

    ) returns the root of the tree that contains v. 1 Splay at v. Now v is the root of the entire virtual tree V . 2 Find the maximum-key node r in v’s solid tree, following right-links. 3 Splay at r, in its solid tree, and return r. a b c e f h i k l n o p q Virtual Tree V f l b q p i c a h k e o n
  81. Introduction Operation Time Analysis Applications Conclusion Operation: Link Link( u,

    v ) adds an edge from u to v (u is a root in T). 1 Splay at u. Now u is a root of u’s virtual tree. 2 Connect u to v via a dashed edge. That is, p ( u ) v. a b c e f h i k l n o p q Link ( e, c ) Virtual Tree V f l b q p i c a h k e o n splay at e !
  82. Introduction Operation Time Analysis Applications Conclusion Operation: Link Link( u,

    v ) adds an edge from u to v (u is a root in T). 1 Splay at u. Now u is a root of u’s virtual tree. 2 Connect u to v via a dashed edge. That is, p ( u ) v. a b c e f h i k l n o p q Link ( e, c ) Virtual Tree V f l b q p i c a e h k o n
  83. Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v

    ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V First, let’s splay at e . f l q p i c a b e h k o n
  84. Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v

    ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V After first pass, making c root f l q p i c a b e h k o n
  85. Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v

    ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V After second pass, making e solid child f l q p i c a b e h k o n
  86. Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v

    ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V After third pass, making e root f l q p i c b a e h k o n
  87. Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v

    ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V Finally ( e, c ) is cutted. f l q p i c b a e h k o n
  88. Introduction Operation Time Analysis Applications Conclusion Time Complexity All the

    operations are done in amortized O (log n ) time. Another potential analysis : in the virtual tree V , weight w ( x ) = 1 for each node x 2 V size s ( x ) := the number of its descendants (the sum of weight), including subtrees connected via dashed edges. rank r ( x ) := log s ( x ) (binary logarithm) potential ( V ) := 2 P x in V r ( x ) A single splay operation costs amortized O (log n ) . The first pass costs 6 log n + k, where k is the depth of x after the first pass (the number of zig cases). The second pass costs zero since there is no rotations. For the third pass, each of the k rotations is charged two to take account for k rotations in the first pass, hence 6 log n + 2 . Now the total amortized time is O (log n ) .
  89. Introduction Operation Time Analysis Applications Conclusion Summary of Splay Tree

    Amortized O (log n ) BST operations (O ( n ) worst) Splay operation : the last-accessed node goes to root. Read-only access might change the structure (NOT thread-safe) As well as basic BST operations, there are various time bounds on some access patterns Various applications, such as O (log n ) Dynamic Tree
  90. Introduction Operation Time Analysis Applications Conclusion Summary of Splay Tree

    Amortized O (log n ) BST operations (O ( n ) worst) Splay operation : the last-accessed node goes to root. Read-only access might change the structure (NOT thread-safe) As well as basic BST operations, there are various time bounds on some access patterns Various applications, such as O (log n ) Dynamic Tree Theoretical time bound is as good as other search trees There are a little gap on practical performance within a constant factor, but splay tree is competitive and highly simple to write a code In some access patterns, quite faster than RB-tree due to the access locality
  91. Introduction Operation Time Analysis Applications Conclusion Some Conjectures On Splay

    Tree Dynamic Optimality Conjecture : Splay tree is dynamically optimal, that is, O (1) -competitive to an optimal o✏ine binary search tree. Open as of now. Splay tree (Sleator and Tarjan, 1985) is proved to be O (log n ) -competitive. Currently best-known competitiveness bound is O (log log n ) . Tango tree (Demaine et al., 2004) and Multi-splay tree (Wang et al., 2006) are O (log log n ) -competitive.
  92. Introduction Operation Time Analysis Applications Conclusion Some Conjectures On Splay

    Tree Dynamic Optimality Conjecture : Splay tree is dynamically optimal, that is, O (1) -competitive to an optimal o✏ine binary search tree. Traversal Conjecture : If T 1 , T 2 are two n-node splay trees containing exactly the same items, then accessing items of T 1 in order of pre-order traversal of T 2 costs O ( n ) .
  93. Introduction Operation Time Analysis Applications Conclusion Some Conjectures On Splay

    Tree Dynamic Optimality Conjecture : Splay tree is dynamically optimal, that is, O (1) -competitive to an optimal o✏ine binary search tree. Traversal Conjecture : If T 1 , T 2 are two n-node splay trees containing exactly the same items, then accessing items of T 1 in order of pre-order traversal of T 2 costs O ( n ) . Split Conjecture : The cost of any ordering of deleting all the n elements is O ( n ) .
  94. Introduction Operation Time Analysis Applications Conclusion Some Conjectures On Splay

    Tree Dynamic Optimality Conjecture : Splay tree is dynamically optimal, that is, O (1) -competitive to an optimal o✏ine binary search tree. Traversal Conjecture : If T 1 , T 2 are two n-node splay trees containing exactly the same items, then accessing items of T 1 in order of pre-order traversal of T 2 costs O ( n ) . Split Conjecture : The cost of any ordering of deleting all the n elements is O ( n ) . Deque Conjecture : The cost of performing m double-ended queue operations is O ( m + n ) . ( () amortized O (1) )