Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Tree
by
P.VASUKI
Splay Tree
• Special type of Binary Search tree –
Self Adjusting balanced tree.
• Tree adjusts itself after every insert,
delete and find
• Works better with Amortized
(Average analysis) running time
Splay Tree
• The node recently accessed may be
needed in near future(locality
concept)
• Moving the recently accessed node
nearer to the root reduces further
processing time.
• Tree Structure is not balanced
always
Splaying
• After a node is accessed(insert/find),
it is splayed to the root (Moving that
node to root)
• Doing this way makes the tree
balanced over a period of time
Splay Tree Terminology
G G G G
P P P P
X X X X
Zig-Zig and Zig-Zag
Parent and grandparent Parent and grandparent
in same direction. in different directions.
Zig-zig 4
G
G 5
P 5
Zig-zag
1 P
X 2
X Similar to dog-Leg like
structure
6
Splay Tree Operations
When X is accessed, apply one of six rotation routines.
ZigFromLeft
root
ZigFromRight
9
Zig-Zag operation
(ZigFromRight) (ZigFromLeft)
ZigZagFromLeft
(ZigFromLeft) (ZigFromLeft)
ZigZigFromLeft
Find(T) Find(R)
12
Example Insert
• Inserting in order 1,2,3,…,8
• Without self-adjustment
1
2 O(n2) time for n Insert
3
4
5
6
7
Example : Courtesy :Washington University 8
With Self-Adjustment
1 1
ZigFromRight
2 1 2
2 1
3
3 2 ZigFromRight
2
1 3
1
4 3
4
2 4 ZigFromRight
3
1
2
1
Each Insert takes O(1) time therefore O(n) time for n Insert!!
10 splay (Zig-Zag) 8
5 15 5 10
2 8 13 20 2 6 9 15
13 20
6 9
Splay (zig) remove
6
attach 5 10
5 10
15 2 6 9 15
2 9
13 20 13 20
16
Note on Splay Tree
• Good at locality concepts – Recently
accessed node will be at the top.
• Better in Amortized analysis