100% found this document useful (1 vote)
159 views

Splay Tree

- A splay tree is a self-adjusting binary search tree that provides fast access to recently accessed elements. It performs basic operations like search, insert, delete in O(log n) amortized time. - During operations, splaying moves the accessed node to the root by performing rotations. This brings frequently accessed nodes closer to the root for faster future access as per the 90-10 rule. - The bottom-up splaying algorithm uses different rotations like zig, zig-zag, and zig-zig based on the position of the accessed node to move it to the root.

Uploaded by

Devinder Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
159 views

Splay Tree

- A splay tree is a self-adjusting binary search tree that provides fast access to recently accessed elements. It performs basic operations like search, insert, delete in O(log n) amortized time. - During operations, splaying moves the accessed node to the root by performing rotations. This brings frequently accessed nodes closer to the root for faster future access as per the 90-10 rule. - The bottom-up splaying algorithm uses different rotations like zig, zig-zag, and zig-zig based on the position of the accessed node to move it to the root.

Uploaded by

Devinder Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Splay Tree

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.

• While descending two levels at a time, nodes are


placed in L or R along with subtrees that are not on
the access path to X, depending on whether the
nodes are smaller or greater than X.
Case 1: Zig

P X
L R L R
X
3 1 2 P
1 2
3

• X should become root.


• P and its right subtree are made left children of the
smallest value in R.
Contd…

P X
L R L R
X
3 P 2 1

2 1
3

• X should become root.


• P and its left subtree are made right children of the
largest value in L.
Case 2: Zig-Zig

G X
L R L R
P
4 1 2 P
X
3 G
1 2
3 4

• The value to be splayed is in the tree rooted at X.


• Rotate P about G and attach as left child of the smallest
value in R.
Contd…

G X
L R L R
P
4 P 2 1
X
3 G
2 1
4 3

• The value to be splayed is in the tree rooted at X.


• Rotate P about G and attach as right child of the largest
value in L.
Case 3: Zig-Zag

G P
L R L R
P X
4 1 G
X
1 2 3
4
2 3

• The value to be splayed is in the tree rooted at X.


• For simplicity, the Zig-Zag rotation is reduced to a single
Zig.
Contd…

G P
L R L R
P X
4 1
G
X
1 3 2
4
3 2

• The value to be splayed is in the tree rooted at X.


• For simplicity, the Zig-Zag rotation is reduced to a single
Zig.
Reassemble

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 l r
N
Tree * splay (int i, Tree * t)
NULL NULL
1. Tree N, *l, *r, *y;
2. if (t == NULL) N
3. return;
4. N.left = N.right = NULL;
R L
5. l = r = &N;

• l points to the maximum node in L.


• r points to the minimum node in R.
Top-Down Splay
6. for (;;)
7. if (i < t->item)
8. if (t->left == NULL) break;
9. if (i < t->left->item)
10. y = t->left; /* rotate right */ l r
11. t->left = y->right; N
12. y->right = t;
NULL
13. t = y;
14. if (t->left == NULL) break;
15. r->left = t; /* link right */
16. r = t;
17. t = t->left; t r
Contd… N

18. else if (i > t->item) t


X
19. if (t->right == NULL) break; R L
20. if (i > t->right->item)
1 2
21. y = t->right; /* rotate left */
22. t->right = y->left;
23. y->left = t; /* reassemble */
24. t = y; 30. l->right = t->left;
25. if (t->right == NULL) break; 31. r->left = t->right;
26. l->right = t; /* link left */ 32. t->left = N.right;
27. l = t; 33. t->right = N.left;
28. t = t->right; 34. return t;
29. else break;
Example 1 – Search 19 (Top-down)

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 18 20
12

5 15 16 25

13 24 30
Contd…
Reassemble
18

12 20

5 15 25

13 16 24 30
Example 2 – Search 30 (Top-down)

EMPTY 80 EMPTY

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 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 – Insertion (Top-Down)
• 9, 2, 90, 53, 4, 64, 95, 59
Insert 9 Insert 2 Zig EMPTY Reassemble
9 9 2 9 2

2 9

Insert 90 Zig-Zig EMPTY Reassemble


2 9 90 90

9 2 9

90 2
Contd…
Insert 53 Zig-Zag EMPTY Zig
90 9 90 9 53 90

9 2 53 2

2 53
Reassemble Insert 4 Zig-Zig EMPTY
53 53 2 9

9 90 9 90 4 53

2 2 90

4
Contd…
Zig
2 4 9
Reassemble 4

53 2 9

Insert 64
4 90 53

2 9 90
Zig-Zig EMPTY
53 9 53

90 4 90

64 2 64
Contd…
Zig-Zag EMPTY Zig
9 90 9 64 90

4 53 64 4 53

2 2

Reassemble 64 Insert 95 64

9 90 9 90

4 53 4 53 95

2 2
Contd…
Zig-Zig EMPTY Reassemble
90 95 95

64 90

9 64

4 53 9

2 4 53

2
Contd…
Insert 59 Zig-Zig EMPTY
95 64 90

90 9 95

64 4 53

9 2 59

4 53

2 59
Contd…
Zig-Zag EMPTY Zig-Zig
9 90 53 59 90

4 53 64 95 9 64 95

2 59 4

2
Contd…
Reassemble
59

53 90

9 64 95

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy