Sie sind auf Seite 1von 5

Splay Trees are

( 9.3)
Binary Search Trees
Splay Trees

3
4

entries stored only at


internal nodes
keys stored at nodes in the
left subtree of v are less
than or equal to the key
stored at v
keys stored at nodes in the
right subtree of v are
greater than or equal to the
key stored at v

(35,R)

Splay Trees

Searching in a Splay Tree:


Starts the Same as in a BST

2004 Goodrich, Tamassia, Dickerson

(14,J)

(7,T)
(1,Q)

(1,C)

(5,H)

all the keys in the yellow


region are 20

(5,G)
(6,Y)

(5,I)

Splay Trees

Example Searching in a BST,


continued
(20,Z)

(10,A)

(7,T)
(1,Q)

(1,C)

(2,R)

(7,P)

(36,L)

(10,A)

(7,T)

(37,P)
(1,Q)

(40,X)

(1,C)

(10,U)

(6,Y)

(5,H)

2004 Goodrich, Tamassia, Dickerson

Splay Trees

(14,J)

(21,O)

(7,P)

(36,L)

(37,P)
(40,X)

(10,U)

(5,G)
(5,I)

(35,R)

(8,N)

(2,R)

(5,G)
(5,I)

Splay Trees

(21,O)

(8,N)

(5,H)

search for key 8, ends at


an internal node.

(35,R)

(14,J)

(40,X)

(10,U)

(7,P)

(2,R)

(37,P)

(36,L)

(20,Z)

Search proceeds down


the tree to found item
or an external node.
Example: Search for
time with key 11.

(21,O)

(8,N)

An inorder traversal will


return the keys in order

2004 Goodrich, Tamassia, Dickerson

(10,A)

BST Rules:

2004 Goodrich, Tamassia, Dickerson

all the keys in the blue


region are 20

(20,Z)

note that two keys of


equal value may be wellseparated

(6,Y)

Splay Trees do Rotations after


Every Operation (Even Search)

Splaying:

x is a left-left grandchild means x is a left child of its

parent, which is itself a left child of its parent


g is ps parent

p is xs parent;

start with
node x

new operation: splay

right rotation

left rotation

makes the left child x of a node y into


ys parent; y becomes the right child
of x

is x the
root?

splaying moves a node to the root using rotations

is x a left-left
grandchild?

stop

zig
- zig
right-rotate about g,
right-rotate about p

yes

makes the right child y of a node x


into xs parent; x becomes the left
child of y
x

a right rotation about y

yes

no

is x a child of
the root?

is x a right-right
grandchild?

no

zig
- zig
left-rotate about g,
left-rotate about p

yes

a left rotation about x


yes

T3

T1

is x a right-left
grandchild?

T1
y

T2

T1

T3

T2

is x the left
child of the
root?
T3
yes

(structure of tree above y


is not modified)

T2

(structure of tree above x


is not modified)

T3

T1

zig
- zag

y
T1

T4

T1

T2

T3

T4

yes

Splay Trees

(20,Z)

(10,A)

(35,R)

g
(14,J)

(7,T)

p
(1,Q)

(8,N)

T2

(5,H)

(2,R)

T3

g
x

zig

(10,A)

(10,U)

(7,T)

T4

(5,G)
(6,Y)

2004 Goodrich, Tamassia, Dickerson

T1

T2

Splay Trees

(20,Z)

(21,O)

(37,P)

(36,L)

(40,X)

(35,R)

(8,N)

(7,T)

(1,C)

(1,Q)

(7,P)

2.

(5,H)

(after first rotation)

T3
T4

(10,U)

(7,P)

(35,R)

(14,J)

(8,N)

(1,Q)

T2
T3

(before
rotating)

(40,X)

g
p

(36,L)

(10,A)

(21,O)

(37,P)

T1

1.

(37,P)

x
(1,C)

(5,I)

T2

(21,O)

(20,Z)

T3

zig
- zig

zig
- zag
right-rotate about p,
left-rotate about g

T1

left-rotate about
the root

let x = (8,N)
x is the right child of its parent,
which is the left child of the
grandparent
left-rotate around p, then rightrotate around g

T4

is x a left-right
grandchild?

zig

2004 Goodrich, Tamassia, Dickerson

left-rotate about p,
right-rotate about g

yes

Splaying Example

Visualizing the
Splaying Cases
y

zig

right-rotate
about the root

T2

Splay Trees

2004 Goodrich, Tamassia, Dickerson

no

zig
- zag

T1

T2

T3

T4

(2,R)

(6,Y)

2004 Goodrich, Tamassia, Dickerson

(40,X)

3.
(after second
rotation)

(5,G)
(5,I)

Splay Trees

(36,L)

(14,J)

(10,U)

(5,H)

(2,R)

(5,G)
(5,I)

(1,C)

(7,P)

(6,Y)

x is not yet the root, so


we splay again 8

Example Result
of Splaying

Splaying Example, Continued


(20,Z)

x
(7,T)
(1,Q)

(1,C)

(35,R)

(10,A)

(7,P)

(14,J)

(10,U)

(5,H)

(2,R)

(5,G)
(5,I)

(21,O)

(6,Y)

(37,P)

(36,L)

(8,N)

2.
(20,Z)

(7,T)

(before applying
rotation)

(1,Q)

(7,P)

(14,J)

(5,H)

(10,U)

(5,G)

(21,O)

(40,X)

(6,Y)

(10,U)

(5,G)
(40,X)
(6,Y)

(10,A)

(1,Q)

(35,R)

(37,P)

(14,J)

(7,T)

(37,P)

(8,N)

(1,C)

(5,H)

(7,P)

(2,R)

(35,R)

(8,N)

(21,O)

(36,L)

(10,U)

(1,C)

(36,L)

(5,H)

(2,R)

after first splay

(5,G)
(5,I)

(21,O)

(7,P)

(5,G)
(5,I)

(6,Y)

2004 Goodrich, Tamassia, Dickerson

(10,U)

(6,Y)

after second
splay

Splay Trees

10

Splay Trees & Ordered


Dictionaries

Splay Tree Definition

which nodes are splayed after each operation?

a splay tree is a binary search tree where a


node is splayed after it is accessed (for a
search or update)

method

deepest internal node accessed is splayed


splaying costs O(h), where h is height of the tree
which is still O(n) worst- case

find(k)

insert(k,v)

O(h) rotations, each of which is O(1)


remove(k)

Splay Trees

(40,X)

(37,P)

(36,L)

Splay Trees

(37,P)

(36,L)

(7,P)

(40,X)

(14,J)

(7,T)

(35,R)

(10,A)

x is the root, so stop

2004 Goodrich, Tamassia, Dickerson

(21,O)

(8,N)

(20,Z)

(10,A)

(after rotation)

(1,Q)
(1,C)

(5,I)

(14,J)

(7,T)

(20,Z)

(40,X)

(2,R)

(35,R)

(5,I)

1.

2004 Goodrich, Tamassia, Dickerson

(10,A)

before

tree might not be more balanced


(1,Q)
e.g. splay (40,X)
before, the depth of the shallowest leaf is(1,C)
(5,H)
3 and the deepest is 7
after, the depth of shallowest leaf is 1
(2,R)
and deepest is 8

now x is the left child of the root


right-rotate around root

(8,N)

(20,Z)

11

splay node
if key found, use that node
if key not found, use parent of ending external node
use the new node containing the entry inserted
use the parent of the internal node that was actually
removed from the tree (the parent of the node that the
removed item was swapped with)

2004 Goodrich, Tamassia, Dickerson

Splay Trees

12

Amortized Analysis of
Splay Trees

Cost per zig

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.
Costs: zig = $1, zig
- zig = $2, zig
- zag = $2.
Thus, cost for playing a node at depth d = $d.
Imagine that we store rank(v) cyber- dollars at each
node v of the splay tree (just for the sake of analysis).

Splay Trees

2004 Goodrich, Tamassia, Dickerson

13

zig
- zig

T4

T1

T2

T2

T3

T4

T1

x
T2

2004 Goodrich, Tamassia, Dickerson

T1

T1

T1

T2

T2

T3

T4

Doing a zig at x costs at most rank(x)- rank(x):


cost = rank(x) + rank(y)- rank(y)- rank(x)
< rank(x)- rank(x).
Splay Trees

14

at most 3(rank(r)- rank(x))- d + 2:


Proof: Splaying x takes d/2 splaying substeps:
d /2

cost cost i
i =1

(3( rank i ( x ) rank i 1 ( x )) 2) + 2

x
z

T4

T3

d /2

zig
- zag
y

Doing a zig
- zig or zig
- zag at x costs at most
3(rank(x)- rank(x))- 2.
Proof: See Proposition 9.2, Page 440.
z

T4

Cost of splaying a node x at depth d of a tree


rooted at r:

T1

T3

Cost of Splaying

y
x

zig

2004 Goodrich, Tamassia, Dickerson

Cost per zig-zig and zig-zag


z

i =1

y
T2

T3

= 3( rank ( r ) rank 0 ( x )) 2( d / d ) + 2

T4

3( rank ( r ) rank ( x )) d + 2.

T3
Splay Trees

15

2004 Goodrich, Tamassia, Dickerson

Splay Trees

16

Performance of
Splay Trees
Recall: rank of a node is logarithm of its size.
Thus, amortized cost of any splay operation is
O(log n).
In fact, the analysis goes through for any
reasonable definition of rank(x).
This implies that splay trees can actually
adapt to perform searches on frequentlyrequested items much faster than O(log n) in
some cases. (See Proposition 9.4 and 9.5.)
2004 Goodrich, Tamassia, Dickerson

Splay Trees

17

Das könnte Ihnen auch gefallen