Professional Documents
Culture Documents
Splay Trees: 08/10/2021 Data Structures and Algorithms, Dr.V.Durgadevi, ASP 1
Splay Trees: 08/10/2021 Data Structures and Algorithms, Dr.V.Durgadevi, ASP 1
•Zig-zig Zag-zag
•Zig-zag Zag-zig Double Rotation
n n
X Y X Y
Zig root
p
n Z
X Y
X Y
Y Z
n
X p
g p
n W
X Y Z W
Y Z
Zig
p
X
n W
Y Z
X n
p
Y
Z W
Data Structures and Algorithms,
08/10/2021 10
Dr.V.Durgadevi, ASP
Case 3: Access (LL, RR) grandchild:
• Zag-Zag (different from AVL)
g 1 n
2
p p
W
Z
X n g
Y
Y Z
W X
Data Structures and Algorithms,
08/10/2021 11
Dr.V.Durgadevi, ASP
Zig-Zig
•x has a grandparent
z x
10 30
y y
20 20
x z
T1 30 10 T4
T2 T3
T3 T4 T1 T2
z x
10 20
y
30
x z y
T1 20 10 30
T4
T2 T3 T1 T2 T3 T4
T2
T3 T4 T1 T2 T3 T4
• Splay 78 50
17 88
32 65 97
• Zig-Zag
28 54 82
z
29 76
y
80
x 78
Data Structures and Algorithms,
08/10/2021 19
Dr.V.Durgadevi, ASP
Complete Example
44
• Splay 78 50
17 88
32 65 97
• Zig-Zag
28 54 82
29 x
78
z 76 80 y
17 88
32 z 65 97
• Zig-Zag y
28 54 82
29
78 x
76 80
17 88
32 x 78 97
• Zig-Zag
28 z 65 y 82
29 54 76 80
17 88 y
32 x 78 97
• Zig-Zag
28 65 82
29 54 76 80
17 50 z 88 y
82
32 97
65
• Zig-Zag
80
28
54 76
29
• Splay 78 78
x
17 50 88 w
82
32 97
65
• Zag -Zag
80
28
54 76
29
17 50 88 w
• Zag -Zag 82
32 97
65
80
28
54 76
29
2 2
zig-zig
3 3
Find(6)
4 6
5 5
2 6
zig-zig
3 3
6 2 5
5 4
6 zig 1
3 3
2 5 2 5
4 4
Data Structures and Algorithms,
08/10/2021 31
Dr.V.Durgadevi, ASP
Find (4)
6 6
1 1
zig-zag
3 4
Find(4)
2 5 3 5
4 2
1 zig-zag 1 6
4 3 5
3 5 2
2
Data Structures and Algorithms,
08/10/2021 33
Dr.V.Durgadevi, ASP
Why Splaying Helps
• If a node n on the access path is at depth d before the splay, it’s
at about depth d/2 after the splay
– Exceptions are the root, the child of the root, and the node splayed
• Overall, nodes which are below nodes on the access path tend
to move closer to the root
split(x)
splay
T L R
OR
L R L R
x >x <x x
Data Structures and Algorithms,
08/10/2021 37
Dr.V.Durgadevi, ASP
Back to Insert
x
split(x)
L R
<x >x L R
x
find(x) delete x
L R
L R <x >x
Now what?
L
splay
L R R
Join(L,R)
T-x
Data Structures and Algorithms,
08/10/2021 41
Dr.V.Durgadevi, ASP
Insert Example
4 4 6
6
split(5) 1 6 1 9
1 9
2 9 2 7
4 7
7
2 5
4 6
• Insert (5)
1 9
08/10/2021
Data Structures and Algorithms,
Dr.V.Durgadevi, ASP
2 7 42
Delete Example
4 6
6
1 6 1 9
1 9 find(4)
2 9 2 7
4 7 Find max
7
2 2 2
1 6 1 6
• Delete (4) 9 9
08/10/2021 7
Data Structures and Algorithms,
Dr.V.Durgadevi, ASP
7 43
Splay Tree Summary
•Can be shown that any M consecutive operations starting from
an empty tree take at most O(M log(N))