CS 473 Algorithms I Lecture X Splay Trees

  • Slides: 27
Download presentation
CS 473 -Algorithms I Lecture X Splay Trees CS 473 Lecture X 1

CS 473 -Algorithms I Lecture X Splay Trees CS 473 Lecture X 1

Splay Trees A splay tree • is a binary search tree • is self

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 operations are performed every time an access is made • The amortized cost of each operation is O(lg n) CS 473 Lecture X 2

Splay Trees • A splay consists of a sequence of rotations • The starting

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. CS 473 Lecture X 3

Review of rotations in R-B Trees Let x be the starting node for rotations

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 B p R α(B) p R δ(B) R x β(B) TYPE LR CS 473 Left-rotate(p) γ(B) x p g B B Right-rotate(g) δ(B) x R g R γ(B) R α(B) β(B) γ(B) δ(B) TYPE LL Lecture X 4

Review of rotations in R-B Trees (cont. ) g B α(B) p Right-rotate(p) R

Review of rotations in R-B Trees (cont. ) g B α(B) p Right-rotate(p) R x R β(B) α(B) δ(B) γ(B) TYPE RL CS 473 g B p p R B Left-rotate(g) g x R β(B) γ(B) δ(B) R x R α(B) β(B) γ(B) δ(B) TYPE RR Lecture X 5

Rotations in Splay Trees • Let x be the splay node; – p =

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 γ Left-rot(p) p/x α β β L-Type R-Type α CS 473 Lecture X γ 6

Rotations in Splay Trees • If x has a parent and a grandparent –

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 p δ p/p α δ x α α β γ x/gp γ g/x β γ β α γ δ β Type-LR Type-LL Type-RR x g α p x δ β CS 473 Lecture X γ δ p g α g β γ δ Type-RL 7

Rotations in Splay Trees TYPE-LL Rotation Right-rot(p²[x]) Right-rot(p[x]) p g p x α x

Rotations in Splay Trees TYPE-LL Rotation Right-rot(p²[x]) Right-rot(p[x]) p g p x α x δ γ α x β γ p α g δ g β γ β δ first right-rotate ( p[ p[x] ] ), then right-rotate( p[x] ) CS 473 Lecture X 8

Rotations in Splay Trees TYPE-LR Rotation Left-rot(p[x]) g p δ x α β g

Rotations in Splay Trees TYPE-LR Rotation Left-rot(p[x]) g p δ x α β g x α x p δ p γ Right-rot(p[x]) γ α g β γ δ β first left-rotate ( p[x] ), then right-rotate( p[x] ) CS 473 Lecture X 9

Splay Algorithm SPLAY(T, X) while x≠root[T] do if p[x]=root[T] then if x is a

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] */ CS 473 Lecture X 10

Sequence of rotations in a Splay starting at Node * 1 1 a 9

Sequence of rotations in a Splay starting at Node * 1 1 a 9 a 8 j 2 b 8 i d e g 4 * f e 3 f c (a) Initial search tree CS 473 h *5 g 5 7 6 4 c i b h 6 j 2 7 3 9 d (b) After RR rotation Lecture X 11

Sequence of rotations in a Splay starting at Node * 1 1 a 9

Sequence of rotations in a Splay starting at Node * 1 1 a 9 a 8 2 b 5 j * 4 c d *5 i 7 g 8 4 b 6 h c 6 e 3 (c) After LL rotation CS 473 j 2 e f 3 9 d i 7 f g h (d) After LR rotation Lecture X 12

Sequence of rotations in a Splay starting at Node * *5 1 9 2

Sequence of rotations in a Splay starting at Node * *5 1 9 2 a b 4 6 e 3 c j 8 i 7 f g d h (e) After RL rotation CS 473 Lecture X 13

Splay Operations • GENERAL IDEA: Splay operations tend to balance the tree. Any long

Splay Operations • GENERAL IDEA: Splay operations tend to balance the tree. Any long access times are balanced by the fact: -The tree ends up better balanced 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 CS 473 Lecture X 14

Splay Operations • AMORTIZED TIME ANALYSIS using potential function method – Each operation is

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 CS 473 Lecture X 15

Amortized Time for Splay Operations Definitions: • Size of x : s(x)= # of

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) X T • The better balanced the tree is, the lower potential is. CS 473 Lecture X 16

Amortized Time for Splay Operations NOTATION: Consider a splay rotation on x (a single

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, 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) CS 473 Lecture X 17

Amortized Time for Splay Operations • CS 473 Lemma 1: r(x) > min{ r(left[x])

Amortized Time for Splay Operations • CS 473 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 18

Amortized Time for Splay Operations • Lemma 2: For a splay rotation on a

Amortized Time for Splay Operations • Lemma 2: For a splay rotation on a node x, we have (1)r’(x) r(x) 0 (2)if p[x] = root[T], then Ф < r(x) (3)if p[x] ≠ root[T], then Ф < 3 r(x) - 1 Proof of (1): • Obvious since gains descendants CS 473 Lecture X 19

Amortized Time for Splay Operations Proof of (2): L-type and R-type rotations – Ranks

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) CS 473 Lecture X 20

Amortized Time for Splay Operations • - Proof of (3): LL, LR, RL, RR-type

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) ) 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) ) CS 473 Lecture X (a) (b) 21

Amortized Time for Splay Operations • Proof of (3) continued: - Consider the middle

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) } 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) } CS 473 Lecture X (c) 22

Amortized Time for Splay Operations • Proof of (3) continued: - Thus we have

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) < -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 = 3 r(x) - 1 CS 473 Lecture X 23

Amortized Time for Splay Operations • Proof of (3) continued: Case 2: Substituting Case

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 = 3 r(x) – 1 CS 473 Lecture X 24

Amortized Time for Splay Operations • Lemma 3: The amortized cost of a splay

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 + r < 3 r (3) Ĉi(x) = 1 + Ф < 1 + (3 r – 1) = 3 r CS 473 Lecture X 25

Amortized Time for Splay Operations - Then, for the whole splay operation Let T

Amortized Time for Splay Operations - Then, for the whole splay operation Let T 0, T 1, T 2, …, Tk be the sequence of trees produced Let r 0, r 1, r 2, …, 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]) – r 0(x) ) = 3( lg n – r(x) ) CS 473 Lecture X 26

Amortized Time for Splay Operations • Theorem: The amortized cost of a splay operation

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 CS 473 Lecture X 27