Presentation is loading. Please wait.

Presentation is loading. Please wait.

SPLAY TREE Features Binary Search Tree Self adjusting balanced tree

Similar presentations


Presentation on theme: "SPLAY TREE Features Binary Search Tree Self adjusting balanced tree"— Presentation transcript:

1 SPLAY TREE Features Binary Search Tree Self adjusting balanced tree
Most recently accessed item is always at the top of the tree Support all the operations of Binary Tree Uses rotations to attempt balance Provides fast access to recently used keys A single operation on a splay tree can take O(N) time. Not balanced at all time

2 BASIC IDEA OF SPLAY TREE
After a node is accessed, it is pushed to the root by a series of AVL tree-like operations (rotations). For most applications, when a node is accessed, it is likely that it will be accessed again in the near future By pushing the accessed node to the root the tree: If the accessed node is accessed again, the future accesses will be much less costly. During the push to the root operation, the tree might be more balanced than the previous tree. Accesses to other nodes can also be less costly.

3 EXAMPLE Accessing node k1
When a node k is accessed, push it towards the root Do a singe rotation between node k’s parent and node k itself k5 k4 F k3 E k2 D k1 A B B access path

4 EXAMPLE But k3 is nearly as deep as k1 was.
An access to k3 will push some other node nearly as deep as k3 is. So, this method does not work ... k1 k1 is now root k2 k5 k4 A B F k3 E C D

5 SPLAYING METHOD Restructuring the tree by applying rotations
The method will push the accessed node to the root. With this pushing operation it will also balance the tree somewhat. So that further operations on the new will be less costly compared to operations that would be done on the original tree. A deep tree will be splayed: Will be less deep, more wide.

6 Node (N) being accessed is:
SPLAYING PROCEDURE Node (N) being accessed is: Root Child of root Has both parent (p) and grandparent (g) Zig-zig pattern: G  P  N is left-left or right-right (Fig A) Zig-zag pattern: G  P  N is left-right or right-left (Fig B) Fig A Fig B G G G G P P P P N N N N

7 SPLAYING PROCEDURE root root X Y X Y Case 1: N is a Root Node Steps:
do nothing root root N N X Y X Y

8 SPLAYING PROCEDURE root root P Z X P X Y Y Z
Case 2: N is a either a left or right child of Root steps: Apply single AVL rotation (Zig) root root P N N Z X P In the case where we’re just one step away from the root, we single rotate. Now, each of these helped and hurt an equal number of nodes at each step, but other than this final zig, our node always helped its children, right? Therefore, every single node except p, Y, and Z in this picture improves (some quite a lot). How bad are things for p, Y, and Z? each might be down one level from where it used to be. How good are things for n and many of its descendants and former ancestors? Very good. Why not zig all the way (no zig-zig)? This just helps _one_ child of n! X Y Y Z Splay Trees CSE 326 Autumn

9 SPLAYING PROCEDURE G W P P Z X G Y Y Z W X
Case 3a: N has both P & G, N is LL or RR child of G Steps: Apply Zig-zig rotation (LL /RR rotation) top down manner G N 1 W P P Z 2 X N G Y Can anyone tell me how to implement this with two rotations? There are two possibilities: Start with rotate n or rotate p? Rotate p! Rotate n makes p n’s left child and then we’re hosed. Then, rotate n. This helps all the nodes in blue and hurts the ones in red. So, in some sense, it helps and hurts the same number of nodes on one rotation. Question: what if we keep rotating? What happens to this whole subtree? It gets helped! Y Z W X Splay Trees CSE 326 Autumn

10 SPLAYING PROCEDURE G X P G P W X Y Z W Y Z
Case 3b: N has both P & G, N is either LR or RL child of G Steps: Apply AVL Double rotation (Zig-Zag) – Either LR or RL rotation G N X P G P N W X Y Z W Y Z Splay Trees CSE 326 Autumn

11 OPERATIONS Insert(x) : Normal BST insert, then splay x
Find(x) : Attempt normal BST find(x) and splay last node visited • If x is in the tree, then we splay x • If x is not in the tree we splay the leaf node where our search ended FindMin(), FindMax() Walk to far left or right of tree, return that node's value and then splay that node DeleteMin(), DeleteMax() Perform FindMin(), FindMax() [which splays the min/max to the root] then delete that node and set root to be the non-NULL child of the min/max • Remove(x) Find(x) splaying it to the top, then overwrite its value with is successor/predecessor, deleting the successor/predecessor node


Download ppt "SPLAY TREE Features Binary Search Tree Self adjusting balanced tree"

Similar presentations


Ads by Google