Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL:...
-
Upload
jocelyn-andrea-powell -
Category
Documents
-
view
215 -
download
0
Transcript of Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL:...
![Page 1: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/1.jpg)
Splay Trees
CSIT 402Data Structures II
![Page 2: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/2.jpg)
MotivationProblems with other balanced trees
– AVL:• extra storage/complexity for height fields• Periulous delete code
– Red-Black• Complex coding
Solution: splay trees– blind adjusting version of AVL trees– amortized time for all operations is O(log n)– worst case time is O(n)– Different idea: Trade a little balance for faster access of most
recently searched for/inserted items• insert/find always rotates node to the root!
2
![Page 3: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/3.jpg)
Splay Tree Idea
17
10
92
5
3
You’re forced to make a really deep access:
Since you’re down there anyway,fix up a lot of deep nodes!
3
![Page 4: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/4.jpg)
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-rightZig-zag pattern: g p n is left-right or right-left
4
![Page 5: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/5.jpg)
Access root:
Do nothing (that was easy!)
X
n
Y
root
X
n
Y
root
5
![Page 6: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/6.jpg)
Access child of root:
Zig (AVL single rotation)
p
X
n
Y
Z
n
Z
p
Y
X
root root
6
![Page 7: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/7.jpg)
Access (LR, RL) grandchild:
Zig-Zag (AVL double rotation)
g
Xp
Y
n
Z
W
n
Y
g
W
p
ZX
7
![Page 8: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/8.jpg)
Access (LL, RR) grandchild:
Zig-Zig
n
Z
Y
p
X
g
W
g
W
X
p
Y
n
Z
Rotate top-down – why?
8
![Page 9: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/9.jpg)
Splaying Example:
Find(6)
2
1
3
4
5
6
Find(6)
2
1
3
6
5
4
zig-zig
9
![Page 10: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/10.jpg)
… still splaying …
zig-zig2
1
3
6
5
4
1
6
3
2 5
4
10
![Page 11: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/11.jpg)
… 6 splayed out!
zig
1
6
3
2 5
4
6
1
3
2 5
4
11
![Page 12: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/12.jpg)
Find (4)
Find(4)
zig-zag
6
1
3
2 5
4
6
1
4
3 5
2
12
![Page 13: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/13.jpg)
… 4 splayed out!
zig-zag
6
1
4
3 5
2
61
4
3 5
2
13
![Page 14: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/14.jpg)
Why Splaying Helps
• If a node n on the access path is at depth d before the splay, it’s 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.)
14
![Page 15: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/15.jpg)
Splay Operations: Find
• Find the node in normal BST manner• Splay the node to the root
15
![Page 16: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/16.jpg)
Splay Operations: Insert
• Ideas?• Can we just do BST insert?
16
![Page 17: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/17.jpg)
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 = )
17
![Page 18: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/18.jpg)
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, what’s true about the left subtree of the
root?– And the right?
18
![Page 19: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/19.jpg)
Split
split(x)
T L Rsplay
OR
L R L R
x x> x < x19
![Page 20: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/20.jpg)
Back to Insert
void insert(Node *& root, Object x){ Node * left, * right; split(root, left, right, x); root = new Node(x, left, right);}
split(x)
L R
x
L R
> x< x
20
![Page 21: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/21.jpg)
Splay Operations: Delete
find(x)
L R
x
L R
> x< x
delete x
Now what?
21
![Page 22: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/22.jpg)
Join
Join(L, R): given two trees such that L < R, merge them
Splay on the maximum element in L, then attach R
L R Rsplay
L
22
![Page 23: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/23.jpg)
Delete Completed
T
find(x)
L R
x
L R
> x< x
delete x
T - x
Join(L,R)
23
![Page 24: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/24.jpg)
Insert Example
91
6
4 7
2
Insert(5)
split(5)
9
6
7
1
4
2
1
4
2
9
6
7
1
4
2
9
6
7
5
24
![Page 25: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/25.jpg)
Delete Example
91
6
4 7
2
Delete(4)
find(4)9
6
7
1
4
2
1
2
9
6
7
Find max
2
1
9
6
7
2
1
9
6
7 25
![Page 26: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/26.jpg)
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
26
![Page 27: Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.](https://reader030.fdocuments.in/reader030/viewer/2022032703/56649d215503460f949f6d08/html5/thumbnails/27.jpg)
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)
27