Escolar Documentos
Profissional Documentos
Cultura Documentos
CSCI 104
Splay Trees
Mark Redekopp
Sources / Reading
Material for these slides was derived from the
following sources
https://www.cs.cmu.edu/~sleator/papers/selfadjusting.pdf
http://digital.cs.usu.edu/~allan/DS/Notes/Ch22.pdf
http://www.cs.umd.edu/~meesh/420/Notes/MountNotes
/lecture10-splay.pdf
Splay Operation
R
T
If we search for or
insert T
T
R
Principle of Locality
2 dimensions of this principle: space & time
Spatial Locality Future accesses will likely cluster
near current accesses
Instructions and data arrays are sequential (they are all
one after the next)
Splay Cases
1.
3.
Zig-Zig
X
a
2.
Root/Zig Case
(Single Rotation)
R
G
a
d
c
b
Leftrotate of X,R
b
a
Right rotate of X,R
Zig-Zag
P
a
1
a
X
c
G
a
G
b
1
d
X
b
2
G
d
c
P
b
Find(1)
6
6
5
7
4
Zig-Zig
Zig
2
2
Zig-Zig
3
3
1
6
Resulting
Tree
4
2
7
5
Find(3)
1
1
Zig-Zag
6
4
2
3
Zig-Zag
7
5
7
4
6
2
7
5
Resulting
Tree
Worst Case
Suppose you want to make the amortized time
(averaged time over multiple calls to
find/insert/remove) look bad, you might try to
always access the ______________ node in the tree
Deepest
10
Insert(11)
20
20
30
12
15
5
3
10
20
30
12
25
15
5
3
25
11
3
Zig-Zig
12
20
8
Resulting Tree
8
Zig-Zig
11
25
11
10
15
10
10
30
12
15
30
25
11
Insert(4)
20
30
12
15
5
3
20
20
25
15
5
3
10
8
15
25
3
10
30
12
30
12
5
10
8
8
Zig-Zig
Zig-Zag
4
3
12
20
5
10
Resulting Tree
15
30
25
25
12
Activity
Go to
https://www.cs.usfca.edu/~galles/visualization/SplayTree.
html
Try to be an adversary by inserting and finding elements
that would cause O(n) each time
13
Find(x)
Attempt normal BST find(x) and splay last node visited
If x is in the tree, then we splay x
If x is not in the tree we splay the leaf node where our search ended
FindMin(), FindMax()
Walk to far left or right of tree, return that node's value and then splay that
node
DeleteMin(), DeleteMax()
Perform FindMin(), FindMax() [which splays the min/max to the root] then
delete that node and set root to be the non-NULL child of the min/max
Remove(x)
Find(x) splaying it to the top, then overwrite its value with is
successor/predecessor, deleting the successor/predecessor node
14
FindMin() / DeleteMin()
FindMin()
20
20
20
30
12
30
5
15
5
3
25
30
25
25
12
12
10
10
15
10
15
8
8
Zig-Zig
DeleteMin()
Zig
Resulting Tree
20
20
30
10
8
15
25
12
25
12
30
10
15
8
Resulting Tree
15
Remove(3)
1
1
Zig-Zag
6
4
2
6
7
6
2
Zig-Zag
6
4
Resulting
Tree
6
2
5
Copy successor or
predecessor to root
7
5
7
5
Delete successor
(Remove node or
reattach single child)
16
17
Top-Down Splaying
1. Zig (If Target is in 2nd level)
T
Root
Root
b
reach Target)
T
a
L
b
R
R
a
18
3.
Top-Down Splaying
Zig-Zig
X
Y
L
Y
X
c
c
Zig-Zag
Y
L
R
Z
a
4.
L
Z
R
Y
L
c
b
a
19
Find(3)
L-Tree
R-Tree
L-Tree
Steps taken on
our journey to
find 3
R-Tree
1
7
6
4
R-Tree
L-Tree
7
5
5
3
Zig-Zag
X
a
X
Z
Y
Z
L
c
R
X
L
Y
b
a
L
Z
R
Y
b
a
20
Find(3)
R-Tree
L-Tree
1
7
2
5
5
Resulting tree after find
reach Target)
T
a
L
b
R
R
a
6
2
5
Resulting tree from
bottom-up approach
21
Insert(11)
L-Tree
R-Tree
L-Tree
20
15
12
30
12
R-Tree
25
20
10
15
30
25
10
8
L-Tree
10
11
10
20
8
Original Resulting
Tree from Bottomup approach
11
12
12
R-Tree
15
30
25
20
8
15
30
25
22
Summary
Splay trees don't enforce balance but are selfadjusting to attempt yield a balanced tree
Splay trees provide efficient amortized time
operations
A single operation may take O(n)
m operations on tree with n elements => O(m(log n))