+ All Categories
Home > Documents > CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

Date post: 17-Dec-2015
Category:
Upload: valentine-simon
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
27
CS473 Lecture X 1 CS473-Algorithms I Lecture X Splay Trees
Transcript
Page 1: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 1

CS473-Algorithms I

Lecture X

Splay Trees

Page 2: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 2

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)

Splay Trees

Page 3: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 3

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.

Page 4: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 4

Review of rotations in R-B TreesLet 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]])

B

R

Rα(B)

β(B) γ(B)

δ(B)B

R

α(B) β(B)

γ(B)

δ(B)

B

R R

α(B) β(B) γ(B) δ(B)

Left-rotate(p) Right-rotate(g)

g g p

R

TYPE LR TYPE LL

p

x

p

x

x g

Page 5: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 5

Review of rotations in R-B Trees (cont.)

B

R

R

α(B)

β(B) γ(B)

δ(B)

B

Rα(B)

β(B)

γ(B) δ(B)

B

R R

α(B) β(B) γ(B) δ(B)

Right-rotate(p) Left-rotate(g)

g g p

R

TYPE RL TYPE RR

p

x

p

x

xg

Page 6: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 6

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

L-Type

Left-rot(p)

Right-rot(p)x/p

α

β γ

x/p

p/x

R-Type

Page 7: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 7

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.

α β

δ

g/x

Type-LL

p/p

γx/gp

α

δ

x/g

p/p

γ

g/xβ

Type-RR

α

β

g

p

γ

β

x

p

γ δ

α

β

g

p

γ

α

g

Type-LR

β

x

γ

g p

δ α

Type-RL

Page 8: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 8

Rotations in Splay TreesTYPE-LL Rotation

α β

δ

g

p

γxβ

x

p

γα

g

δ

α

β

δ

g

p

γ

x

first right-rotate ( p[ p[x] ] ), then right-rotate( p[x] )

Right-rot(p²[x]) Right-rot(p[x])

Page 9: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 9

Rotations in Splay TreesTYPE-LR Rotation

α

β

δ

g

p

γ

first left-rotate ( p[x] ), then right-rotate( p[x] )

α β

δ

g

x

γpx

β

p

x

γα

g

δ

Right-rot(p[x])Left-rot(p[x])

Page 10: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 10

Splay AlgorithmSPLAY(T,X)while x≠root[T] do

if p[x]=root[T] thenif 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] */

Page 11: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 11

Sequence of rotations in a Splay starting at Node *

1

9

8

2

7

6

3

4

5

1

9

8

2

7

6

5

4

3

a

ji

b

h

g

c

d

e f

(a) Initial search tree (b) After RR rotation

a

j

ib

h

g

fe

dc

*

*

Page 12: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 12

Sequence of rotations in a Splay starting at Node *

1

9

8

2

5

4

3

6

7

1

9

5

2

4

3

8

6

7

a

ji

b

hgc d

e f

(c) After LL rotation (d) After LR rotation

a

j

ib

hgfe

dc

**

Page 13: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 13

Sequence of rotations in a Splay starting at Node *

5

1

2

3

9

8

7

(e) After RL rotation

a j

ib

hg

fe

dc

*

4 6

Page 14: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 14

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

Page 15: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 15

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

Page 16: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 16

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

Page 17: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 17

Amortized Time for Splay OperationsNOTATION: 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)

Page 18: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 18

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

Page 19: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 19

Amortized Time for Splay Operations

• Lemma 2: For a splay rotation on a node x, we have

(1)r’(x) r(x) r(x) 0

(2)if p[x] = root[T], then Ф < r(x)

(3)if p[x] ≠ root[T], then Ф < 3r(x) - 1

Proof of (1):• Obvious since gains descendants

Page 20: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 20

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)

Page 21: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 21

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)

Page 22: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 22

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) }

Page 23: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 23

Amortized Time for Splay Operations

• Proof of (3) continued:- Thus we have either

r’(x) > 1 + r(x) r’(x) - r(x) > 1 Case 1or

r(g) > 1 + r’(g) r’(g) - r(g) < -1 Case 2Case 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

Page 24: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 24

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

Page 25: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 25

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

Page 26: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 26

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) )

)(33C)(0

100

i i

k

ii

k

ii

k

i

rrrxC

Page 27: CS473Lecture X1 CS473-Algorithms I Lecture X Splay Trees.

CS473 Lecture X 27

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


Recommended