Splay Trees- self adjusting trees Bottom-Up Splay Trees • Binary search trees. • Search, insert, delete, and join are done as in an • No extra information is stored on the nodes (no height nor unbalanced binary search . color) • Search, insert, and delete are followed by a • All operations “search, insert, delete, and split” have splay operation that begins at a splay node . amortized complexity O(log n) & actual complexity O( n). • When the splay operation completes, the splay  (split – generating two binary trees, one contains all the node has become the tree root. keys larger than a given value, the other tree contains the rest). • Join requires no splay. • Actual and amortized complexity of join is O( 1).  (Join – gluing trees. Inverse of split.)

Splay Splay Step Splaying – pushing a node to the root. • If q = null or q is the root, do nothing (splay is • Let q be a node in the tree, which we refer to as over). Else the splay node . • If q is at level 2, do a one-level move and terminate the splay operation. • During a splay operation, q is moved up the q tree using a series of splay steps . p a p • In a splay step , the node q moves up the tree by q 1or 2 levels. a,b,c,d - subtrees a b b c • the case that q right child of p is symmetric.

Splay Step 2-Level Move (case 2) • If q is at a level > 2, do a two-level move and continue the splay operation. q gp q gp p gp p a p d p d ab c d a b gp q q c b c a b c d •q is a left child of left child of gp , or • q lef t child of right child of gp is symmetric. •q is a right child of right child of gp . Treaed symmetrically •Zig-zag step • This is a zig-zig step. Performing search( k) Performing insert( x ) 20 20

10 40 10 40

6 15 30 6 15 30 47

2 8 25 2 8 25 • If there is already a node contains key x, then this node is the splay • Definition: If there is a node containing key k, then this is the splay node. node . • Otherwise, the newly inserted node is the splay node. • Otherwise, the node where the search terminates is the splay node . • Example – insert(47) • On either case, perform splay on this node.

Performing delete( x) – deleting x Performing concat(T1,T 2) – merging 2 trees

• Given two trees T1, T2, such that each key in T1, is smaller • Find x and splay it – it moves to the root. than each key in T2. • Remove it – it leaves two subtrees. • Concatenate them. • Idea: We splay the largest element y of T1, • We obtain a tree whose root does not have a right subtree. • Same result – shorter description: • We connect the root to the right subtree.  Find x, splay it, and replace the root with the largest key in the last subtree (note that it does not have right child) y x y Splay x kill x and concat T T T1 T2 1 2 T T1 T2 T1 T2

split( k) Main Result • Split – generate two subtrees, one with all its keys larger than k, and one with all keys smaller than k. • Use the unbalanced binary insert algorithm to insert a a Theorem: Any sequence of m operations on an new key k. initially empty that never contains • Then splay on k. more than n nodes takes O( m log n) • Following the splay, the left and right subtrees are the output trees.

k

TSL TBR Amortized Analysis of splay trees

• Running time of each operation is proportional to time for splaying. • Define rank( v ) as the logarithm (base 2) of the number of nodes in subtree rooted at v. • Actual Costs: zig = $1 , zig- zig= $2 , zig- zag= $2 . • Thus, cost for splaying a node at depth d = $d . • Imagine that we store rank( v) dollars at each node v of the splay tree (just for the sake of analysis).

• Invariant. Each node v always has at least rank( v) dollars. • Lemma 1 : Each splay operations requires us to invest at most 3 log 2n+1 new dollars, to maintain the invariant.