Splay Trees Advanced)
-
Upload
api-3801329 -
Category
Documents
-
view
135 -
download
3
Transcript of Splay Trees Advanced)
Splay Trees
by
Amna Humayun
Splay Trees
Self-adjusting form of binary search tree. A splay rotation consist of a sequence of
rotations.
They follow the heuristicMove to the root
Rotation
Rotation plays an important role in Splay
trees.
Types of rotation: Left-Rotation Right-Rotation
Left-Rotation
a
x
b
P
c
b
a
x
b
P
c
b
Right-Rotation
x
a b
P
c
b
x
a b
P
c
b
Use of Splay Trees
In many applications, when a node is accessed, it is likely to be accessed again in the near future.
The central idea of splay is
If a node is deep, there are many node on the path that are relatively deep, and then
restructuring make future accesses cheaper on all these nodes.
Types of Splay Trees
Bottom-Up Splay Trees Top-Down Splay Trees
Bottom-Up Splay Trees
Bottom-Up Splay Trees
Similar to binary search tree. Search, insert and delete operations are
performed in the same way, except that they are followed by splay.
In split, however first splay is performed. Bottom-up splay rotations are performed along
the path from the start node to the root of the binary search tree.
Simple idea
One way of performing restructuring is to perform single rotations, bottom-up.
Rotate every node on the access path with its parent.
p4
p5
f
g
p3
e
Example — Search(p1) Search(p1)
p2
b
p2
c d
p1p1
c
p2
p5
p4
p5
p4
p3p3
p1
p2
p4
p5
f
g
p3
e
Example — Search(p1) Perform single rotation bottom-up.
p2
b
p2
c d
p1p1
c
p2
p4
p5
f
g
p3
e
Example — Search(p1) Perform single rotation bottom-up.
p2
b
p2
c d
p1p1
c
p2
b
p2 d
p1p1
c
p2
p4
p5
f
g
Example — Search(p1) Perform single rotation bottom-up.
p2
b
p2 d
p1p1
c
p2
p3
e
p3p3p3
p4
p5
f
g
Example — Search(p1) Perform single rotation bottom-up.
dp2
b
p2
p1p1
c
p2
p3
e
p3p3
d
dp2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p3
p4
p5
f
g
Example — Search(p1) Perform single rotation bottom-up.
p2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p4
p5
g
Example — Search(p1) Perform single rotation bottom-up.
p2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p4
f
p4
d
p3
e
p3p3
d
p2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p4
f
p4
p4p1
p5
g
Example — Search(p1) Perform single rotation bottom-up.
p2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p4
f
p4
p5
Example — Search(p1) Perform single rotation bottom-up.
p2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p4
f
p4
p5
g
p5
d
p3
e
p3p3
p4
f
p4
p1
p5
Example — Search(p1)
p2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p4
f
p4
p5
g
p5
The rotations have pushed p1 to the root, for easy future access. But, it has pushed another node p3
almost as deep as p1 used to be.
p4
p5
f
g
p3
ep2
b
p2
c d
p1p1
c
p2
p5
p4
p5
p4
p3p3
p1
p2
Original Tree
SPLAYING
The splaying strategy is similar to rotation idea, except that it a little more selective about how rotations are performed.
Steps for Splay
Let x be the node at which splay is being performed.
If x is a root, then splay terminates. If x is a non-root node and x has parent (p) but
no grandparent — zig case. If x is a non-root node and x has both parent (p)
and grandparent (g), then there are two cases:Zig-Zig CaseZig-Zag Case
Steps for Splay…ZIG CASEIf x is the left child of p perform right rotation around p.
x
a b
p
cRight-Rotation
x
a b
P
c
b
Steps for Splay… ZIG CASE (symmetric)
If x is the right child of p perform left rotation around p.
a
x
b
p
cLeft-Rotation
a
x
b
P
c
b
Steps for Splay…ZIG-ZIG CASE If x is the left child of p, and p is also the left child of g. Perform right rotation around g. Perform right rotation around p.
x
a b
p
c
g
dp
g
c
b
p
a c
g
d
c
x
a
x g
x
a b
p
ag
dc
b
p
x
Steps for Splay…ZIG-ZIG CASE (symmetric)If x is the right child of p, and p is also the right child of g. Perform left rotation around g. Perform left rotation around p.
g
a bp
ax
dc
b
g
pa
p
b
g
a
b
d
x
c
g x
p
b
g
a d
x
c
c
x
p
Steps for Splay…ZIG-ZAG CASE If x is the left child of p, and p is the right child of g. Perform right rotation around p. Perform left rotation around g.
x
b c
p
d
g
a p
x
x
b c
P
d
c
g
a
x
bP
dc
g
p
g
ax
ap
dc
b
b
x
g
Steps for Splay…ZIG-ZAG CASE (symmetric)If x is the right child of p, and p is the left child of g. Perform left rotation around p. Perform right rotation around g.
x
b c
p
a
g
dp
x b
x
a c
g
d
c
p
a
x
b c
p
a
g
d
b
x
cp
a b
g
x
gp
Search
If the search is successful, then it must end at a particular node and the start node of splay is that particular node.
If the search is unsuccessful, then it must end at some external node and the start node of splay is the parent of that external node.
p4
p5
f
g
p3
e
Example Search(p1) — Using Splay Search(p1). Search is successful.
p2
b
p2
c d
p1p1
c
p2
p5
p4
p5
p4
p3p3
p1
p2
p4
p5
f
g
p3
e
Example Search(p1) — Using Splay Perform splay bottom-up from p1.
p2
b
p2
c d
p1p1p1zig-zag
p2
p4
p5
f
g
p3
e
Perform splay bottom-up from p1.
p2
b
p2
c d
p1p1
c
zig-zag
Example Search(p1) — Using Splay
p2
b
p2 d
p1p1
c
p2
p4
p5
f
g
Perform splay bottom-up from p1.
p2
b
p2 d
p1p1
c
p2
p3
e
p3p3
zig-zag
Example Search(p1) — Using Splay
p4
p5
f
g
Example — Search(p1) Perform single rotation bottom-up.
dp2
b
p2
p1p1
c
p2
p3
e
p3p3
d
dp2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p3
p4
f
Perform splay bottom-up from p1.
p2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p4
p5
g
p5
zig-zig
f
Example Search(p1) — Using Splay
p5
p4
f
Perform splay bottom-up from p1.
p2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
p4
p5
g
p5
zig-zig
f
Example Search(p1) — Using Splay
p5
Perform splay bottom-up from p1.
p2
b
p2
p1p1
c
p2
d
p3
e
p3p3
d
zig-zigp4p4
f
p5
g
p5p1
Example Search(p1) — Using Splay
d
p3
e
p3p3
d
p4
p1
p2
b
p2
p1p1
c
p2p4p4
f
p5
g
p5
p1
d
p3
e
p3p3
d
Example Search(p1) — Using Splay
Splaying not only moves the accessed node to the root, but also has the effect of roughly halving the depth of
most nodes on the access path.
p4
p5
f
g
p3
ep2
b
p2
c d
p1p1
c
p2
p5
p4
p5
p4
p3p3
p1
p2
Original Tree
Insert
If the element to be inserted is already present in the tree, then the start node of splay is the node containing that particular element.
If the element to be inserted is not present in the tree, then insert element in the tree and the start node of splay is the newly inserted node.
Insert(36)
Search(36) Element not found. Insert 36.
16
319
5 13 20 37
4035
16
31
37
35
36
16
31
37
35
Insert(36)
Perform splay bottom-up from 36.
16
319
5 13 20 37
4035
36
zig-zag
36
35
3636
35
35
Insert(36)
Perform splay bottom-up from 36.
16
319
5 13 20 zig-zag
3636
35
37
40
37373737
Insert(36)
Perform splay bottom-up from 36.
16
319
5 13 20 zig-zag
3636
35
37
40
37
3636
3537
40
37
37
Insert(36)
Perform splay bottom-up from 36.
31
20 3636
3537
40
37
zig-zig
31
16
9
5 13
1616
Insert(36)
Perform splay bottom-up from 36.
31
20 3636
3537
40
37
zig-zig
31
16
9
5 13
16
20
Insert(36)
Perform splay bottom-up from 36.
3636
3537
40
37
zig-zig3131
20
16
9
5 13
16 36
35
31
36
Insert(36)
37
40
373131
20
16
9
5 13
16
36
35
31
36
Original Tree
16
319
5 13 20 37
4035
16
31
37
35
36
16
31
37
35
Delete
If the element to be deleted is present in the tree, then the node containing that element is deleted, and the start node of splay is the parent of the deleted node.
If the element to be deleted is not present in the tree, unsuccessful search must end at some external node, and the start node of splay is the parent of that external node.
Delete(36)
Search(36) Element found. Delete 36.
16
319
5 13 20 37
4035
16
31
37
35
36
16
31
37
35
36
Delete(36)
Perform splay bottom-up from parent of 36 i.e. 35.
16
319
5 13 20
3535
zig-zag
37
40
37
Delete(36)
Perform splay bottom-up from parent of 36 i.e. 35.
16
319
5 13 20
3535
zig-zag
37
40
37
3535
37
40
37
37
Delete(36)
Perform splay bottom-up from parent of 36 i.e. 35.
16
9
5 13
zig-zag
3535
37
40
37
31
20
31
Delete(36)
Perform splay bottom-up from parent of 36 i.e. 35.
16
9
5 13
zig-zag
3535
37
40
37
31
20
31
3535
37
40
3731
20
31
31
Delete(36)
Perform splay bottom-up from parent of 36 i.e. 35.
3535
37
40
3731
20
31
16
9
5 13
16 zig16
Delete(36)
Perform splay bottom-up from parent of 36 i.e. 35.
3535
37
40
3731
20
31
16
9
5 13
16 zig
31
20
31
35
16
Delete(36)
3535
37
40
37
31
20
31
16
9
5 13
16
31
16
Original Tree
16
319
5 13 20 37
4035
16
31
37
35
36
16
31
37
35
Delete(16)
Search(16) Element found. Delete 16. 35
3716
9 31
5
40
13 20
35
16
35
31
20
31
delete by copy
Delete(16)
Search(16) Element found. Delete 16. 35
37
9 31
5
40
13
35
16
35
31
20
31
Delete(16)
Perform splay bottom-up from parent of 16 i.e. 31.
35
37
9 31
5
40
13
35
20
35
3131
2020
31
zig-zag
31
Delete(16)
Perform splay bottom-up from parent of 16 i.e. 31.
35
37
31 40
3535
3131
zig-zag
319
5 13
202020
31313131
9
5 13
202020
20
Delete(16)
Perform splay bottom-up from parent of 16 i.e. 31.
35
3731
20
5
9
40
13
353535zig-zag
Delete(16)
Perform splay bottom-up from parent of 16 i.e. 31.
31
20
5
9
13
35
37
40
353535zig-zag
31
35
Delete(16)
35
3520
9
135
37
40
353135
3716
9 31
5
40
13 20
3535
3131
Original Tree
Split (similar to search) Search the node on which split is to be performed.
If the search is successful, then it must end at a particular node and the start node of splay is that particular node.
If the search is unsuccessful, then it must end at some external node and the start node of splay is the parent of that external node.
Split return two trees one contain all items in the tree less than or equal to searched node, the other tree contain all items in tree greater than searched node.
Split (at 40) Search(40) Search is successful.
70
8030
20 8050
20 6040
39 43
35
70
50
70
3030
40
50
Split (at 40) Perform splay bottom-up from 40.
70
8030
20 8050
20 6040
39 43
35
70
50
70
3030
40
50
zig-zag
50
Split (at 40) Perform splay bottom-up from 40.
70
8030
20
2040
39 43
35
7070
3030
40
zig-zag
50
60
505050
43
2040
39
35
40
50
60
505050
43
50
Split (at 40) Perform splay bottom-up from 40.
70
8030
20
7070
3030zig-zag
2040
39
35
40
50
60
505050
43
30
Split (at 40) Perform splay bottom-up from 40.
70
80
7070
zig-zag
39
35
50
60
505050
43
204040
30
20
303030
39
35
50
60
505050
43
204040
30
20
303030
39
35
30
Split (at 40) Perform splay bottom-up from 40.
50
60
505050
43
204040
30
20
303030
39
35
80
707070zig
50
60
505050
43
40
70
Split (at 40) Perform split at 40.
80
707070
50
60
505050
43
204040
30
20
303030
39
35
40
Split (at 40)
80
707070
50
60
505050
43
204040
30
20
303030
39
35
40
Original Tree
70
8030
20 8050
20 6040
39 43
35
70
50
70
3030
50
Amortized Analysis Lemma If a + b ≤ c, and a and b are both positive integers, then loga + logb ≤ 2 logc -2
Proof By the arithmetic-geometric mean inequality, ab ≤ (a + b)/2 Thus ab ≤ c /2 Squaring both sides gives ab ≤ c2 /4 Taking logarithms of both sides log(ab) ≤ log(c2 /4) loga + logb ≤ 2 logc -2
Amortized Analysis Amortized Time (AmT) = Actual Time for the ith operation (AcT) +
(Potential Cost of ith operation (Pi) – Potential Cost of (i-1)th operation (Pi-1) )
AmT = AcT + (Pi – Pi-1 ) Potential function Ф(T) = Σ log s(i)
where s(i) = Number of descendants of i + i itself. r(i) = log s(i) (Rank of node i) Then Ф(T) = Σ r(i) Benefit of the potential function A splay rotation can only change the rank of x , p and g. where r (x) and r '(x) is the rank before and after rotation.
i Є T
i Є T
Theorem The amortized time to splay a tree with root T at node x is at most
3( r(T) - r(x) ) +1= O(log N).
Proof If x is the root of T, then there are no rotations.
Potential change = 0.
AcT = 1 (cost to access the node)
AmT = AcT + (Pi – Pi-1 )
AmT = 1 + 0
Amortized Analysis
Cost of zig case
AcT = 1 (cost of single rotation)
Pi = r '(x)+ r '(p)
Pi-1= r (x)+ r (p)
AmT = AcT + (Pi – Pi-1 )
AmT = 1 + r '(x)+ r '(p) - r (x) - r (p) since only x and p can change rank
AmT ≤ 1 + r '(x) - r (x) since r (p) ≥ r '(p)
AmT ≤ 1 +3( r '(x) - r (x)) since r' (x) ≥ r (x)
Amortized Analysis
x
a b
p
c
c
p
b
X
a
Original Tree Final Tree
For more detail
Cost of zig-zig case
AcT = 2 (cost of double rotation)
Pi = r '(x) + r '(p) + r '(g)
Pi-1= r (x) + r (p) + r (g)
AmT = AcT + (Pi – Pi-1 )
AmT = 2 + r '(x)+ r '(p) + r '(g) - r (x) - r (p) - r (g) since only x, p and g can change rank
AmT = 2 + r '(p) + r '(g) - r (x) - r (p) since r '(x) = r (g)
AmT ≤ 2 + r '(p) + r '(g) - 2 r (x) since r (x) ≤ r (p)
AmT ≤ 2 + r '(x) + r '(g) - 2 r (x) —— (1) since r '(x) ≥ r '(p)
Amortized Analysis
Original Tree Final Tree
x
a b
p
c
g
d
x
a bp
ag
dc
b
Cost of zig-zig case
s '(x) +s '(g) ≤ s '(x) from figure
log s '(x) + log s '(g) ≤ 2 log s '(x) -2 from lemma
r '(x) + r '(g) ≤ 2 r '(x) – 2 by definition of rank
AmT ≤ 2 + r '(x) + r '(g) – 2 r (x) Putting the value in equation 1
AmT ≤ AmT ≤ 2 + r '(x) + r '(g) + r (x) – 3 r (x)
AmT ≤ 3( r '(x) - r (x) )
Amortized Analysis
Original Tree Final Tree
x
a b
p
c
g
d
x
a bp
ag
dc
b
For more detail
Cost of zig-zag case
AcT = 2 (cost of double rotation)
Pi = r '(x) + r '(p) + r '(g)
Pi-1= r (x) + r (p) + r (g)
AmT = AcT + (Pi – Pi-1 )
AmT = 2 + r '(x)+ r '(p) + r '(g) - r (x) - r (p) - r (g) since only x, p and g can
change rank
AmT = 2 + r '(p) + r '(g) - r (x) - r (p) since r '(x) = r (g)
AmT ≤ 2 + r '(p) + r '(g) - 2 r (x) —— (1) since r (x) ≤ r (p)
Amortized Analysis
Original Tree Final Tree
x
b c
p
d
g
ag
a b
x
ap
dc
Cost of zig-zag case
s '(p) +s '(g) ≤ s '(x) from figure
log s '(p) + log s '(g) ≤ 2 log s '(x) -2 from lemma
r '(p) + r '(g) ≤ 2 r '(x) – 2 by definition of rank
AmT ≤ 2 + r '(p) + r '(g) – 2 r (x) Putting the value in equation 1
AmT ≤ 2( r '(x) - r (x) )
AmT ≤ 3( r '(x) - r (x) ) since 2( r '(x) - r (x) ) ≤ 3( r '(x) - r (x) )
Amortized Analysis
Original Tree Final Tree
x
b c
p
d
g
ag
a b
x
ap
dc
For more detail
Since the last splaying step leaves x at the root T.
The amortized bound of entire splay is
Amortized Analysis
3( r '(T) - r (x)) + 1 = O( log N)
Top-Down Splay Trees
Similar to bottom-up search trees, top-down splay trees consists of a sequence of rotations.
Splay rotations are performed along the path from root to the splay node (start node of bottom-up splay tree) of the binary search tree.
Top-Down Splay Trees…
Steps for Splay
Partition the binary search tree into three components.
Original Tree Right Tree (R) Left Tree (L)
L and R are initially empty. They are reassembled
with the new root of the original tree in the end.
c
p q
x
r
L R
null null null null
Steps for Splay
Reassemble
Attach left child of the new root of original tree as the right child of largest element in L.
Attach right child of the new root of original tree as the left child of smallest element in R.
Attach right child of L as the left child of new root of original tree.
Attach left child of R as the right child of new root of original tree.
Steps for Splay…
Let x be a node. If x is the splay node, then splay terminates. If x is a non- splay node and x has child (c) —
zig case. If x is a non-splay node and x has both child (c)
and grandchild (gc), then there are two cases:Zig-Zig CaseZig-Zag Case
Steps for Splay… ZIG CASE If c is the left child of x. Attach x and its right child as a left child of the smallest item in R. x is the new smallest item in R. c is now the new root of original tree.
c
p q
x
r
L R
null null
c
p q
x
r
L R
null null null null nullnull
Steps for Splay… ZIG CASE (symmetric) If c is the right child of x. Attach x and its left child as a right child of the largest item in L. x is the new largest item in R. c is now the new root of original tree.
L R
r
c
q
x
p
L R
r
c
q
x
p
null nullnull nullnull nullnull null
Steps for Splay…
For the zig case to apply
c needs not to be a leaf node. If the item to be searched for is smaller than c, and c
has no left child but has a right child. If the item to be searched for is greater than c, and c has no right child but has a left child.
Steps for Splay… ZIG-ZIG CASE If gc is the left child of c and c is the left child of x. Perform right rotation around x. Attach c and its right child as a left child of the smallest item in R. c is the new smallest item in R. gc is now the new root of original tree.
L RL R
gc
p q
c
r
x
sgc
p q
c
r
x
s
x
cnull null null null
null null null null
Steps for Splay… ZIG-ZIG CASE (symmetric) If gc is the right child of c and c is the right child of x. Perform left rotation around x. Attach c and its left child as a right child of the largest item in L. c is the new largest item in L. gc is now the new root of original tree.
L RL Rx
p bc
agc
sr
qx
p q
c
agc
sr
c
x
null nullnull null
null nullnull null
Steps for Splay… ZIG-ZAG CASE If gc is the left child of c and c is the right child of x. Attach x and its left child as a right child of the largest item in L. x is the new largest item in L. c is now the new root of original tree.
L RL R
gc
q r
C
s
X
p
gc
q r
C
s
X
pnull nullnull null
null nullnull null
Steps for Splay… ZIG-ZAG CASE (symmetric) If gc is the right child of c and c is the left child of x. Attach x and its right child as a left child of the smallest item in R. x is the new smallest item in R. c is now the new root of original tree.
L R
gc
q r
c
p
x
s
gc
q r
c
p
x
s
L r
gc
q r
c
p
x
s null nullnull null null null null null
Search
Let x is the node to be searched. Perform splay rotations along the path from root
to the splay node (x). If the search is successful, then splay must end at a
searched node, which is now the new root of the original tree.
If the search is unsuccessful, then splay must end at a particular node greater than/ less than the node to be searched and this particular node is now the new root of the original tree.
Reassemble the three trees in the end.
Search(19) Search(19) by performing splay.
30
5 8050
20 3020
15 24
13
50
3012
25
18
16
Search(19) Search(19) by performing splay.
30
5 8050
20 3020
15 24
13
50
3012
25
18
16
L R
12
25
20null null null null
Search(19) Search(19) by performing splay.
L R
30
5
301212
8050
20 3040
15 24
13
5025
18
16
25
20
zig-zag
12null null nullnull
Search(19) Search(19) by performing splay.
2040
1524
13 18
16
L R
30
5
301212
8050
30
502525
20
15
zig-zig
24
null null null
Search(19) Search(19) by performing splay.
L R
30
5
301212
15
13
16
18
2040
24
8050
30
502525
20
zig-zig
20
25
nullnull null
Search(19) Search(19) by performing splay.
L R
30
5
301212 2040
24
8050
30
502525
20
15
13
16
18
15
18
zig
null
Element not found .
15
null null
Search(19) Reassemble.
L R
30
5
301212 2040
24
8050
30
502525
20
15
13
15
16
1818
null null
Search(19) Reassemble.
L R
2040
24
8050
30
502525
20
1818
30
5
301212
15
13 16
15
18
nullnullnullnull
Search(19)
Bottom-Up Splay Tree
20
16
18
1225
5 15
13
20
2040
24
8050
30
502525
20
1818
30
5
301212
15
13 16
15
Top-Down Splay TreeOriginal Tree
30
5 8050
20 3020
15 24
13
50
3012
25
18
16
Insert Let x is the node to be inserted. Create a new node (z). If tree is empty, a one-node tree is created. Otherwise perform splay rotations along the path from
root to the splay node (x). If the element to be inserted is already present in the
tree, then discard the z. If the element to be inserted is not present in the tree.
If the new root of original tree contains a value larger than the z Attach new root and its right subtree to the right of a z.
If the new root of original tree contains a value smaller than the z Attach new root and its left subtree to the left of a z.
Insert(80) Create a new node 80
60
90
10070
65
30
10 40
20
50
80
Insert(80) Search(80) by performing splay.
60
90
10070
60
90
65
30
10 40
20
zig-zig
5050
80
RL
null null null null
Insert(80) Search(80) by performing splay.
10070
60
65
zig-zig30
10 40
20
5050RL
80
10070
90
65
null null null null
Insert(80)
Search(80) by performing splay.
zig-zigRL
6060
30
10 40
20
5050
80
10070
65
10070
90
65
60
50
null null nullnull
Insert(80)
Search(80) by performing splay.
RL
6060
30
10 40
20
5050
Element not found . 80
zig
70
65
70
65
70 100100
90
null
70null null null
Insert(80)
Search(80) by performing splay.
RL
6060
30
10 40
20
5050
Element not found . 80
zig
70
65
70
65
70 100100
90
90null nullnull
Insert(80)
Insert 80.
70 < 80
RL
6060
30
10 40
20
5050
90
100
90
70
65
70
65
70
8080
null null
Insert(80) Attach 70 and its left sub tree to the left of 80.
RL
6060
30
10 40
20
5050
90
100
906565
707070
8080
70
null null
Insert(80)
Reassemble.
RL
6060
30
10 40
20
5050
90
100
90
707070
65
8080
null null
Insert(80)
Reassemble.
RL 80
6060
30
10 40
20
50507070
65
90
100
90
80
null nullnull null
Insert(80)
60
90
10070
60
90
8565
30
10 40
20
5050
Original TreeBottom-Up Splay Tree
50
20 9060
50 70
5080
10 40
20
30
100
65
80
6060
30
10 40
20
505070
65
90
100
90
Top-Down Splay Tree
Insert(33) Search 33 by performing splay. 33
31
2037
4035
31
37
16
9
5 13
1616
31
37
L
nullnull
zig-zig
36
R
nullnull
Insert(33) Search 33 by per forming splay.
33
31
2037
4035
31
37
zig-zig
20
16
9
5 13
16
36
L
nullnull
R
nullnull
Insert(33) Search 33 by per forming splay.
33
L
nullnull
zig-zig
3131
20
16
9
5 13
16
31
16
37
4035
37
36
R
nullnull
Insert(33) Search 33 by per forming splay.
33
L
null
zig
3131
20
16
9
5 13
16
37
4035
37
35
?
Element not found .
35
36
R
nullnull
Insert(33) Search 33 by per forming splay.
33
L
null null
R
null
zig
3131
20
16
9
5 13
16
37
40
37
373535
36
Insert(33) Insert 33. 35 > 33 33
L
null null
R
3131
20
16
9
5 13
16
37
40
37
35
36
Insert(33) Attach 35 and its right sub tree to the right of 33.
33
L
null null
R
3131
20
16
9
5 13
16
37
40
37
35
36
35
36
Insert(33) Reassemble.
33
L
null null
R
3131
20
16
9
5 13
16
37
40
37
35
36
Insert(33) Reassemble.
33L
null
R
null3131
20
16
9
5 13
16
null
33
37
40
37
35
36
null
31
2037
35 40
31
37
16
9
5 13
16
Original Tree
36
33
3131
20
16
9
5 13
16
37
40
37
35
33
Top-Down Splay Tree
36
33
3131
20
16
9
5 13
16
37
37
35
33
40
Bottom-Up Splay Tree
36
Insert(33)
Delete Let x be the node to be deleted. Perform splay rotations along the path from root to the splay node (x). If the element to be deleted is not present in the tree, the simply return. If the element to be deleted is present in the tree.
If the left child of new root of original tree is null. Make its right child the new root. Delete x. Reassemble.
If the left child of new root of original tree is not null. Find maximum from the left sub tree by performing splay. Make it the new root. Attach right child to it. Delete x. Reassemble.
If both the left and right of new root of original tree are null. Delete x. Reassemble L and R by attaching right child of L as the left child of smallest item in R.
Delete(70) Search(70) by performing splay.
50
85
9570
80
29
15 40
20
45
8375
Delete(70) Search(70) by performing splay.
50
85
9570
50
85
80
29
15 40
20
zig-zig
4545RL
8375
nullnullnullnull
Delete(70) Search(70) by performing splay.
60
90
9570
50
85
80
zig-zig29
15 40
20
5045RL
8375
nullnullnullnull
Delete(70) Search(70) by performing splay.
90
9570
85
80
zig-zigRL
6050
29
15 40
20
5045
8375
90
9570
85
80
8375
50
45
nullnullnull
null
Delete(70) Element found.
90
9570
85
80
RL
6050
29
15 40
20
5045
70
zig
8375
nullnullnull
Delete(70) Element found.
RL
6050
29
15 40
20
5045
zig90
95
85
80
7070
8375
Empty85
nullnullnull
Delete(70) Left child of 70 is null. Make its right child the new root.
RL
6050
29
15 40
20
5045
90
95
85
7070
80
8375
nullnull null
Delete(70) Delete 70.
RL
6050
29
15 40
20
5045
90
95
85
7070 80
8375nullnull
Delete(70) Reassemble
RL
6050
29
15 40
20
5045
90
95
85
80
8375nullnull
Delete(70) Reassemble
RL80
6050
29
15 40
20
5045 75
90
95
85
83
nullnull null null
Delete(70)
Original Tree
50
85
9570
80
29
15 40
20
45
8375
Bottom-Up Splay Tree
15
20
85
9583
43
29 75
40
8080
6050
29
15 40
20
5045 75
90
95
85
83
Top-Down Splay Tree
Delete(40) Search(40) by performing splay.
72
8459
35
22 40
55
Delete(40) Search(40) by performing splay.
Element found.
72
8459
35
22 40
55L R
55
35
4040
zig-zag
nullnullnullnull
Delete(40) Search(40) by performing splay. Element found.
L R
35
22 40
35
4040
zig-zag
72
8459
5555
55 nullnullnull
null
Delete(40) Search(40) by performing splay. Element found.
L R35
22
35
40
72
8459
5555
zig
35 nullnull null
Delete(40) Both left and right of 40 are null. Reassemble L and R by attaching right child of L as the left child of
smallest item in R.
L R40
72
8459
555535
22
35 nullnull nullnull
Delete(40) Delete 40.
L R
35
22
35
40
72
8459
5555nullnull nullnull
Delete(40)
Original Tree
72
8459
35
22 40
55
Bottom-Up Splay Tree
55
72
59
22
84
35
35
22
35 72
8459
5555
Top-Down Splay Tree
Delete(69) Search(69) by performing splay.
120
125
60
69
80
90
65
6762
75
Delete(69) Search(69) by performing splay. Element found.
120
125
60
69
80
90
65
6762
80
60
69
zig-zag
LR
75
nullnullnullnull
69
Delete(69) Search(69) by performing splay. Element found.
zig-zag
120
125
80
90
80L
R
80
60
70
65
6762
60
69
7575
nullnullnull
null
Delete(69) Search(69) by performing splay.
120
125
80
90
80
LR
60
70
65
6762
69
7575
zig
60 nullnull
null
Delete(69) Left child of 69 is not null. Find maximum from left subtree of 69 by performing splay.
120
125
80
90
80
LR
70
65
6762
69
7575
60 65
67
nullnull
L
null null
R
null null
Delete(69) Left child of 69 is not null. Find maximum from left subtree of 69 by performing splay.
120
125
80
90
80
LR
7069
7575
60
67
65
62
65
67
zig nullnull
L
null null
R
null null
Delete(69) Left child of 69 is not null. Find maximum from left subtree of 69 by performing splay.
120
125
80
90
80
LR
7069
7575
60
67
65
62
65
67
zig nullnull
L
null
R
nullnull null
Delete(69) 67 is now the new root.
120
125
80
90
80
LR
7069
7575
60
65
62
65
67
nullnull
L
null
R
null null
67
null
Delete(69) Attach right child of 69 as right child of 67.
120
125
80
90
80
LR
7069
75
60
65
62
65
nullnull 67
Delete(69) Delete 69.
120
125
80
90
80
LR
7069
75
60
65
62
65
nullnull 67
Delete(69) Reassemble.
120
125
80
90
80
LR
75
60
65
62
65
nullnull 67
Delete(69) Reassemble.
LR
null 6760
65
62
65
null
120
125
80
90
80
75
null null67
Delete(69)
Original Tree
120
125
60
69
80
90
65
6762
75
67
60
75
62 120
125
80
90
80
67
65
Bottom-up Splay Tree
67
60
65
62
6565 120
125
80
90
80
75
67
Top-Down Splay Tree
Amortized Cost
Search O(logn)
Insert O(logn)
Delete O(logn)
References
Daniel Dominic Sleator, Robert Endre Tarjan, “Self-Adjusting Binary Search Trees” ,Journal of the
Association for Computing Machinery, Vol. 32, No. 3, July 1985, pp. 652-686.
Mark Allen Weiss, “Data Structures and Algorithm Analysis in C (2nd Edition)”, Addison-Wesley, 1995 .
Cost of zig case
AcT = 1 (cost of single rotation)
Pi = r '(x)+ r '(p)
Pi-1= r (x)+ r (p)
AmT = AcT + (Pi – Pi-1 ) since only x and p can change rank
AmT = 1 + r '(x)+ r '(p) - r (x) - r (p) since r (p) ≥ r '(p)
AmT ≤ 1 + r '(x)+ r '(p) - r (x) - r '(p)
AmT ≤ 1 + r '(x) - r (x)
since r '(x) ≥ r (x)
AmT ≤ 1 +3( r '(x) - r (x))
Amortized Analysis
x
a b
p
c
c
p
b
X
a
Original Tree Final Tree
Back
Cost of zig-zig case
AcT = 2 (cost of double rotation) Pi = r '(x) + r '(p) + r '(g) Pi-1= r (x) + r (p) + r (g)
AmT = AcT + (Pi – Pi-1 ) since only x, p and g can change rank AmT = 1 + r '(x)+ r '(p) - r (x) - r (p) since r '(x) = r (g) AmT = 2 + r '(x)+ r '(p) + r '(g) - r (x) - r (p) - r (g) AmT = 2 + r '(p) + r '(g) - r (x) - r (p)
since r (x) ≤ r (p) AmT ≤ 2 + r '(p) + r '(g) - r (x) - r (x) AmT ≤ 2 + r '(p) + r '(g) - 2 r (x)
Amortized Analysis
Original Tree Final Tree
x
a b
p
c
g
d
x
a bp
ag
dc
b
NextBack
Cost of zig-zig case
Since r '(x) ≥ r '(p)
AmT ≤ 2 + r '(x) + r '(g) - 2 r (x) —— (1) from figure s (x) + s '(g) ≤ s '(x) from lemma log s (x) + log s '(g) ≤ 2 log s '(x) -2 by definition of rank r (x) + r '(g) ≤ 2 r '(x) - 2
Putting the value in equation 1
AmT ≤ 2 + r '(x) + r '(g) – 2 r (x)
AmT ≤ 2 + r '(x) + r '(g) + r (x) – 3 r (x)
AmT ≤ 2 + r '(x) + ( 2 r '(x) - 2 ) – 2 r (x)
AmT ≤ 3( r '(x) - r (x) )
Amortized Analysis
Original Tree Final Tree
x
a b
p
c
g
d
x
a bp
ag
dc
b
Back
Cost of zig-zag case
AcT = 2 (cost of double rotation) Pi = r '(x) + r '(p) + r '(g) Pi-1= r (x) + r (p) + r (g)
AmT = AcT + (Pi – Pi-1 ) since only x, p and g can change rank AmT = 2 + r '(x)+ r '(p) + r '(g) - r (x) - r (p) - r (g)
since r '(x) = r (g) AmT = 2 + r '(x)+ r '(p) + r '(g) - r (x) - r (p) - r (g) AmT = 2 + r '(p) + r '(g) - r (x) - r (p)
since r (x) ≤ r (p) AmT ≤ 2 + r '(p) + r '(g) - r (x) - r (x) AmT ≤ 2 + r '(p) + r '(g) - 2 r (x) —— (1)
Amortized Analysis
Original Tree Final Tree
x
b c
p
d
g
ag
a b
x
ap
dc
Back Next
Cost of zig-zag case
from figure s '(p) +s '(g) ≤ s '(x) from lemma log s '(p) + log s '(g) ≤ 2 log s '(x) -2 by definition of rank r '(p) + r '(g) ≤ 2 r '(x) - 2
Putting the value in equation 1
AmT ≤ 2 + r '(p) + r '(g) – 2 r (x)
AmT ≤ 2 + ( r '(x) - 2 ) – 2 r (x)
AmT ≤ 2( r '(x) - r (x) )
since 2( r '(x) - r (x) ) ≤ 3( r '(x) - r (x) )
AmT ≤ 3( r '(x) - r (x) )
Amortized Analysis
Original Tree Final Tree
x
b c
p
d
g
ag
a b
x
ap
dc
Back