270 likes | 388 Views
C S473 -Algorithms I. Lecture X Splay Trees. Splay Trees. A splay tree is a binary search tree is self adjusting maintains balance without any explicit balance condition such as color serves as an excellent tool to demonstrate the power of amortized analysis
E N D
CS473-Algorithms I Lecture X Splay Trees Lecture X
Splay Trees A splay tree • is a binary search tree • is self adjusting • maintains balance without any explicit balance condition such as color • serves as an excellent tool to demonstrate the power of amortized analysis • Splay operationsare performed every time an access is made • The amortized cost of each operation is O(lg n) Lecture X
Splay Trees • A splay consists of a sequence of rotations • The starting node for a splay is obtained as follows • SEARCH: The node containing the searched element • INSERT : The newly inserted node • DELETE : The parent of the physically deleted node • An unsuccessful search can be modeled as the last node encountered during the search • Splay rotations are performed along the path from the starting node to the root. • Splay rotations are similar to those performed in AVL & R-B trees. Lecture X
Review of rotations in R-B Trees Let x be the starting node for rotations during an insert operation Let p & g; denote x’s parent(p[x]) and grandparent(p[p[x]]) g p g B B B Right-rotate(g) Left-rotate(p) p p x g δ(B) R δ(B) R R R x x α(B) R R γ(B) α(B) β(B) γ(B) δ(B) β(B) γ(B) α(B) β(B) TYPE LR TYPE LL Lecture X
Review of rotations in R-B Trees (cont.) g p g B B B Left-rotate(g) p Right-rotate(p) p g x α(B) R R α(B) x R R x R δ(B) R β(B) α(B) β(B) γ(B) δ(B) β(B) γ(B) δ(B) γ(B) TYPE RR TYPE RL Lecture X
Rotations in Splay Trees • Let x be the splay node; • p = p[ x ] • g = p[ p[ x ] ] • If x = NIL or x = root[T] , then splay terminates • If x has a parent but no grandparent ( i.e. p = root[T] ) Rotation is classified as: Type-L/ Type-R: x is a left/right child of p p/x x/p Right-rot(p) x/p p/x γ Left-rot(p) α γ β α β L-Type R-Type Lecture X
Rotations in Splay Trees • If x has a parent and a grandparent • Type LL/ Type RR: Both p and x are Left/Right children. • Type LR/Type RL: p is a Left/Right child whereas x is a Right/Left child. x g g/x x/g p g p δ p/p p/p α δ x α α γ δ β x/gp g/x γ β γ β α γ δ β Type-LR Type-LL Type-RR x g p p g α x α γ δ β δ Type-RL β γ Lecture X
g p δ x γ α β Rotations in Splay Trees TYPE-LL Rotation Right-rot(p²[x]) Right-rot(p[x]) p x x p g α g β α γ β δ γ δ firstright-rotate (p[p[x]]), thenright-rotate(p[x]) Lecture X
g x δ p γ α β Rotations in Splay Trees TYPE-LR Rotation Right-rot(p[x]) x Left-rot(p[x]) g p p g δ x α α γ β δ γ β firstleft-rotate (p[x]), thenright-rotate(p[x]) Lecture X
Splay Algorithm SPLAY(T,X) while x≠root[T] do if p[x]=root[T] then if x is a left child then /* TYPE L */ RIGHT-ROTATE(T,p[x]) else LEFT-ROTATE(T,p[x]) /* TYPE R */ elseif both p[x] & x are left children /* TYPE-LL */ RIGHT-ROTATE(T,p[p[x]]) RIGHT-ROTATE(T,p[x]) elseif both p[x] & x are right children /* TYPE-RR */ LEFT-ROTATE(T,p[p[x]]) LEFT-ROTATE(T,p[x]) elseif p[x] & x are left & right children /* TYPE-LR */ LEFT-ROTATE(T,p[x]) RIGHT-ROTATE(T,p[x]) /* node: this is a new p[x] */ elseif p[x] & x are right & left children /* TYPE-RL */ RIGHT-ROTATE(T,p[x]) LEFT-ROTATE(T,p[x]) /*node: this is a new p[x] */ Lecture X
Sequence of rotations in a Splay starting at Node * 1 1 a a 9 9 j 8 8 j i 2 2 i b 7 b 7 h 6 6 h * g 3 5 g c 4 4 * f d 5 e 3 e f c d (a) Initial search tree (b) After RR rotation Lecture X
Sequence of rotations in a Splay starting at Node * 1 1 a a 9 9 j 8 * j i 2 5 * b 5 2 8 b 4 4 6 i 6 e f e f 3 7 3 7 g g h h c d c d (c) After LL rotation (d) After LR rotation Lecture X
Sequence of rotations in a Splay starting at Node * * 5 1 9 a 2 j 8 b i 4 6 e f 3 7 g h c d (e) After RL rotation Lecture X
Splay Operations • GENERAL IDEA: Splay operations tend to balance the tree . Any long access times are balanced by the fact: -The tree ends up betterbalanced speeding subsequent access • IN POTENTIAL TERMS: The idea is • As a tree is built high, its potential energy increases. • Accessing a deep item releases the potential • As the tree sinks down • Paying for the extra work required Lecture X
Splay Operations • AMORTIZED TIME ANALYSIS using potential function method • Each operation is followed by a splay • Actual complexity of a splay operation is of the same order of the whole access operation • Therefore it is sufficient to consider only the complexity of splay operation Lecture X
Amortized Time for Splay Operations Definitions: • Size of x : s(x)= # of nodes in the subtree Tx rooted at x • Rank of x : r(x)= lg s(x) • s(leaf node) = 1; s(root) = n • r(leaf node) = 0; r(root) = lg n • Potential of a splay tree T Ф(T) = r(x) • The better balanced the tree is, the lower potential is. X T Lecture X
Amortized Time for Splay Operations NOTATION:Consider a splay rotation on x (a single splay step) • r(x) & r’(x) : rank of node x before and after the rotation • T & T’ : the splay tree before and after the rotation • Ф(T) andФ’(T) : Potential of tree before and after the rotation • T’’: The middle tree during LL, LR, RR, RL type rotations. • r’’(x) : rank of a node x in the middle TR • Ф = Ф’(T) - Ф (T) : Increase in the potential of the tree r(x)= r’(x) - r(x) : Increase in the rank of node x due to a splay rotation on x • Amortized cost of a splay step (rotation) on a node x • Ĉi(x) = Ci(x) + Ф’(T) - Ф(T) = Ci(x) + Ф Note that actual cost of a rotation: Ci(x) =O(1) Lecture X
Amortized Time for Splay Operations • Lemma 1: r(x) > min{ r(left[x]) , r(right[x]) } + 1 Proof: s(x) = s(left[x]) + s(right[x]) + 1 2 min{ s(left[x]) , s(right[x]) } + 1 > 2 min{ s(left[x]) , s(right[x]) } lg s(x) > lg(2 min{ s(left[x]) , s(right[x]) }) = lg 2 + lg(min{ s(left[x]) , s(right[x]) }) = 1 + min{ r(left[x]) , r(right[x]) } QED Lecture X
Amortized Time for Splay Operations • Lemma 2: For a splay rotation on a node x, we have • r’(x) r(x) r(x) 0 • if p[x] = root[T], then Ф < r(x) • if p[x] ≠ root[T], then Ф < 3r(x) - 1 Proof of (1): • Obvious since gains descendants Lecture X
Amortized Time for Splay Operations Proof of (2): L-type and R-type rotations • Ranks of , , do not change • Only nodes x & p change ranks • s’(x) = s(p) r’(x) = r(p) • Ф = ( r’(x) + r’(p) ) – ( r(x) + r(p) ) = ( r’(x) – r (x) ) + ( r’(p) – r(p) ) = r’(p) – r(x) < r’(x) – r(x) since r’(p) < r’(x) Lecture X
Amortized Time for Splay Operations • Proof of (3): LL, LR, RL, RR-type rotations • Consider the LL-Type rotation, the others are similar. • Ranks of , , , δ do not change • Only x, p, g change ranks • Ф = ( r’(x) – r (x) ) + ( r’(p) – r(p) ) + ( r’(g) – r(g) ) (a) s’(p) < s’(x) r’(p) < r’(x) s(p) > s(x) r(p) > r(x) Therefore, r’(p) – r(p) < r’(x) – r(x) Hence, (a) becomes Ф < 2( r’(x) – r(x) ) + ( r’(g) – r(g) ) (b) Lecture X
Amortized Time for Splay Operations • Proof of (3) continued: • Consider the middle tree T’’, due to Lemma 1, r’’(p) > 1 + min{ r’’(x) , r’’(g) } (c) but r’’(p) = r(g) = r’(x) since Tp’’ = Tg = Tx’ r’’(x) = r(x) since Tx’’ = Tx r’’(g) = r’(g) since Tg’’ = Tg Hence, (c) becomes r(g) = r’(x) > 1 + min{ r(x) , r’(g) } Lecture X
Amortized Time for Splay Operations • Proof of (3) continued: • Thus we have either r’(x) > 1 + r(x) r’(x) - r(x) > 1 Case 1 or r(g) > 1 + r’(g) r’(g) - r(g) < -1 Case 2 Case 1: r’(g) < r(g) r’(g) - r(g) < 0 < r’(x) - r(x) - 1 (d) substituting (d) into (b) we get Ф < 3( r’(x) - r(x) ) - 1 = 3r(x) - 1 Lecture X
Amortized Time for Splay Operations • Proof of (3) continued: Case 2: Substituting Case 2 into (b) we again get Ф < 2( r’(x) - r(x) ) - 1 Ф < 3( r’(x) - r(x) ) - 1 = 3r(x) – 1 Lecture X
Amortized Time for Splay Operations • Lemma 3: The amortized cost of a splay operation that begins at node x of a BST with n nodes is at most 3(lg n – r(x)) + 1 That is Ĉ(x) < 3(lg n – r(x)) Proof: - Consider the two cases (2) & (3) of Lemma 2 (2) Ĉi(x) = Ci(x) + Ф < 1 + Ф < 1 + r < 3r (3) Ĉi(x) = 1 + Ф < 1 + (3r – 1) = 3r Lecture X
Amortized Time for Splay Operations - Then, for the whole splay operation Let T0, T1, T2, …, Tk be the sequence of trees produced Let r0, r1, r2, …, rk be the respective rank functions • Hence, • Note that the latter series telescopes • Ĉ(x) < 3( final rank of x – initial rank of x ) = 3( r(root[T]) – r0(x) ) = 3( lg n – r(x) ) Lecture X
Amortized Time for Splay Operations • Theorem: The amortized cost of a splay operation is O(lg n) Follows from lemma 3 since r(x) ≥ 0 Lecture X