Вы находитесь на странице: 1из 166

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 heuristic


Move to the root
Rotation
Rotation plays an important role in Splay
trees.
Types of rotation:
 Left-Rotation
 Right-Rotation
Left-Rotation

P
c x

c x
b a

b a

b b
Right-Rotation

P
P

x c
x c

a b
a b

b
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.
Example — Search(p1)
 Search(p1)
p5

p4 g

p3 f

p2 e

b
p1

c d

c
Example — Search(p1)
 Perform single rotation bottom-up.
p5

p4 g

p3 f

p2 e

b
p1

c d

c
Example — Search(p1)
 Perform single rotation bottom-up.
p5

p4 g

p3 f

p2 e

bp2
p1

c
p2 d

c
b c
Example — Search(p1)
 Perform single rotation bottom-up.
p5

p4 g

p3 f

p1 e

p2 d

b c
Example — Search(p1)
 Perform single rotation bottom-up.
p5

p4 g

p3 f

p1 e
p3

p2 d
p3

b c d e
d
Example — Search(p1)
 Perform single rotation bottom-up.
p5

p4 g

p1 f

p2 p3

b c d e
d
Example — Search(p1)
 Perform single rotation bottom-up.
p5

p4 g

p1 p4f

p2 p3
p4

b c dp3 ef
d
p3
d e
d
d e
Example — Search(p1)
 Perform single rotation bottom-up.
p5

p1 g

p2 p4

b c p3 f

d e
d
Example — Search(p1)
 Perform single rotation bottom-up.
p5

p1 g

p2 p4
p5

b c p3 f

p4e
d
d
p3 f

d e
Example — Search(p1)
p5
p1
p4 g

p2 p5
p3 f
p2 e
b p4 g
c
b p1
c d p3 f
c
Original Tree
d e
d
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.
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 Case
Zig-Zag Case
Steps for Splay…
ZIG CASE
If x is the left child of p perform right rotation around p.

x c
p

Right-Rotation
x c a b

a b

b
Steps for Splay…
ZIG CASE (symmetric)
If x is the right child of p perform left rotation around p.

p c x

Left-Rotation
c x b a

b a

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.
g p

p x ag
d
g
p
x b c d
p d a g a
c

x c b
a b

a b
c c
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. p

g
g x
g a p
a d
b p
c

a bp bg a
x

b ax c
c d

c d
b
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.
g
g

g x
a
a P

a p b
g ap
x p
d

c d
x d
P
b c b

b c
c c d
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.
g
g

g x d
p d

p d p
pa a g
x c

a x b
a
pb c
b
b c
a c
b
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.
Example Search(p1) — Using Splay
 Search(p1).
 Search is successful. p5

p4 g

p3 f

p2 e

b
p1

c d

c
Example Search(p1) — Using Splay
 Perform splay bottom-up from p1.

p5

p4 g

p3 f

p2 e

b zig-zag
p1

c d
Example Search(p1) — Using Splay
 Perform splay bottom-up from p1.
p5

p4 g

p3 f

p2 e

bp2 zig-zag
p1

c
p2 d

c
b c
Example Search(p1) — Using Splay
 Perform splay bottom-up from p1.
p5

p4 g

p3 f

p1 e

d zig-zag
p2

b c
Example — Search(p1)
 Perform single rotation bottom-up.
p5

p4 g

p3 f

p1 e
p3

p2 d
p3

b c d e
d
Example Search(p1) — Using Splay
 Perform splay bottom-up from p1.
p5

zig-zig
p4 g

p1 f

p2 p3

b c d e
d f
Example Search(p1) — Using Splay
 Perform splay bottom-up from p1.
p5

zig-zig
p4 g

p1 p5
f

p2 p3

b c d e
d f
Example Search(p1) — Using Splay
 Perform splay bottom-up from p1.

zig-zig
p4

p5
p1

p4 f g
p2 p3

b c d e
d
p3

d e
d
Example Search(p1) — Using Splay
p5

p4 g p1

p3 f p4
p2
p2 e p5
b b p3
p1 c
f g
c d
d e
c
Original Tree d

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.
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
16

9 31
31

5 13 20 37
37

35
35 40

36
Insert(36)
 Perform splay bottom-up from 36.

16

9 31

5 13 20 37
zig-zag

35
35 40

35
36

35
Insert(36)
 Perform splay bottom-up from 36.

16

9 31

5 13 20 37
37
zig-zag

36 40

35
Insert(36)
 Perform splay bottom-up from 36.

16

9 31

5 13 20 37
37
zig-zag

37
36 40

37
37
35

40
Insert(36)
 Perform splay bottom-up from 36.

16
16
zig-zig

31
31
9

20 36
5 13

37
37
35

40
Insert(36)
 Perform splay bottom-up from 36.

16
16
zig-zig

31
31
9

20 36
5 13

37
37
35

40

20
Insert(36)
 Perform splay bottom-up from 36.

31
31 zig-zig

16
16 36

3135 37
37
9 20

40
5 13

35
Insert(36)
16 36

9 37
37
31 31
31

16
16 40
5 13 20 37 35

35 40 9 20

36 5 13

Original Tree
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
16

9 31
31

5 13 20 37
37

35
35 40

36
Delete(36)
 Perform splay bottom-up from parent of 36 i.e. 35.

16
zig-zag
9 31

5 13 20 37
37

35
35 40
Delete(36)
 Perform splay bottom-up from parent of 36 i.e. 35.

16
zig-zag
9 31

5 13 20 37
37

37
35
35
35 40

37
37

40
Delete(36)
 Perform splay bottom-up from parent of 36 i.e. 35.

16
zig-zag
9 31
31

5 13 35
35
20

37
37

40
Delete(36)
 Perform splay bottom-up from parent of 36 i.e. 35.

16
zig-zag
9 31
31

5 13 35
35
3120
37
37
31
31

40
20
Delete(36)
 Perform splay bottom-up from parent of 36 i.e. 35.

16
16 zig

9 35
35

37
37
5 13 31
31

40
20
Delete(36)
 Perform splay bottom-up from parent of 36 i.e. 35.

16
16 zig

9 35
35

16 37
37
5 13 31
31

40
20
31
31

20
Delete(36)
16
31
35
35
9 31
16
16 37
37

5 13 20 37
9 31
31 40

35 40
5 13 20

36
Original Tree
Delete(16)
 Search(16)
 Element found.
 Delete 16. 35
35

16
16 37

9 31
31 40

delete by copy
5 13 20
Delete(16)
 Search(16)
 Element found.
 Delete 16. 35
35

16 37

9 31
31 40

5 13 20
Delete(16)
 Perform splay bottom-up from parent of 16 i.e. 31.

35
35
zig-zag

20 37

9 31
31 40

5 13
Delete(16)
 Perform splay bottom-up from parent of 16 i.e. 31.

35
35
zig-zag

20 37

20
9 31
31 40

5 20
13

5 13
Delete(16)
 Perform splay bottom-up from parent of 16 i.e. 31.

35
35
zig-zag

31 37

20 40

5 13
Delete(16)
 Perform splay bottom-up from parent of 16 i.e. 31.

35
35
zig-zag

31 37

35
20 40

5 13
Delete(16)
35
35
31
35

16 37 20 35

9 31
31 40 9 37

5 13 20 5 13 40

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

30 80

20 80
50

20
40 60

39 43

35
Split (at 40)
 Perform splay bottom-up from 40.

70
zig-zag
30 80

20 80
50

20
40 60

39 43

35
Split (at 40)
 Perform splay bottom-up from 40.

70
zig-zag
30 80

20 50

20
40 60
50

39 43
50

35 43 60

43
Split (at 40)
 Perform splay bottom-up from 40.

70
zig-zag
30 80

20 20
40

39 50

35 43 60
Split (at 40)
 Perform splay bottom-up from 40.

70
zig-zag
30 80

3020
40
20

39
30 50

35
20 3943 43 60

39
35

35
Split (at 40)
 Perform splay bottom-up from 40.

70
zig
80
20
40

30 5070

20 39 43 60

35
50

43 60
Split (at 40)
 Perform split at 40.

20
40

30 70

20 80
39 50

35 43 60
Split (at 40)
70
20
40
70
30 80
30 80
50
20 80
50
20 39
20
40 60 43 60

39 43 35

35
Original Tree
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) = iΣЄ Tlog s(i)

where s(i) = Number of descendants of i + i itself.


r(i) = log s(i) (Rank of node i)
Then Ф(T) = i ЄΣ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.
Amortized Analysis
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 p X

x c a p
AcT = 1 (cost of single rotation)
a b b c
Pi = r '(x)+ r '(p) Original Tree Final Tree
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)
For more detail
Amortized Analysis
 Cost of zig-zig case
g x

AcT = 2 (cost of double rotation) p d


a bp
x c
Pi = r '(x) + r '(p) + r '(g) b ag
Pi-1= r (x) + r (p) + r (g) a b c d
Original Tree Final Tree
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
g x
 Cost of zig-zig case
p d
a bp
x c b ag

a b c d
Original Tree Final Tree
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)
For more detail
AmT ≤ 3( r '(x) - r (x) )
Amortized Analysis
 Cost of zig-zag case g x

a p
AcT = 2 (cost of double rotation) g ap
x d
Pi = r '(x) + r '(p) + r '(g) a b c d
b c
Pi-1= r (x) + r (p) + r (g) Original Tree Final Tree

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
 Cost of zig-zag case g x
a p
g ap
x d
a b c d
b c
Original Tree Final Tree

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


(x) more
) ≤ 3(detail
r '(x) - r (x) )
Amortized Analysis

Since the last splaying step leaves x at the root T.

The amortized bound of entire splay is

3( r '(T) - r (x)) + 1 = O( log N)


Top-Down Splay Trees
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.
Steps for Splay
Partition the binary search tree into three components.
x
 Original Tree L R
 Right Tree (R) null null
c r null null

 Left Tree (L)


p q

L and R are initially empty. They are reassembled


with the new root of the original tree in the end.
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 Case
Zig-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.

x R L R
L x
null null null
null null null
null null
c r c r

p q p q
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
L x R x
null null null null
null null null null

p c p c

q r q r
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 R
x
L R c
null null null null

null null c s null null

gc x
gc r
q r s
p

p q
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 R
L x R
null null null null

null null null null c


p bc

x agc
q agc

q r s
s p
r
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 X R
L X R
null null null null

null null null null p C


p C

gc
gc s
s

q r
q r
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.

x L R
L r x
null null null null
null null null null
c s
c s

p gc
p gc

q r
q r
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.

12
30

5 80
50
25

20 30

15 24

13 18

16
Search(19)
 Search(19) by performing splay.

12
30
12

L R
5 80
50
25
25

null null 20
20 30 null null

15 24

13 18

16
Search(19)
 Search(19) by performing splay.

zig-zag
12
30
12

L R
5 80
25
50
25

12
null null 20
40
20 30 null null

15 24

13 18

16
Search(19)
 Search(19) by performing splay.

zig-zig 80
25
50
25
L R
40
20
20 30

null 12
30
12 null null
15
15
24
5
13 18

16

24
Search(19)
 Search(19) by performing splay.

L R
zig-zig

null 12
30
12 40
20
20 null
20 null

5 15
80
25
50
25 25
13 18
24 30

16
Search(19)
Search(19)
 Element
 not by performing
found . splay.

zig
15
15
L R
13 18
18

null 12
30
12 40
20
20 null
16
null
5 15
80
25
50
25

24 30
Search(19)
 Reassemble.

18
18
L R
16

null 12
30
12 40
20
20 null

5 15
15
80
25
50
25

13
24 30
Search(19)
 Reassemble.

18
18
L R

null 12
30
12
null 20
40
20
null null

5 15
15
80
25
50
25

13 16 24 30
Search(19)
12
30

18 20
18
5 50
80
25
40
20 25
12
30 20 30 12

5 15 80
25
50 15 24 5 15 20

13 24 30 13 18
16 13 16

16
Bottom-Up Splay Tree
Top-Down Splay Tree
Original Tree
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 80

50

30 60

10 40 90

20 70 100

65
Insert(80)
 Search(80) by performing splay. 80

50
L R
zig-zig
30 60
60
null null null null

10 40 90
90

20 70 100

65
Insert(80)
 Search(80) by performing splay. 80

50
L R
zig-zig
30 60
null null null null

10 40 90

20 70 100

65
Insert(80)
 Search(80) by performing splay. 80

L R
zig-zig
60
60

null null null


60
null 50
90
50 30

70 100
10 40

65
20
Insert(80)

 Search(80)
Element not by performing
found . splay. 80

L 90 zig R

70
70 100
null 60
60 null null

50
65 null

30

10 40

20
Insert(80)
 Search(80)
 Element by performing
not found . splay. 80

L 90 zig R

70 100
null 60
60 null
90 null

50
65

30

10 40

20
Insert(80)
 Insert 80. 80
 70 < 80
70
L R

60
60 65 90
90
null null

50
100
30

10 40

20
Insert(80)
 Attach 70 and its left sub tree to the left of 80.
80

70 70
L R

null 60
60 65 90
90 null

50
100
30

10 40

20
Insert(80)
 Reassemble. 80

L 70
70 R

null 60
60 65 90
90 null

50
100
30

10 40

20
Insert(80)
 Reassemble.

L 80 R

null null
60
60 null
90
90 null

50
70
70 100
30

65
10 40

20
Insert(80)
80 80
50
50

60 90 30 60
60 20
60 90

50 50 70 100
70 10 40 90
90
100
30 65
30 20 70 100
10 40
65
10 40 65 85
20
Bottom-Up Splay Tree
20 Top-Down Splay Tree Original Tree
Insert(33)
 Create a node 33. 33

16
16

9 31
31

37
37
5 13 20

35 40

36
Insert(33)
 Search 33 by performing splay. 33

16
16
L zig-zig R

9 31
31
null null null null
37
37
5 13 20

35 40

36
Insert(33)
 Search 33 by per forming splay.
33

16
16
L zig-zig R

9 31
31
null null null null
37
37
5 13 20

35 40

36

20
Insert(33)
 Search 33 by per forming splay.
33

L zig-zig R

31
31
null 31
null null null
37
37
16
16
16
35 40
9 20

36
5 13
Insert(33)
 Search
Element 33found
not by per
. forming splay.
33

L zig R
37
37

null 31
31 null null
35
35 40

16
16
36
?

9 20

5 13
Insert(33)
 Search 33 by per forming splay.
33

L zig R
37
37

null 31
31 null
37 null
35
35 40

16
16
36

9 20

5 13
Insert(33)
 Insert 33.
33
 35 > 33

L 35 R

31
31 36 37
37 null
null

40
16
16

9 20

5 13
Insert(33)
 Attach 35 and its right sub tree to the right of 33.
33

L 35 35 R

31
31 36 36 37
37 null
null

40
16
16

9 20

5 13
Insert(33)
 Reassemble.
33

L 35 R

31
31 36 37
37 null
null

40
16
16

9 20

5 13
Insert(33)
 Reassemble.

33 R
L

31
31
null null null
null 37
37

16
16 40
35

9 20
36

5 13
Insert(33)

33 16
16 33

31 31 37
35
37
9 31
31

40 16 37
16 37
37
35 5 13 20
36 40
9 20
9 20 36 35 40

5 13
5 13 36
Original Tree Bottom-Up Splay Tree
Top-Down Splay Tree
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.

45

29 50

15 40 85

20 70 95

80

75 83
Delete(70)
 Search(70) by performing splay.

45
L R
zig-zig
29 50
50
null null null null

15 40 85
85

20 70 95

80

75 83
Delete(70)
 Search(70) by performing splay.

45
50
L R
zig-zig
29 50
60
null null null null

15 40 90
85

20 70 95

80

75 83
Delete(70)
 Search(70) by performing splay.

L R
zig-zig
50
60
null null
null 50
null 45
50
90
85
45 29

70 95
15 40

80
20

75 83
Delete(70)
 Element found.

L zig R
90
85

50
60 70
70 95 null null
null
45
50
80
29
75 83

15 40

20
Delete(70)
 Element found.

L zig R
90
85
Empty
70
70 95 85 null
50
60 null
null
45
50
80
29
75 83

15 40

20
Delete(70)
 Left child of 70 is null.
 Make its right child the new root.

70
70
L R

null 80
50
60 90
85 null
null
45
50 75 83
95
29

15 40

20
Delete(70)
 Delete 70.

70
70 80
L R

75 83
50
60 90
85 null
null
45
50
95
29

15 40

20
Delete(70)
 Reassemble

80 R
L

75 83
50
60 90
85 null
null
45
50
95
29

15 40

20
Delete(70)
 Reassemble

80 R
L

50
60
null 90
85
null null
null
45
50 75
83 95
29

15 40

20
Delete(70)
80 45
80
29 50
50
60 90
85 43
85
45
50 15 40 85
75
83 95
20 29 75 83 95
29 70 95

15 40
80
15 40
75 83 20
20 Top-Down Splay Tree Bottom-Up Splay Tree
Original Tree
Delete(40)
 Search(40) by performing splay.

55

35 72

22 40 59 84
Delete(40)
 Search(40) by performing splay.
 Element found.
zig-zag
55
L R

35
35 72
null null null null

22 40
40 59 84
Delete(40)
 Search(40) by performing splay.
 Element found.

zig-zag
55
L R

35
35 72
null null 55
null null
22 40
40 59 84
Delete(40)
 Search(40) by performing splay.
 Element found.

zig
35
35
L R

22 40
55
null null35 null

72

59 84
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.
40
L R

null 35
35 55
null null
null

72
22

59 84
Delete(40)
 Delete 40.

40
L R

55
null null null null
35
35 72

22 59 84
Delete(40)

55 35
55

22 55
35 72
35 72

72
22 59 84 22 40 59 84
59 84

Top-Down Splay Tree Original Tree Bottom-Up Splay Tree


Delete(69)
 Search(69) by performing splay.

80

60 120

69 90 125

65 75

62 67
Delete(69)
 Search(69) by performing splay.
 Element found. zig-zag

80
L
R
60
60 120
null null
null null
69
69 90 125

65 75

62 67
Delete(69)
 Search(69) by performing splay.
 Element found.
zig-zag

80
L
R
60
60 120
null null
80
null null
69
70 90 125

65 75

62 67
Delete(69)
 Search(69) by performing splay.

zig

L 60
R

69
70
null 60
null 80 null

65 75
120

62 67
90 125
Delete(69)
 Left child of 69 is not null.
 Find maximum from left subtree of 69 by performing splay.

69
70
L
R

65 75
null 60 65
80 null
62 67
67
120
L R

90 125
null null null null
Delete(69)
 Left child of 69 is not null.
 Find maximum from left subtree of 69 by performing splay.

69
70
L
R

75
null 60 65
65
zig 80 null

62 67
120
L R

90 125
null null null null
Delete(69)
 Left child of 69 is not null.
 Find maximum from left subtree of 69 by performing splay.

69
70
L
R

75
null 60 65
65
zig 80 null

62 67
120
L R

90 125
null null null null
Delete(69)
 67 is now the new root.

69
70
L
R

75
null 60 67
80 null

67
120
L R

90 125
null 65
65
null null null

62
Delete(69)
 Attach right child of 69 as right child of 67.

69
70
L
R

75
null 60 67
80 null

65
65
120

62
90 125
Delete(69)
 Delete 69.

69
70
L
R

null 60 67
80 null

65
65 75 120

62
90 125
Delete(69)
 Reassemble.

L
R

null 60 67
80 null

65
65 75 120

62
90 125
Delete(69)
 Reassemble.

L
R

null 60 67
null null
80 null

65
65 120
75

62 125
90
Delete(69)
67 80 67
65

60 80 60 120 60 80

120 69 90 125
65
65 75 62 67 120

65 75
62 90 125
67 75 90 125
62
Top-Down Splay Tree Original Tree Bottom-up 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 .
Amortized Analysis
 Cost of zig case p X
x c a p
AcT = 1 (cost of single rotation)
a b b c
Pi = r '(x)+ r '(p)
Original Tree Final Tree
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)


Back
AmT ≤ 1 +3( r '(x) - r (x))
Amortized Analysis
 Cost of zig-zig case g x
p d
AcT = 2 (cost of double rotation) a bp
Pi = r '(x) + r '(p) + r '(g) x c b ag
Pi-1= r (x) + r (p) + r (g)
a b c d
AmT = AcT + (Pi – Pi-1 ) Original Tree Final Tree
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) Back Next
AmT ≤ 2 + r '(p) + r '(g) - 2 r (x)
Amortized Analysis
 Cost of zig-zig case
g x
Since r '(x) ≥ r '(p)
p d
AmT ≤ 2 + r '(x) + r '(g) - 2 r (x) —— (1) a bp
x c b ag
from figure
s (x) + s '(g) ≤ s '(x) a b c d
from lemma Original Tree Final Tree
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) Back
AmT ≤ 3( r '(x) - r (x) )
Amortized Analysis
 Cost of zig-zag case g x

AcT = 2 (cost of double rotation) a p


g ap
Pi = r '(x) + r '(p) + r '(g)
x d
Pi-1= r (x) + r (p) + r (g)
a b c d
b c
Original Tree Final Tree
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) Back Next
AmT ≤ 2 + r '(p) + r '(g) - 2 r (x) —— (1)
Amortized Analysis
 Cost of zig-zag case g x

a p
from figure g ap
s '(p) +s '(g) ≤ s '(x) x d
from lemma
log s '(p) + log s '(g) ≤ 2 log s '(x) -2 a b c d
b c
by definition of rank Original Tree Final Tree
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) ) Back

Вам также может понравиться