Download presentation
Presentation is loading. Please wait.
1
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department
2
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 2 Spay Trees So far we have seen: BST: binary search trees Worst-case running time per operation = O(N) Worst case average running time = O(N) Think about inserting a sorted item list AVL tree: Worst-case running time per operation = O(logN) Worst case average running time = O(logN) We will now wee a new data structure, called splay trees, for which: Worst-case running time of one operation = O(N) Worst case running time of M operations = O(MlogN) O(logN) amortized running time. A spay tree is a binary search tree.
3
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 3 Splay trees A splay tree guarantees that, for M consecutive operations, the total running time is O(MlogN). A single operation on a splay tree can take O(N) time. So the bound is not as strong as O(logN) worst- case bound in AVL trees.
4
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 4 Amortized running time Definition: For a series of M consecutive operations: If the total running time is O(M*f(N)), we say that the amortized running time (per operation) is O(f(N)). Using this definition: A splay tree has O(logN) amortized cost (running time) per operation.
5
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 5 Splay tree idea: Try to make the worst-case situation occur less frequently. In a Binary search tree, the worst case situation can occur with every operation. (while inserting a sorted item list). In a splay tree, when a worst-case situation occurs for an operation: The tree is re-structured (during or after the operation), so that the subsequent operations do not cause the worst-case situation to occur again. The basic idea of splay tree is: 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 access, it is likely that it will be accessed again in the near future.
6
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 6 Splay tree idea: By pushing the accessed node to the root the tree: 1) If the accessed node is accessed again, the future accesses will be much less costly. 2) 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.
7
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 7 Splay tree: a bad method for pushing to the root A simple idea, which is not working, is: When a node K is accessed, push it towards the root by the following algorithm: On the path from k to root: Do a singe rotation between node k’s parent and node k itself. Every single rotation will push k to one level higher. Finally, it will reach to the root.
8
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 8 Splay tree: a bad method for pushing to the root F k4k4 E D A k1k1 B B k5k5 k3k3 k2k2 access path Accessing node k 1
9
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 9 F k4k4 E D B k5k5 k3k3 After rotation between k 2 and k 1 A k2k2 Pushing k 1 to the root C k1k1
10
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 10 F k4k4 E B k5k5 k1k1 After rotation between k 3 and k 1 A Pushing k 1 to the root k2k2 DC k3k3
11
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 11 F k1k1 B k5k5 After rotation between k 4 and k 1 A Pushing k 1 to the root k2k2 DC E k4k4 k3k3
12
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 12 k1k1 B After rotation between k 5 and k 1 A Pushing k 1 to the root k2k2 DC F k3k3 k5k5 E k4k4 k 1 is now root But k 3 is nearly as deep as k 1 was. An access to k 3 will push some other node nearly as deep as k 3 is. So, this method does not work!
13
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 13 Splaying – A method that works Now, we will give a method that works. It 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 spayed: Will be less deep, more wide.
14
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 14 Splaying - algorithm Assume we access a node. We will splay along the path from access node to the root. At every splay step: We will selectively rotate the tree. Selective operation will depend on the structure of the tree around the node around which rotation will be performed
15
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 15 Splaying - algorithm Let X be a non-root node on the access path at which we are rotating. If parent of X is root, We rotate between X and root. X becomes root. And we are done! Otherwise, X has a parent P which is non-root. X certainly has also a grand-parent G. There two cases to consider Zig-zag case: P is left child of G and X is right child of P Zig-zig case: P is left child of G and X is left child of P We will have also symmetric cases of the two cases above. The operations for these symmetric cases will be very similar to the operations for the two cases above. Symetric cases are: P is right child of G and X is left child of P P is right child of G and X is right child of P.
16
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 16 Splaying - algorithm Depending on the case that is determined from the interconnection of X, P, and G, we will perform one of the two operations we will describe below: Zig-zag case: Perform double rotation (an AVL tree operation). Zig-zig case Transform the tree to an other one as described below.
17
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 17 Case 1: Zig-zag case operation CB X A P G D P X G A BCD double-rotation between G, P, and X
18
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 18 Case 2: Zig-zig case operation P G D X C B A X D A P C G B
19
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 19 A general example: F k4k4 E D A k1k1 B B k5k5 k3k3 k2k2 node k 1 is accessed and it will be pushed to root. X P G This is case 1: Zig-zag case Double rotate Original tree
20
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 20 A general example: F k4k4 E AB k5k5 k1k1 After double rotation k2k2 CD k3k3 X P G This is case 2: Requires zig-zig case operation
21
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 21 A general example – final tree k1k1 After zig-zig case operation EF k5k5 k4k4 AB k2k2 CD k3k3 k 1 is root The tree is more balanced than the original tree The depth of the new tree is nearly half of the depth of the original tree New tree
22
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 22 A specific example Assume 7,6,5,4,3,2,1 are inserted into an empty splay tree. 7 6 5 4 3 2 1 Splaying at node with item 1 X P G 7 6 5 4 1 2 3 7 6 1 4 5 X P G 2 3 1 6 7 2 3 4 5 X P G original tree new tree after splaying
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.