Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

Splay Trees

• In balanced tree schemes, explicit rules are


followed to ensure balance.
• In splay trees, there are no such rules.
• Search, insert, and delete operations are like in
binary search trees, except at the end of each
operation a special step called splaying is done.
• Splaying ensures that all operations take O(lg n)
amortized time.
• First, a quick review of BST operations…
Data Structures and Algorithms,
08/10/2021 1
Dr.V.Durgadevi, ASP
Splaying
• In splay trees, after performing an ordinary
BST Search, Insert, or Delete, a splay
operation is performed on some node x (as
described later).
• The splay operation moves x to the root of the
tree.
• The splay operation consists of sub-operations
called zig-zig, zig-zag, and zig.

Data Structures and Algorithms,


08/10/2021 2
Dr.V.Durgadevi, ASP
Splaying
•Bring the node being accessed to the root of the
tree, when accessing it, through one or more
splay steps.
•A splay step can be:
•Zig Zag Single Rotation

•Zig-zig Zag-zag
•Zig-zag Zag-zig Double Rotation

Data Structures and Algorithms,


08/10/2021 3
Dr.V.Durgadevi, ASP
Splaying Cases
• Node being accessed (n) is:
– the root
– a child of the root
Do single rotation: Zig or Zag pattern
– has both a parent (p) and a grandparent (g)
Double rotations:
(i) Zig-zig or Zag-zag pattern:
g  p  n is left-left or right-right
(ii) Zig-zag pattern:
g  p  n is left-right or right-left

Data Structures and Algorithms,


08/10/2021 4
Dr.V.Durgadevi, ASP
Case 0: Access root

• When the node is root


root root

n n

X Y X Y

Data Structures and Algorithms,


08/10/2021 5
Dr.V.Durgadevi, ASP
Case 1: Access child of root:

• Zig (AVL single rotation) - Demo

Zig root
p

n Z

X Y

Data Structures and Algorithms,


08/10/2021 6
Dr.V.Durgadevi, ASP
Case 1: Access child of root

• Zig and Zag (AVL single rotations)


root root
p Zig – right rotation n

n Z Zag – left rotation p


X

X Y
Y Z

Data Structures and Algorithms,


08/10/2021 7
Dr.V.Durgadevi, ASP
Case 2: Access (LR, RL) grandchild:
• Zig-Zag (AVL double rotation)
g

n
X p

g p
n W

X Y Z W
Y Z

Data Structures and Algorithms,


08/10/2021 8
Dr.V.Durgadevi, ASP
Case 2: Access (LR, RL) grandchild:
• Zig-Zag (AVL double rotation)
g

Zig
p
X

n W

Y Z

Data Structures and Algorithms,


08/10/2021 9
Dr.V.Durgadevi, ASP
Case 2: Access (LR, RL) grandchild:
• Zig-Zag (AVL double rotation)
Zag
g

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

• (Symmetric case too)


• Note: x’s depth decreases by two.
Data Structures and Algorithms,
08/10/2021 12
Dr.V.Durgadevi, ASP
Zig-Zag
• x has a grandparent

z x
10 20
y
30
x z y
T1 20 10 30

T4

T2 T3 T1 T2 T3 T4

• (Symmetric case too) Note: x’s depth decreases by two.


Data Structures and Algorithms,
08/10/2021 13
Dr.V.Durgadevi, ASP
Zig
• x has no grandparent (so, y is the root)
• Note: w could be NIL
y x
10 20
x
20
w y w
T1 30 10 30

T2

T3 T4 T1 T2 T3 T4

• (Symmetric case too)


• Note: x’s depth decreases by one.

Data Structures and Algorithms,


08/10/2021 14
Dr.V.Durgadevi, ASP
Top Down Splay Trees

• Works by starting at the top and working


down to produce a similar result.
• All of the nodes lower than the target are put
into one tree and all of the nodes greater than
the target are put into another tree then it is
recombined.

Data Structures and Algorithms,


08/10/2021 15
Dr.V.Durgadevi, ASP
Top Down Splaying
• Example

Data Structures and Algorithms,


08/10/2021 16
Dr.V.Durgadevi, ASP
Top Down Splaying
• Example

Data Structures and Algorithms,


08/10/2021 17
Dr.V.Durgadevi, ASP
Top Down Splaying
• Example

Data Structures and Algorithms,


08/10/2021 18
Dr.V.Durgadevi, ASP
Complete Example
44

• 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

Data Structures and Algorithms,


08/10/2021 20
Dr.V.Durgadevi, ASP
Complete Example
44
• Splay 78 50

17 88

32 z 65 97
• Zig-Zag y
28 54 82

29
78 x
76 80

Data Structures and Algorithms,


08/10/2021 21
Dr.V.Durgadevi, ASP
Complete Example
44
• Splay 78 50

17 88

32 x 78 97
• Zig-Zag
28 z 65 y 82

29 54 76 80

Data Structures and Algorithms,


08/10/2021 22
Dr.V.Durgadevi, ASP
Complete Example
44
z
• Splay 78 50

17 88 y

32 x 78 97
• Zig-Zag
28 65 82

29 54 76 80

Data Structures and Algorithms,


08/10/2021 23
Dr.V.Durgadevi, ASP
Complete Example
44
x
• Splay 78 78

17 50 z 88 y

82
32 97
65
• Zig-Zag
80
28
54 76

29

Data Structures and Algorithms,


08/10/2021 24
Dr.V.Durgadevi, ASP
Complete Example
y 44

• Splay 78 78
x

17 50 88 w
82
32 97
65
• Zag -Zag
80
28
54 76

29

Data Structures and Algorithms,


08/10/2021 25
Dr.V.Durgadevi, ASP
Complete Example
• Splay 78 78 x
y 44

17 50 88 w

• Zag -Zag 82
32 97
65
80
28
54 76

29

Data Structures and Algorithms,


08/10/2021 26
Dr.V.Durgadevi, ASP
Result of splaying
• The result is a binary tree, with the left subtree
having all keys less than the root, and the
right subtree having keys greater than the
root.
• Also, the final tree is “more balanced” than the
original.
• However, if an operation near the root is done,
the tree can become less balanced.

Data Structures and Algorithms,


08/10/2021 27
Dr.V.Durgadevi, ASP
When to Splay
•Search
– Successful: Splay node where key was found.
– Unsuccessful: Splay last-visited internal node (i.e., last node with
a key).
•Insert:
– Splay newly added node.
•Delete:
– Splay parent of removed node (which is either the node with the
deleted key or its successor).
•Note: All operations run in O(h) time, for a tree of height h.

Data Structures and Algorithms,


08/10/2021 28
Dr.V.Durgadevi, ASP
Splaying Example:
Find(6)
1 1

2 2
zig-zig
3 3
Find(6)
4 6

5 5

08/10/2021 6 Data Structures and Algorithms,


Dr.V.Durgadevi, ASP
4 29
Contd.,
1 1

2 6
zig-zig
3 3

6 2 5

5 4

08/10/2021 4 Data Structures and Algorithms,


Dr.V.Durgadevi, ASP
30
Contd.,
• 6 splayed out!
1 6

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

Data Structures and Algorithms,


08/10/2021 32
Dr.V.Durgadevi, ASP
Contd.,
• 4 splayed out!
6 4

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

• Splaying gets amortized O(log n) performance. (Maybe not now,


but soon, and for the rest of the operations.)

Data Structures and Algorithms,


08/10/2021 34
Dr.V.Durgadevi, ASP
Splay Operations: Find
• Find the node in normal BST manner
• Splay the node to the root

Data Structures and Algorithms,


08/10/2021 35
Dr.V.Durgadevi, ASP
Splitting in Splay Trees
How can we split?
– We have the splay operation.
– We can find x or the parent of where x should be.
– We can splay it to the root.
– Now, what’s true about the left subtree of the root?
– And the right?

Data Structures and Algorithms,


08/10/2021 36
Dr.V.Durgadevi, ASP
Split

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

void insert(Node *& root, Object x)


{
Node * left, * right;
split(root, left, right, x);
root = new Node(x, left, right);
}

Data Structures and Algorithms,


08/10/2021 38
Dr.V.Durgadevi, ASP
Splay Operations: Delete

x
find(x) delete x
L R
L R <x >x

Now what?

Data Structures and Algorithms,


08/10/2021 39
Dr.V.Durgadevi, ASP
Join
Join(L, R): given two trees such that L < R, merge them

L
splay
L R R

Splay on the maximum element in L, then attach R

Data Structures and Algorithms,


08/10/2021 40
Dr.V.Durgadevi, ASP
Delete Completed
x
find(x) delete x
L R
T L R <x >x

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

–All splay tree operations run in amortized O(log n) time

•O(N) operations can occur, but splaying makes them infrequent

•Implements most-recently used (MRU) logic


–Splay tree structure is self-tuning

Data Structures and Algorithms,


08/10/2021 44
Dr.V.Durgadevi, ASP

You might also like