Sunteți pe pagina 1din 17

Splay Trees

6
v
3 8
z
4

Splay Trees 1
Binary Search
Trees (20 all the keys in the blue
note that two keys of ,Z) region are ≥ 20
equal value may be well-
separated (10 (35
,A) ,R)
BST Rules:
(14
 items stored only at (7, ,J) (21 (37
internal nodes T) ,O) ,P)
 keys stored at nodes in (1, (8, (36 (40
the left subtree of v are Q) N) ,L) ,X)
less than or equal to the
key stored at v
(1, (5, (7, (10
 keys stored at nodes in C) H) P) ,U)
the right subtree of v are
greater than or equal to
(2, (5, all the keys in the yellow
the key stored at v
R) G) region are ≤ 20
An inorder traversal will (5 (6,
return the keys in order ,I) Y)

Splay Trees 2
Starts the Same as in a
BST (20
,Z)
Search proceeds
down the tree to (10 (35
,A) ,R)
found item or an
external node. (7,
(14
,J) (21 (37
T) ,O) ,P)
Example: Search for
(1, (8, (36
time with key 11. Q) N) ,L)
(40
,X)

(1, (5, (7, (10


C) H) P) ,U)

(2, (5,
R) G)
(5 (6,
,I) Y)

Splay Trees 3
Example Searching in a
BST, continued (20
,Z)
search for key 8, ends
(10 (35
at an internal node. ,A) ,R)

(14
(7, ,J) (21 (37
T) ,O) ,P)

(1, (8, (36 (40


Q) N) ,L) ,X)

(1, (5, (7, (10


C) H) P) ,U)

(2, (5,
R) G)
(5 (6,
,I) Y)

Splay Trees 4
after Every Operation (Even
Search)
new operation: splay
splaying moves a node to the root using rotations

 right rotation  left rotation


 makes the left child x of a node y into  makes the right child y of a node x
y’s parent; y becomes the right child into x’s parent; x becomes the left
of x child of y

y x
a right rotation about y a left rotation about x

x y
T T
x y
3 1

T T y T T x
T T
1 2 2 3
1 3

(structure of tree above y T T (structure of tree above x T T


is not modified) 2 3 is not modified) 1 2

Splay Trees 5
Splayin
g:  “x is a left-left grandchild” means x is a
left child of its parent, which is itself a
start with left child of its parent
node x  p is x’s parent; g is p’s parent

is x a left-left
is x the yes
stop grandchild? zig-zig
root?
yes right-rotate about g,
no right-rotate about p
is x a right-right
is x a child of no grandchild? zig-zig
the root? left-rotate about g,
yes
left-rotate about p
yes
is x a right-left
grandchild? zig-zag
is x the left
no yes left-rotate about p,
child of the
right-rotate about g
root?
is x a left-right
yes zig zig grandchild? zig-zag
right-rotate left-rotate about yes right-rotate about p,
about the root the root left-rotate about g
Splay Trees 6
the Splaying
Cases zig-zag x
z
z
y z y
y T T
4
1
x T T T T T
x T 4 1 2 3 4

3
T T
T T zig-zig 2 3

1 2

x y
zig
x x
T y T
1 4

z w w y
T T
2 3

T T T T
T T T T
1 2 3 4
3 4
1 2

Splay Trees 7
Example
(2
0,
Z)

(10 (35
,A) ,R)
let x = (8,N) g

 x is the right child of its p


(7
,T
(1
4,J
)
(21
(3
7,
1.
,O)
) P) (before
parent, which is the left (1 (8 (3
,Q ,N 6, (40
rotating)
child of the grandparent ) ) L) ,X)
x
 left-rotate around p, then (1 (5 (7 (10
,C ,H ,P
right-rotate around g ) ) )
,U)

(2 (5
(2 ,R ,G
0, ) )
( (6
Z) ,Y
5,
I) ) (2
(10 (35 0,
g ,A) ,R) Z)

(8 (1 (8 (35
4,J (3
x ,N (21
7,
x ,N ,R)
) ) ,O) )
P) g
(7 (3 (7 (3
(10 (40 (10 (21
p ,T
,U)
6,
,X)
p ,T
,A) ,O)
7,
) L) ) P)
(1 (7 (1 (7 (1 (3
,Q ,Q ,P 6, (40
,P 4,J
) ) ) L) ,X)
) )

(1
,C
(5 2. (1
,C
(5 (10

3.
,H ,H ,U)
) ) (after first rotation) ) )

(2 (5 (2 (5 (after second
,R
)
,G
)
,R
)
,G
)
rotation)
( (6 ( (6
5, ,Y 5, ,Y x is not yet the root, so
I) ) I) )
Splay Trees we splay again 8
Splaying Example,
Continued
now x is the left child of the root
(2  right-rotate around root
0,
Z)

(8 (35
x ,N ,R)
)
(7 (3
(10 (21
,T 7,
,A) ,O)
) P)
(1 (7 (1 (3
,Q ,P 6, (40 (8
4,J
) ) L) ,X) x ,N
2.
)
)
(1 (5 (10 (7 (2
,C ,H ,U) ,T 0, (after rotation)
) )
1. (1
)
(7
Z)

(2 (5
,R ,G (before applying ,Q
)
,P
)
(10
,A)
(35
,R)
)
(
)
(6 rotation) (1 (5 (1
5, ,Y (3
,C ,H 4,J (21
I) ) 7,
) ) ) ,O)
P)
(3
(2 (5 (10 (40
6,
,R ,G ,U) ,X)
L)
) )
( (6
5, ,Y
I) )
x is the root, so stop
Splay Trees 9
Example (2

Result of
0,
Z)

(10 (35
,A) ,R)

Splaying before (7
,T
)
(1
4,J
)
(21
,O)
(3
7,
P)
tree might not be more balanced (1
,Q
(8
,N
(3
6, (40
,X)
e.g. splay (40,X) ) ) L)

(1 (5
 before, the depth of the shallowest ,C ,H
(7 (10
,P ,U)
leaf is 3 and the deepest is 7 ) ) )

(2 (5
 after, the depth of shallowest leaf is ,R ,G
(40
1 and deepest is 8 )
(
)
(6 ,X)
5, ,Y
(2
(2 I) )
0,
0, Z)
Z)
(10 (3
(10 (40 ,A) 7,
,A) ,X) P)
(1
(1 (3 (7 4,J (35
(7 4,J 7, ,T ,R)
)
,T ) P) )
) (1 (8
(1 (8 (35 ,Q ,N (3
(21
,Q ,N ,R) ) ) 6,
,O)
) ) L)
(1 (5 (7
(1 (3 (10
(5 (7 (21 ,C ,H ,P
(10 6, ,U)
,C ,H ,P ,O) ) ) )
,U) L)
) ) )
(2 (5
(2 (5 ,R ,G
,R
)
,G
)
after first splay )
(
)
(6 after second
( (6
,Y
5, ,Y
)
splay
5, I)
)
I)
Splay Trees 10
Splay Tree Definition
a splay tree is a binary search tree
where a node is splayed after it is
accessed (for a search or update)
 deepest internal node accessed is splayed
 splaying costs O(h), where h is height of the
tree – which is still O(n) worst-case
 O(h) rotations, each of which is O(1)

Splay Trees 11
Splay Trees & Ordered
Dictionaries
which nodes are splayed after each operation?

method splay node

if key found, use that node


findElement
if key not found, use parent of ending external node

insertElement use the new node containing the item inserted

use the parent of the internal node that was


actually removed from the tree (the parent of the
removeElemen
node that the removed item was swapped with)
t

Splay Trees 12
Amortized Analysis of
Splay Trees
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 13
Cost per zig
y zig

x T x
4

w w y
T
3
T T T T
T T
1 2 3 4
1 2

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
Cost per zig-zig and zig-
zag
z x
y T zig-zig T y
4
x T 1
T z
3
T T 2
T T
1 2
3 4

Doing a zig-zig or zig-zag at x costs at most


3(rank’(x) - rank(x)) - 2.
 Proof: See Theorem 3.9, Page 192.

z zig-zag x

y z y
T
x T T T T T
1
4 1 2 3 4
T T
2 3

Splay Trees 15
Cost of Splaying
Cost of splaying a node x at depth d of a tree
rooted at r:
 at most 3(rank(r) - rank(x)) - d + 2:
 Proof: Splaying x takes d/2 splaying substeps:
d /2
cost ≤ ∑ cost i
i =1
d /2
≤ ∑ (3( rank i ( x ) − rank i −1 ( x )) − 2) + 2
i =1

= 3( rank ( r ) − rank 0 ( x )) − 2( d / d ) + 2
≤ 3( rank ( r ) − rank ( x )) − d + 2.
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 frequently-requested items
much faster than O(log n) in some cases. (See
Theorems 3.10 and 3.11.)

Splay Trees 17

S-ar putea să vă placă și