Professional Documents
Culture Documents
Introduction
• Invented by Daniel Sleator and Robert Tarjan in 1985.
• Self-adjusting BST that combines all normal BST
operations with one basic operation, i.e. splaying.
– Placing recently accessed element at the root of the tree.
• Splaying can be done using either of the two
algorithms.
– Top-down or Bottom-up.
• Main property: Recently accessed elements are quick
to access again.
• Performs basic operations in O(log n) amortized time.
• Particularly useful for implementing caches and
garbage collection algorithms.
The main idea is….
• Balanced BST
– Require storage of an extra piece of information per
node.
– Complicated to implement.
– Have identical worst-case, average-case, and best-case
performances.
• Make second access to the same piece of data
cheaper than the first.
• The 90-10 rule: Empirical studies suggest that in
practice 90% of the accesses are to 10% of the data
items.
Splaying
4
2 5
1 3 4
3 5
2
1 3
2 4
1 5
Contd…
• Zig (Single rotation)
P N
N Left rotation P
1 3
2 3
Right rotation 1 2
• Zig-Zag (Double rotation)
G P
N
P G
4 P G 1
N N
1 4
1 2 3 4
2 3 2 3
Contd…
• Zig-Zig
G N
P P
4 1
N G
3 2
1 2 3 4
Example – Splay at 'c'
h
f i
b g
h
a e
f i
d Zig-Zig
b g
c
a c
d
e
Contd…
h
f i
b g
a c Zig-Zag h
d c i
e b f
a d g
e
Contd…
h
c i
b f Zig
c
a d g
b h
e
a f i
d g
e
Bottom-Up Splay
splay(node *x)
1. { while( x->parent )
2. { if( !x->parent->parent )
3. { if( x->parent->left == x )
4. right_rotate( x->parent )
5. else
6. left_rotate( x->parent ) }
7. else if(x->parent->left == x &&
x->parent->parent->left == x->parent )
8. { right_rotate( x->parent->parent )
9. right_rotate( x->parent ) }
Contd…
10. else if( x->parent->right == x &&
x->parent->parent->right == x->parent )
11. { left_rotate( x->parent->parent )
12. left_rotate( x->parent ) }
13. else if( x->parent->left == x &&
x->parent->parent->right == x->parent )
14. { right_rotate( x->parent )
15. left_rotate( x->parent ) }
16. else
17. { left_rotate( x->parent )
18. right_rotate( x->parent ) } } }
Operations
• Insertion
– Splay at the newly inserted node.
• Searching
– Successful: Splay at the node being searched.
– Unsuccessful: Splay at the node accessed just before
reaching the NULL pointer.
• FindMin
– Splay at the minimum node.
• FindMax
– Splay at the maximum node.
Contd…
• DeleteMin
– FindMin.
– Use the right child as the new root and delete the node
containing the minimum.
• DeleteMax
– FindMax.
– Use the left child as the new root and delete the node
containing the maximum.
• Deletion
– Splay at the node to be deleted.
– Delete the root leaving two subtrees L (left) and R (right).
– Find the largest element in L using a FindMax.
– Make R the right child of L’s root.
Example – Insertion (Bottom-up)
• 9, 2, 90, 53, 4, 64, 95, 59
Insert 9 Insert 2 Zig Insert 90 Zig-Zig
9 9 2 2 90
2 9 9 9
90 2
Insert 53 Zig-Zag
90 53
9 9 90
2 53 2
Contd…
Insert 4 Zig-Zag Zig
53 53 4
9 90 4 90 2 53
2 2 9 9 90
Insert 64 Zig-Zag
4 4 4
2 53 2 64
9 90 53 90
64 9
Contd…
Zig Insert 95 Zig-Zig
64 64 95
4 90 4 90 90
2 53 2 53 95 64
9 9 4
2 53
9
Contd…
Insert 59 Zig-Zig
95 95
90 90
64 64
4 59
2 53 53
9 59 4
2 9
Contd…
Zig-Zig Zig
95 59
59 53 95
53 64 4 64
4 90 2 9 90
2 9
Example – Successful Searching (Bottom-up)
• Search 80.
50
30 60
10 40 90
20 70 100
15 80
Contd…
Example – Un-successful Searching (Bottom-up)
• Search 80.
50
30 60
10 40 90
20 70 100
15
Contd…
Zig-Zag
50
Zig
30 70 70
10 40 60 90 50 90
20 100 30 60 100
15 10 40
20
15
50
Example – FindMin
10
(Bottom-up) 30 60
10 40 90
30
20 70 100
20 50
15 80
15 40 60
90
Zig-Zig
70 100
80
Example – FindMax (Bottom-up)
50
30 60
10 40 90
20 70 100
15 80
Zig-Zig
50
Contd…
30 100
10 40 90
100
Zig
20 60
50
15 70
30 90
80
10 40 60
20 70
15 80
50
Example – DeleteMin
(Bottom-up) 30 60
10
10 40 90
30
20 70 100
20 50
15 80
15 40 60
90
FindMin
70 100
80
Contd…
• Make right child of old root the new root and delete
the old root. 30
20 50
15 40 60
90
70 100
80
50 Example – DeleteMax
30 60 (Bottom-up)
10 40 90
100
20 70 100
50
15 80
30 90
10 40 60
FindMax
20 70
15 80
Contd…
• Make left child of old root the new root and delete
the old root.
50
30 90
10 40 60
20 70
15 80
Example – Deletion (Bottom-up)
• Delete 30
80
30 90
10 50 100
20 40 60
15 70
Delete 30
Contd… 10 80
20 50 90
• Search 30
15 40 60 100
30
70
10 80
20 50 90
Delete 30
15 40 60 100
70
Contd…
FindMin in R FindMax in L
Contd…
FindMin in R FindMax in L
• Join • Join
Top-Down Splay Tree
• Basic idea
– While descending in search for a node, move the
nodes on the access path and their subtrees out
of the way.
• At any point in the middle of a splay there are three
subtrees.
• Let splay is performed for a node X.
– Part of BST that may contain node X.
– Tree L with nodes less than X.
– Tree R with nodes larger than X.
Contd…
• Initially, the three subtrees are
– The complete BST.
– Empty L and R subtrees.
P X
L R L R
X
3 1 2 P
1 2
3
P X
L R L R
X
3 P 2 1
2 1
3
G X
L R L R
P
4 1 2 P
X
3 G
1 2
3 4
G X
L R L R
P
4 P 2 1
X
3 G
2 1
4 3
G P
L R L R
P X
4 1 G
X
1 2 3
4
2 3
G P
L R L R
P X
4 1
G
X
1 3 2
4
3 2
X
X
L R
L R
1 2
1 2
Example – Splay at 'c'
EMPTY h EMPTY
f i Zig-Zig
b g
a e
d EMPTY b f
c a e h
d g i
c
Contd…
EMPTY b f
a e h Zig-Zag
d g i
c
b e f
a d h
c g i
Contd…
b e f
a d h Zig-Zig
c g i
b c f
a d h
e g i
Contd…
b c f
a d h
e g i
c
Reassemble b f
a d h
e g i
Top-Down Splay
Tree * splay (int i, Tree * t)
l r
1. {Tree N, *l, *r, *y;
N
2. if (t == NULL)
3. return t; NULL NULL
4. N.left = N.right = NULL;
5. l = r = &N;
30 90
10 50 100
20 40 60
15 70
Contd…
Zig
EMPTY 30 80
10 50 90
20 40 60 100
15 70
Contd…
Reassemble 30
10 80
20 50 90
15 40 60 100
70
Example – Un-successful Searching (Top-down)
• Search 19.
EMPTY 12 EMPTY
5 25
20 30
15 24
13 18
16
Contd…
Zig-Zag
12 25 EMPTY
5 20 30
15 24
13 18
16
Contd…
Zig-Zig 12 15 20
5 13 18 25
16 24 30
Zig-Zig 12 18 20
5 15 25
13 16 24 30
Contd…
Reassemble
18
12 20
5 15 25
13 16 24 30
Example 3 – Search 30 (Top-down)
EMPTY 80 EMPTY
60 90
50 70 100
30
10 40
20
15
Contd…
Zig-Zig
EMPTY 50 60
30 80
10 40 70 90
20 100
15
Contd…
Zig
EMPTY 30 60
10 40 50 80
20 70 90
15 100
Contd…
Reassemble
30
10 60
20 50 80
15 40 70 90
100
Example – FindMin (Top-down)
EMPTY 50 EMPTY
30 60
10 40 90
20 70 100
15 80
Contd…
Zig-Zig
EMPTY 10 30
20 50
15 40 60
90
70 100
80
Contd…
Reassemble
10
30
20 50
15 40 60
90
70 100
80
Example – DeleteMin (Top-down)
• FindMin as in the
30
previous slides.
• Make right child of 20 50
old root the new
root and delete the 15 40 60
old root.
90
70 100
80
Example – FindMax (Top-down)
EMPTY 50 EMPTY
30 60
10 40 90
20 70 100
15 80
Contd…
Zig-Zig
60 90 EMPTY
50 70 100
30 80
10 40
20
15
Contd…
Zig-Zig
60 100 EMPTY
50 90
30 70
10 40 80
20
15
Contd… 100
Reassemble
60
50 90
30 70
10 40 80
20
15
Example – DeleteMax (Top-down)
• FindMax as in the 60
previous slides.
• Make left child of 50 90
old root the new 30 70
root and delete the
old root. 10 40 80
20
15
Example – Deletion (Top-down)
• Delete 7. First search 7.
EMPTY 11 EMPTY
4 12
2 6 13
1 3 5 9 14
7 10
Zig-Zag
EMPTY 4 11
Contd…
2 6 12
1 3 5 9 13
7 10 14
Zig-Zig
6 9 11
4 7 10 12
2 5 13
1 3 14
Zig
Contd… 6 7 11
4 9 12
2 5 10 13
1 3 7 14
Reassemble 6 11
4 9 12
2 5 10 13
1 3 14
Contd…
• Delete 7.
6 11
4 9 12
2 5 10 13
1 3 14
Contd…
FindMin in R FindMax in L
Contd…
FindMin in R FindMax in L
• Join • Join
Insertion (Top-Down)
• If root == NULL.
– Insert new node with value i.
• Else
– Splay at value, i.e. i, to be inserted.
– If i is less than key at root.
• Create a new node with value i.
• Right of new node = root.
• Left of new node = Left of root.
• Left of root = NULL.
– Else if i is greater than key at root.
• Create a new node with value i.
• Left of new node = root.
• Right of new node = Right of root.
• Right of root = NULL.
Example – Insertion (Top-Down)
• 9, 2, 90, 53, 4, 64, 95, 59
Insert 9 Insert 2 9>2 Insert 90 9 < 90
9 Splay 2 9 2 Splay 90 9 90
9 2 9
2
Insert 53 EMPTY Reassemble
Splay 53
9 90 9 9 < 53 53
Zig-Zag
2 2 90 9 90
2
Contd…
EMPTY
Insert 4
2 9 Reassemple
2 2<4 4
Splay 4
Zig-Zig
53 9 2 9
90 53 53
90 90
Insert 64 9 EMPTY
Splay 64
53 Zig-Zag
9 EMPTY
Zig-Zig 90
4 90
4 53
2
2
Contd… 90 64
Reassemble 90 > 64 Insert 95
9 9 90 Splay 95
Zig-Zig
4 53 4 53
2 2 90
EMPTY EMPTY
64
4 53
2
Contd…
Reassemble 90 < 95
90 95
64 90
9 64
4 53 9
2 4 53
2
Contd…
Insert 59 EMPTY
Splay 59 64 90
Zig-Zig
9 95
4 53
Zig-Zag EMPTY 9 90
4 53 64 95
2
Contd…
Zig-Zig Reassemble
EMPTY 53 90 53
9 64 95 9 90
4 4 64 95
53 < 59 59
2 2
53 90
9 64 95