Splay Trees CSE 326 Data Structures - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Splay Trees CSE 326 Data Structures

Description:

Splaying Binary Search Trees. Double rotations. Search Tree ... Total cost of ownership. Examples. Owning a car. Small recurring costs gas, parking. Big ... – PowerPoint PPT presentation

Number of Views:479
Avg rating:3.0/5.0
Slides: 24
Provided by: Douglas240
Category:
Tags: cse | data | splay | structures | trees

less

Transcript and Presenter's Notes

Title: Splay Trees CSE 326 Data Structures


1
Splay TreesCSE 326 Data Structures
  • Richard Anderson
  • July 11, 2005

Textbook reference Section 4.5
2
Classroom Presenter and Student Submissions
3
AVL Trees
  • What is the most important aspect of AVL trees?

4
Lecture Outline
  • Amortized Complexity
  • Splaying Binary Search Trees
  • Double rotations
  • Search Tree operations using splay trees
  • Why it works

5
Amortized 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

6
Data 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

7
Problem 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
8
Balancing 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

9
Splay 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

10
The 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

11
Splaying 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
12
ZIG-ZIG
Z
Y
D
X
C
A
B
13
ZIG-ZAG
Show the ZIG-ZAG transformation to bring X to the
root
Z
Y
A
D
X
B
C
14
ZAG-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

15
Find 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

16
Splay 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
17
Splay E (submit your answer)
E
I
A
H
B
A
G
C
I
D
F
B
H
C
G
D
F
E
18
Insert 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

19
Remove
X
X
Z
Z
Z
20
Describe how you find the largest element in a
tree
21
Splaying 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

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

23
Splay Trees
  • What is the most important aspect of Splay trees?
Write a Comment
User Comments (0)
About PowerShow.com