Presentation is loading. Please wait.

Presentation is loading. Please wait.

Splay Trees.

Similar presentations


Presentation on theme: "Splay Trees."— Presentation transcript:

1 Splay Trees

2 Motivation for Splay Trees
Problems with AVL Trees extra storage/complexity for height fields complicated 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 You’re forced to make 17 a really deep access:
Since you’re down there anyway, fix up a lot of deep nodes! Doorbell rings – clean up on the way to answer the door. 5 2 9 3

4 Self adjusting Trees Ordinary binary search trees have no balance conditions Balanced trees like AVL trees enforce a balance condition when nodes change tree is always balanced after an insert or delete Self-adjusting trees get reorganized over time as nodes are accessed Tree adjusts after insert, delete, or find

5 Splay Trees Splay trees are tree structures that: The procedure:
Are not perfectly balanced all the time Data most recently accessed is near the root. (principle of locality; “rule” – 80% of the accesses are to 20% of the data) The procedure: After node X is accessed, perform “splaying” operations to bring X to the root of the tree. Reorganize on the way out of the recursion Do this in a way that creates a more balanced as a whole

6 Splay Tree Terminology
Let X be a non-root node with  2 ancestors. P is its parent node. G is its grandparent node. G G G G P P P P X X X X

7 Zig-Zig and Zig-Zag Zig-zig 4 G G 5 P 5 Zig-zag 1 P X 2 X
Parent and grandparent in same direction. Parent and grandparent in different directions. Zig-zig 4 G G 5 P 5 Zig-zag 1 P X 2 X

8 Splay Tree Operations 1. Helpful if nodes contain a parent pointer.
element left right 2. When X is accessed, apply one of six rotation routines. Single Rotations (X has a P (the root) but no G) ZigFromLeft, ZigFromRight Double Rotations (X has both a P and a G) ZigZigFromLeft, ZigZigFromRight ZigZagFromLeft, ZigZagFromRight

9 Zig at depth 1 (no grandparent)
“Zig” is just a single rotation, as in an AVL tree Let R be the node that was accessed (e.g. using Find) moves R to the top faster access next time root Right Rotation

10 Zig at depth 1 Suppose Q is now accessed using Find
moves Q back to the top root Left rotation

11 Zig-Zag operation “Zig-Zag” consists of two rotations of the opposite direction (assume R is the node that was accessed) - same as for AVL double rotation. (RightRotation) (LeftRotation) Double right rotation

12 Zig-Zig operation “Zig-Zig” consists of two single rotations of the same direction (R is the node that was accessed) (ZigFromLeft) (ZigFromLeft) ZigZigFromLeft

13 Zig-Zig operation I like to visualize it as follows (ZigFromLeft)
RightZigZig

14 Decreasing depth - "autobalance"
Find(T) Find(R)

15 Splay Tree Insert and Delete
Insert x Insert x as normal then splay x to root. (Starting at x, look two levels up and do proper rotation. Continue until x is at root.) Delete x Splay x to root and remove it. (note: the node does not have to be a leaf or single child node like in BST delete.) Two trees remain, right subtree and left subtree. Splay the max in the left subtree to the root Attach the right subtree to the new root of the left subtree. Your book doesn’t talk about deletion. There are LOTS of ways it could be done. We will take this method as “the way” to do it for this class.

16 Example Insert Inserting in order 1,2,3,…,8 Without self-adjustment 1
operations O(n2) time for n Insert 2 3 4 5 6 7 8

17 With Self-Adjustment 1 2 3 1 1 2 2 1 3 2 2 1 3 1 ZigFromRight

18 With Self-Adjustment 3 4 4 2 4 ZigFromRight 3 1 2 1 Each Insert takes O(1) time therefore O(n) time for n inserts!! Of course, a find of node 1 takes O(n) access, but the tree will be restructured on the way out of the recursion.

19 Example Deletion 10 splay 8 5 15 5 10 13 20 15 2 8 2 6 9 13 20 6 9
(Zig-Zag) 8 5 15 5 10 13 20 15 2 8 2 6 9 13 20 6 9 Splay on max of left tree attach remove 6 5 10 5 10 15 15 2 6 9 2 9 13 20 13 20

20 Analysis of Splay Trees
Splay trees tend to be balanced m operations takes time O(m log n) for m > n operations on n items. (proof is difficult) Amortized O(log n) time. Webster: Amortize: gradually write off the initial cost of (an asset). Splay trees have good “locality” properties Recently accessed items are near the root of the tree. Items near an accessed one are pulled toward the root.


Download ppt "Splay Trees."

Similar presentations


Ads by Google