Title: Splay Trees CSE 326 Data Structures
1Splay TreesCSE 326 Data Structures
- Richard Anderson
- July 11, 2005
Textbook reference Section 4.5
2Classroom Presenter and Student Submissions
3AVL Trees
- What is the most important aspect of AVL trees?
4Lecture Outline
- Amortized Complexity
- Splaying Binary Search Trees
- Double rotations
- Search Tree operations using splay trees
- Why it works
5Amortized cost
- Cost per operation vs. cost of a sequence of
operations - Total cost of ownership
- Examples
- Owning a car
- Small recurring costs gas, parking
- Big expenses insurance, repairs
- Processing email
- Dealing with arriving messages
- Cleaning up inbox once every three months
6Data Structure ExampleBuffering recent items
- Problem
- Allow access to the most recent k items received
- Solution
- Maintain an array of recent items, add items at
the end, compact the array when full - Array of size 2k
- If space left just add the item
- If full shift all elements k positions to the left
7Problem What is the amortized cost of inserting
k2 items?
- Number of insertions
- Cost per insertion
- Total cost of insertions
- Number of array compactions
- Cost per compaction
- Total cost of compactions
Total overall cost Amortized cost per item
8Balancing trees
- Basic binary search trees
- No explicit balancing
- Order determined by order of insertion
- AVL Trees
- Explicit operations to keep tree balanced
- Guarantees on height of tree with respect to
number of nodes - Self adjusting trees
- Operations applied which help keep the tree
balanced
9Splay Trees
- Splay trees are tree structures that
- Are not perfectly balanced all the time
- Data most recently accessed is near the root.
- The procedure
- After node X is accessed, perform splaying
operations to bring X to the root of the tree. - Do this in a way that leaves the tree more
balanced as a whole
10The result
- Splay trees support insert, find, and delete at
O(log n) Amortized cost - The cost of a sequence of n insert, find, and
delete operations is O(n log n) - No guarantee (better than O(n)) on the cost of a
single operation
11Splaying a node
Elements in increasing order A,B,,G
- Rotate a node to the root of the tree two levels
at a time
D
G
G
G
G
A
A
A
A
D
F
F
C
D
E
C
B
E
E
E
D
B
B
F
C
C
D
B
12ZIG-ZIG
Z
Y
D
X
C
A
B
13ZIG-ZAG
Show the ZIG-ZAG transformation to bring X to the
root
Z
Y
A
D
X
B
C
14ZAG-ZIG, ZAG-ZAG, ZIG, ZAG
- Other two level transformations similar
- ZIG and ZAG used to handle the case of a path to
root of odd length
15Find operation
- Find the value as in a binary search tree
- Once the value is found, splay it to the root
- This will rearrange nodes in the tree, generally
making future operations less expensive
16Splay E
E
Items order A, , I, smallest to largest
H
B
I
A
D
F
G
C
I
H
A
B
G
F
C
D
E
17Splay E (submit your answer)
E
I
A
H
B
A
G
C
I
D
F
B
H
C
G
D
F
E
18Insert and Remove
- Insert x
- Apply standard BST insert
- Splay x to the root
- Remove x
- Splay x to the root
- Delete the root
- Splay largest element of left subtree to the root
- Merge trees
19Remove
X
X
Z
Z
Z
20Describe how you find the largest element in a
tree
21Splaying summary
- Every time we access an element, we bring it to
the root with a splay - This ensures that the work done in accessing a
node also rebalances the tree
22Splay tree performance
- Claim n Insert, Remove, Find operations take
O(n log n) time - Intuitive argument long splay operations reduce
the depth of many nodes - Rigorous argument accounting based proof to
show each step costs O(log n)
23Splay Trees
- What is the most important aspect of Splay trees?