0% found this document useful (0 votes)
47 views15 pages

Splay Trees Are Binary Search Trees (BSTS) That

Splay trees are binary search trees that balance themselves during operations like search or insertion by rotating nodes along access paths toward the root. This process, called "splaying", aims to bring frequently accessed nodes closer to the root over time for faster future access. Splay trees use one of six possible rotation operations - zig, zig-zig, zig-zag, zag, zag-zig, zag-zag - to splay the accessed node to the root. Analysis shows that any sequence of operations on a splay tree takes amortized O(log N) time per operation.

Uploaded by

Lini Ickappan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views15 pages

Splay Trees Are Binary Search Trees (BSTS) That

Splay trees are binary search trees that balance themselves during operations like search or insertion by rotating nodes along access paths toward the root. This process, called "splaying", aims to bring frequently accessed nodes closer to the root over time for faster future access. Splay trees use one of six possible rotation operations - zig, zig-zig, zig-zag, zag, zag-zig, zag-zag - to splay the accessed node to the root. Analysis shows that any sequence of operations on a splay tree takes amortized O(log N) time per operation.

Uploaded by

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

1

Splay Trees
Splay trees are binary search trees (BSTs) that:
Are not perfectly balanced all the time
Allow search and insertion operations to try to balance the
tree so that future operations may run faster

Based on the heuristic:
If X is accessed once, it is likely to be accessed again.
After node X is accessed, perform splaying operations to
bring X up to the root of the tree.
Do this in a way that leaves the tree more or less balanced
as a whole.
2
Motivating Example
Initial tree
Root
15
6
18
3
12
9 14
14
Root
12
6
15
3
9 18
After splaying with 12
After Search(12)
Splay idea: Get 12
up to the root
using rotations
Not only splaying with 12 makes the tree balanced,
subsequent accesses for 12 will take O(1) time.
Active (recently accessed) nodes will move towards
the root and inactive nodes will slowly move further
from the root
1
2
3
Splay Tree Terminology
P
X
Let X be a non-root node, i.e., has at least 1 ancestor.
Let P be its parent node.
Let G be its grandparent node (if it exists)
Consider a path from G to X:
Each time we go left, we say that we zig
Each time we go right, we say that we zag
There are 6 possible cases:
G
P
X
G
P
X
G
P
X
G
P
X
P
X
1. zig 2. zig-zig 3. zig-zag
4. zag-zig 5. zag-zag 6. zag
4
Splay Tree Operations
When node X is accessed, apply one of six
rotation operations:
Single Rotations (X has a P but no G)
zig, zag

Double Rotations (X has both a P and a G)
zig-zig, zig-zag
zag-zig, zag-zag
5
Splay Trees: Zig Operation
Zig is just a single rotation, as in an AVL tree
Suppose 6 was the node that was accessed (e.g. using
Search)
15
6
18
3
12
Zig-Right moves 6 to the root.
Can access 6 faster next time: O(1)
Notice that this is simply a right rotation in AVL tree
terminology.
15
6
18
3
12
Zig-Right
6
Splay Trees: Zig-Zig Operation
Zig-Zig consists of two single rotations of the same
type
Suppose 3 was the node that was accessed (e.g., using
Search)
Due to zig-zig splaying, 3 has bubbled to the top!
Note: Parent-Grandparent is rotated first.
15
6
18
3
12
1 4
12
6
18
3
15
1 4
12
6
18
3
15
1
4
Zig-Right
Zig-Right
7
Splay Trees: Zig-Zag Operation
Zig-Zag consists of two rotations of the opposite type
Suppose 12 was the node that was accessed (e.g., using Search)
Due to zig-zag splaying, 12 has bubbled to the top!
Notice that this is simply an LR imbalance correction in AVL tree
terminology (first a left rotation, then a right rotation)
15
6
18
3
12
10
14
Zag-Left
15
6
18
3
12
10
14
15
6
18
3
12
10 14
Zig-Right
8
Splay Trees: Zag-Zig Operation
Zag-Zig consists of two rotations of the opposite type
Suppose 17 was the node that was accessed (e.g., using Search)
Due to zag-zig splaying, 17 has bubbled to the top!
Notice that this is simply an RL imbalance correction in
AVL tree terminology (first a right rotation, then a
left rotation)
15
6
20
30 17
16 18
Zig-Right
15
6
20
30
17
16
18
15
6
20
30
17
16
18
Zag-Left
9
Splay Trees: Zag-Zag Operation
Zag-Zag consists of two single rotations of the same type
Suppose 30 was the node that was accessed (e.g., using Search)
Due to zag-zag splaying, 30 has bubbled to the top!
Note: Parent-Grandparent is rotated first.
Zag-Left
15
6
20
30 17
25 40
15
6
20
30
17
25 40
15
6
20
30
17
25
40
Zag-Left
10
Splay Trees: Zag Operation
Zag is just a single rotation, as in an AVL tree
Suppose 15 was the node that was accessed (e.g., using
Search)
15
6
18
3
12
Zag-Leftmoves 15 to the root.
Can access 15 faster next time: O(1)
Notice that this is simply a left rotation in AVL tree
terminology
15
6
18
3
12
Zag-Left
11
Splay Trees: Example 40 is accessed
80
70
85
60
75
50 65
40 55
30 45
(a)
80
70
85
75
50
40
30
45
(b)
60
55
65
After Zig-zig
70
50
40
30
45 60
55
65
80
75 85
(c)
After Zig-zig
12
Splay Trees: Example 60 is accessed
70
50
40
30
45 60
55
65
80
75 85
70
50
40
30
45
60
55
65
80
75 85
70
50
40
30
45
60
55 65
80
75 85
(a) (b)
After Zig-zag
(c)
After zag
13
Do it yourself exercise
Insert the keys 1, 2, , 7 in that order into
an empty splay tree.
What happens when you access 7?
14
Splaying during other operations
Splaying can be done not just after Search, but also
after other operations such as Insert/Delete.

Insert X: After inserting X at a leaf node (as in a
regular BST), splay X up to the root

Delete X: Do a Search on X and get X up to the root.
Delete X at the root and move the largest item in its
left sub-tree, i.e, its predecessor, to the root using
splaying.

Note on Search X: If X was not found, splay the leaf
node that the Search ended up with to the root.
15
Summary of Splay Trees
Examples suggest that splaying causes tree to get balanced.

The actual analysis is rather advanced and is in Chapter 11. Such
analysis is called amortized analysis

Result of Analysis: Any sequence of M operations on a splay
tree of size N takes O(M log N) time. So, the amortized running
time for one operation is O(log N).

This guarantees that even if the depths of some nodes get very
large, you cannot get a long sequence of O(N) searches because
each search operation causes a rebalance.

Without splaying, total time could be O(MN).

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