240 likes | 299 Views
Explore the actual and amortized complexities of search, insert, delete, join, and split operations in Splay Trees. Learn about potential functions and root rank concepts, with examples and detailed analyses.
E N D
Bottom-Up Splay Trees–Analysis • Actual complexity of search, insert, delete, and split is O(n). • Actual complexity of join is O(1). • Amortized complexity of search, insert, delete, join, and split is O(log n).
Potential Function • size(x) = #nodes in subtree whose root is x. • rank(x) = floor(log2 size(x)). • P(i) = Sx is a tree node rank(x). • P(i) is potential after i’th operation. • size(x) and rank(x) are computed after i’thoperation. • P(0) = 0. • When join and split operations are done, number of splay trees > 1 at times. • P(i) is obtained by summing over all nodes in all trees.
20 10 40 6 30 8 Example 6 2 • size(x) is in red. 3 1 2 1 2 1 1 0 1 0 • rank(x) is in blue. • Potential = 5.
6 2 20 3 1 2 1 10 40 2 1 1 0 6 30 1 0 8 Root Rank • rank(root) <= floor(log2 n) • n is total number of operations
m S B Join • Actual complexity = 1 • Rank of root is <= floor(log2 n). • Other ranks unchanged. • Potential increases by <= floor(log2 n). • amortized complexity = actual complexity + DP <= 1 + floor(log2 n)
Other Operations • actual complexity = # of splay steps • amortized complexity = actual complexity + DP = # of splay steps + DP
Splay Step Potential Change • If q = null or q is the root, do nothing. • DP = 0.
q p a p q c b c a b 1-Level Move • Do a one-level move. • r(x) = rank of x before splay step. • r’(x) = rank of x after splay step.
q p a p q c b c a b 1-Level Move • DP = r’(p) + r’(q) – r(p) – r(q) <= r’(q) – r(q).
q gp a p p d b gp q c c d a b 2-Level Move (case 1) • DP = r’(gp) + r’(p) + r’(q) – r(gp) – r(p) – r(q)
q gp a p p d b gp q c c d a b 2-Level Move (case 1) • r’(gp) <= r’(q) • r (q) <= r(p) • r’(q) = r(gp) • r’(p) <= r’(q)
2-Level Move (case 1) • DP = r’(gp) + r’(p) + r’(q) – r(gp) – r(p) – r(q) • r’(q) = r(gp) • r’(gp) <= r’(q) • r’(p) <= r’(q) • r (q) <= r(p) • DP <= r’(q) + r’(q) – r(q) – r(q) = 2(r’(q) – r(q))
2-Level Move (case 1) A more careful analysis reveals that DP <= 3(r’(q) – r(q)) –1
2-Level Move (case 2) • Similar to Case 1.
Only 1-Level Moves • Suppose that the splay node q is initially at level L > 1. • Let r(q,j) be the rank of q when it is at level j following/preceding a splay step. • When only 1-level moves are made, DP <= S2<=j<=L(r(q,j-1)-r(q,j)) = r(q,1)-r(q,L)
Search With Only 1-Level Moves • amortized complexity = # of splay steps + DP <= L-1+r(q,1)-r(q,L) <= n-1+floor(log2n) = O(n)
Correct Splay • Make as many 2-level moves as possible. A 1-level move is made at the end if necessary. • When L is odd, floor(L/2)2-level moves are made; there is no 1-level move. • Single 2-level move DP <= 3(r’(q) – r(q)) –1 • For complete splay DP <= 3(r(q,1)-r(q,L))-floor(L/2) = 3(r(q,1)-r(q,L))- # of splay steps
Correct Splay • When L is even, L/2-12-level moves are made; there is also one 1-level move. • Single 2-level move DP <= 3(r’(q) – r(q)) –1 • Single 1-level move DP <= r’(q) – r(q) <= 3(r’(q) – r(q)) • For complete splay DP <= 3(r(q,1)-r(q,L))- # of splay steps+1 DP <= S2<=j<=L(r(j-1)-r(j)) = r(1)-r(L)
Correct Splay • So, regardless of the value of L DP <= 3(r(q,1)-r(q,L))- # of splay steps+1 <= 3floor(log2n)-# of splay steps+1
Search With Correct Splay • amortized complexity = # of splay steps + DP <= # of splay steps +3floor(log2n)-# of splay steps+1 = 3floor(log2n)+1 = O(log n)
Insert 7 4 • On every leaf to root path ranks are non-decreasing. • At most floor(log2 n)+1 different ranks on such a path. • Insert may increase the rank of only the last node in each sequence of equal rank nodes (view sequence from bottom to top). • When you insert, potential increases by up to floor(log2 n)+1. 4 4 1 1 0
Insert With Correct Splay • total potential change caused by an insert <=floor(log2n)+1 +3floor(log2n)-# of splay steps+1 • amortized complexity = # of splay steps + DP <= 4floor(log2n)+2 = O(log n)
Delete With Correct Splay • total potential change caused by a delete <=3floor(log2n)-# of splay steps+1 • amortized complexity = # of splay steps + DP <= 3floor(log2n)+1 = O(log n)
Split With Correct Splay • total potential change caused by a split <=3floor(log2n)-# of splay steps+2 • amortized complexity = # of splay steps + DP <= 3floor(log2n)+2 = O(log n)