CSE 326 Splay Trees - PowerPoint PPT Presentation

About This Presentation
Title:

CSE 326 Splay Trees

Description:

Now, that s gauranteed to have no right child, right? Just snap R onto that NULL right side of the max. So, we just join the two subtrees for delete. – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 28
Provided by: coursesCs89
Category:
Tags: cse | child | right | splay | trees

less

Transcript and Presenter's Notes

Title: CSE 326 Splay Trees


1
CSE 326Splay Trees
  • David Kaplan
  • Dept of Computer Science Engineering
  • Autumn 2001

2
Motivation for Splay Trees
  • Problems with AVL Trees
  • extra storage/complexity for height fields
  • ugly delete code
  • Solution splay trees
  • blind adjusting version of AVL trees
  • amortized time for all operations is O(log n)
  • worst case time is O(n)
  • insert/find always rotates node to the root!

3
Splay Tree Idea
10
Youre forced to make a really deep access
17
5
9
2
3
4
Splaying Cases
  • Node being accessed (n) is
  • Root
  • Child of root
  • Has both parent (p) and grandparent (g)
  • Zig-zig pattern g ? p ? n is left-left or
    right-right
  • Zig-zag pattern g ? p ? n is left-right or
    right-left

5
Access rootDo nothing (that was easy!)
root
root
n
n
X
Y
X
Y
6
Access child of rootZig (AVL single rotation)
root
root
p
n
n
Z
p
X
X
Y
Z
Y
7
Access (LR, RL) grandchildZig-Zag (AVL double
rotation)
g
n
X
p
g
p
n
W
Y
W
Z
X
Y
Z
8
Access (LL, RR) grandchildZig-Zig
Rotate top-down why?
n
g
Z
p
W
p
Y
g
X
n
X
W
Y
Z
9
Splaying ExampleFind(6)
1
1
2
2
zig-zig
3
3
Find(6)
4
5
6
10
still splaying
1
1
2
6
zig-zig
3
3
2
5
4
11
6 splayed out!
1
6
zig
3
3
2
5
2
5
4
4
12
Splay it Again, Sam!Find (4)
zig-zag
3
4
Find(4)
2
5
3
5
4
2
13
4 splayed out!
4
6
1
zig-zag
3
5
4
2
3
5
2
14
Why Splaying Helps
  • If a node n on the access path is at depth d
    before the splay, its 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.)

15
Splay Operations Find
  • Find the node in normal BST manner
  • Splay the node to the root

16
Splay Operations Insert
  • Ideas?
  • Can we just do BST insert?

17
Digression Splitting
  • Split(T, x) creates two BSTs L and R
  • all elements of T are in either L or R (T L ?
    R)
  • all elements in L are ? x
  • all elements in R are ? x
  • L and R share no elements (L ? R ?)

18
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, whats true about the left subtree of the
    root?
  • And the right?

19
Split
split(x)
splay
T
L
R
OR
L
R
L
R
? x
? x
gt x
lt x
20
Back to Insert
x
L
R
gt x
lt x
  • void insert(Node root, Object x)
  • Node left, right
  • split(root, left, right, x)
  • root new Node(x, left, right)

21
Splay Operations Delete
x
delete x
L
R
gt x
lt x
Now what?
22
Join
  • Join(L, R) given two trees such that L lt R,
    merge them
  • Splay on the maximum element in L, then attach R

L
23
Delete Completed
x
T
delete x
L
R
gt x
lt x
Join(L,R)
T - x
24
Insert Example
4
4
6
6
split(5)
6
1
1
9
9
1
9
2
2
7
4
7
7
5
2
4
6
Insert(5)
1
9
2
7
25
Delete Example
4
6
6
6
1
1
9
9
1
find(4)
9
2
2
7
4
7
Find max
7
2
2
2
1
6
1
6
Delete(4)
9
9
7
7
26
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

27
Splay Tree Summary (cont.)
  • Splaying can be done top-down better because
  • only one pass
  • no recursion or parent pointers necessary
  • There are alternatives to split/insert and
    join/delete
  • Splay trees are very effective search trees
  • relatively simple no extra fields required
  • excellent locality properties
  • frequently accessed keys are cheap to find (near
    top of tree)
  • infrequently accessed keys stay out of the way
    (near bottom of tree)
Write a Comment
User Comments (0)
About PowerShow.com