Vous êtes sur la page 1sur 17

SPLAY TREE INTRODUCTION A splay tree is a self-balancing binary search tree with the additional property that recently

accessed elements are quick to access again. It performs basic operations such as insertion, look-up and removal in O(log(n)) amortized time. For many non-uniform sequences of operations, splay trees perform better than other search trees, even when the specific pattern of the sequence is unknown. The splay tree was invented by Daniel Sleator and Robert Tarjan. All normal operations on a binary search tree are combined with one basic operation, called splaying. Splaying the tree for a certain element rearranges the tree so that the element is placed at the root of the tree. One way to do this is to first perform a standard binary tree search for the element in question, and then use tree rotations in a specific fashion to bring the element to the top. Alternatively, a top-down algorithm can combine the search and the tree reorganization into a single phase. Advantages and disadvantages Good performance for a splay tree depends on the fact that it is self-balancing, and indeed self optimizing, in that frequently accessed nodes will move nearer to the root where they can be accessed more quickly. This is an advantage for nearly all practical applications, and is particularly useful for implementing caches and garbage collection algorithms; however it is important to note that for uniform access, a splay tree's performance will be considerably (although not asymptotically) worse than a somewhat balanced simple binary search tree. Splay trees also have the advantage of being considerably simpler to implement than other self-balancing binary search trees, such as red-black trees or AVL trees, while their average-case performance is just as efficient. Also, splay trees do not need to store any bookkeeping data, thus minimizing memory requirements. However, these other data structures provide worst-case time guarantees, and can be more efficient in practice for uniform access. One worst case issue with the basic splay tree algorithm is that of sequentially accessing all the elements of the tree in the sorted order. This leaves the tree completely unbalanced (this takes n accesses - each a O(log n) operation).

Reaccessing the first item triggers an operation that takes O(n) operations to rebalance the tree before returning the first item. This is a significant delay for that final operation, although the amortized performance over the entire sequence is actually O(log n). However, recent research shows that randomly rebalancing the tree can avoid this unbalancing effect and give similar performance to the other selfbalancing algorithms. It is possible to create a persistent version of splay trees which allows access to both the previous and new versions after an update. This requires amortized O(log n) space per update. Contrary to other types of self balancing trees, splay trees work well with nodes containing identical keys. Even with identical keys, performance remains amortized O(log n). All tree operations preserve the order of the identical nodes within the tree, which is a property similar to stable sorting algorithms. A carefully designed find operation can return the left most or right most node of a given key.

SPLAY TREE OPERATIONS I. SPLAY 1. Splay tree is a binary search tree where every time a node is accessed it is brought to the root by a sequence of rotations. A node is said to be accessed when: a. the node is found b. the node is inserted c. the node is deleted 2. Rotations are similar to that of AVL tree with some difference; - Rotations in AVL tree are done to keep the tree balanced while rotations in Splay tree is done to bring the accessed node to the root. Therefore, AVL tree always guarantees a height balanced tree but splay tree does not.

3. In the process of bringing the accessed node to the root by rotations, the splay tree becomes more balanced. 4. The surgery on the tree is done using rotations, also called as splaying steps. There are six different splaying steps: a. Zig Rotation (Right Rotation) b. Zag Rotation (Left Rotation) c. Zig-Zag (Zig followed by Zag) d. Zag-Zig (Zag followed by Zig) e. Zig-Zig f. Zag-Zag The Right and Left Rotation (ZIG/ZAG):

Right rotation makes the left child x of a node y into ys parent; y becomes the right child of x.

Left rotation makes the right child y of a node x into xs parent; x becomes the left child of y.

Algorithm on splay operation: 1. Let x be a node in the tree, which refer to as splay node. 2. If x is root stop 3. If x is a child of the root If x is the left child of the root do ZIG otherwise do ZAG 4. Else if x is a left-left grandchild Do ZIG-ZIG 5. Else if x is a right-right grandchild Do ZAG-ZAG 6. Else if x is a right-left grandchild Do ZIG-ZAG 7. Else if x is a left-right grandchild Do ZAG-ZIG NOTE: x is a left-left grandchild means x is a left child of its parent, which is itself a left child of tits parent. Illustrations on Splay Operations: Consider the path going from the root down to the accessed node. Each time we move left going down this path, we say we zig and each time we move right, we say we zag. NOTE: T1, T2 and T3 are considered to be subtrees Zig rotation and zag rotation

Zig-zag rotation

Zig-zig and zag-zag rotations

Zig-zig and zag-zag rotations

Two successive right rotations

NOTES: Zig Rotation and Zag Rotation o Note that a zig rotation is the same as a right rotation whereas the zag step is the left rotation. Zig-Zag o This is the same as a double rotation in an AVL tree. Note that the target element is lifted up by two levels. Zag-Zig o o This is also the same as a double rotation in an AVL tree. Here again, the target element is lifted up by two levels.

Zig-Zig and Zag-Zag o o The target element is lifted up by two levels in each case. Zig-Zig is different from two successive right rotations; zag-zag is different from two successive left rotations.

II. INSERT Insertion in a splay tree is done in precisely the same way as in an ordinary binary search tree. This insertion method does not allow duplicate elements to be inserted. If the element to be inserted is already in the tree, then insertion is not done, but the node holding the duplicate element is splayed. If the insertion succeeds, then the newly inserted node is splayed.

Algorithm:

1. If the tree is empty, insert the new item on the root node 2. If the new items key is smaller than the roots key, then insert the item in the roots left subtree. Otherwise insert the new item in the rights subtree. 3. Then splay the newly inserted item. 4. If the element to be inserted is already in the tree, then insertion is not done, but the node holding the duplicate element will be splayed. Illustrations on insert operation: Example: Insert (5, 1, 19, 25, 17, 21, 20)

Insert 5

Insert 1

Insert 19

Insert 25

Insert 17

Insert 21

Insert 20

Inserting keys in sorted order into an initially empty tree using splay insertion takes only a constant number of steps per insertion but leaves an unbalanced tree. In splay tree insertion, we not only bring that node to the root but also bring the other nodes that we encounter (on the search path) closer to the root. Precisely, the rotations that we perform cut in half the distance from the root to any node that we encounter. This property also holds if we implement the search operation such that it performs the splay transformations during the search. Some paths in the trees do get longer: If we do not access nodes on those paths, that effect is of no consequence to us. If we do access nodes on a long path, it becomes one-half as long after we do so; thus, no one path can build up high costs. III. FIND/SEARCH The find operation searches the splay tree for the element sought. If found, that node is splayed to the root. If not found, the last node encountered on the search path (the parent of the non-existent element) is splayed to the root.

Algorithm on find/search operation: 1. Find the node using normal binary search tree find operation. 2. If the node is found splay the node to the root Else splay the last node encountered on the search path. NOTE: In splaying use the steps or algorithm of splay operation.

Illustrations on Find Operation: Assuming we inserted the numbers 14 our tree will look like this:

Find:> 1, 2 and 3

a. Find 1

Splayed to the root

Found

b. Find 2

Splayed to the root Found

c. Find 3

Splayed to the root

Found

IV. REMOVE/DELETE The remove operation for a splay tree differs markedly from that in an ordinary BST. In fact, it's a lot simpler. If the item to be deleted is not in the tree, the node last visited in the search is splayed and we are done. On the other hand, if the item is in the tree, its node is splayed and then deletion occurs as described below. In either event, a find for the item will result in the appropriate node being splayed to the root.

Let's look at the case in which the item is in the tree. The splay that resulted from the find operation puts that items node at the root. In general, the node has both left and right children. Do the following algorithms: Algorithm 1: 1. Disconnect the left and right subtrees from the root. Call these TL, and TR. respectively. Discard the root node. 2. Splay the maximum item in TL. This will result in the root of TL having no right child. (The largest element is at the root, so the root has no larger elements - no right subtree.) 3. Splay the minimum item in TR. This will result in the root of TR having no left child. (The smallest element is at the root, so the root has no smaller elements no left subtree.) 4. All the elements in TL are smaller than the elements in TR. Make TR be the right subtree of the root of TL (or vice-versa). Notice that there are three splays done: a. The splay done when searching for the item in the tree. b. The splay done in the left subtree TL. c. The splay done in the right subtree TR. There are no "cases" to worry about as in the ordinary BST deletion method. Removing a node in the splay tree can also be done as: Algorithm 2: Delete (i, t) Search for i. If the search is unsuccessful, splay at the last non-null node encountered during search. If the search is successful, let x be the node containing i. Assume x is not the root and let y be the parent of x. Replace x by an appropriate descendent of y in the usual fashion and then splay at y.

Illustrations on splay remove/delete: First Example: (Using the 2nd algorithm) Delete(30,t)

Splay at y I II III

Second example: (Using the 1st algorithm) Delete 20

TR TL TR

Disconnect the left and right subtrees from the root. Call these TL, and TR. respectively. Discard the root node.

TL

TR

Splay the maximum item in TL. This will result in the root of TL having no right child. (The largest element is at the root, so the root has no larger elements - no right subtree.)

TL

TR

The largest element from TL is splayed to the root.

The final tree.

TL TR

All the elements in TL are smaller than the elements in TR. Make TR be the right subtree of the root of TL (or vice-versa). PERFORMANCE CONSIDERATIONS The insert operation uses ordinary BST insertion, then splays the inserted node to the root. Clearly the insertion portion of this operation has the same asymptotic behavior as that for BSTs, namely O(depth). What about the splaying portion? If the work required at each step of the splay is independent of the number of nodes in the tree, the splay portion will be in O(1) per step. The number of steps is bounded by the depth of the tree, so the splaying portion would also be in O(depth). Well, is the splaying portion independent of the number of nodes in the tree? Look at what's involved: 1. 2. 3. Find the parent of the node and perhaps its parent too. Determine if the node and its parent are the same type (left or right). Rearrange the local structure of the tree.

Determining the type and rearranging the structure are local operations involving only a few nodes (the node, its parent, its grandparent). These operations must, be in O(1). The cost of determining the parent of a node depends on how the parent is found. If each node stores a reference to its parent (or if a stack of parents is maintained), the cost is in O(1), keeping the splay operation in O(depth). AMORTIZED COST For the average BST, d = O(lg n), so on average, operations on BSTs are in O(lg n)- A sequence of m operations (insert, remove, mid find} will cost O(m lg n). The situation with splay trees is different. The structure of the tree is changed with each operation, so the notion of average is not relevant. A technique known as "amortized analysis" is used to show that over a sequence of m operations (starting with an empty tree) the cost of the sequence will be O(m log n). This is the same cost as that for BSTs so overall performance is the same. What's different is that the splay tree does not guarantee O(lg n) performance for each operation. A Final Example Figure shows a splay tree before and after node "X" is accessed by find operation.

REFERENCES

1) Algorithms in Java: Part 1-4, 3rd Ed. By Robert Sedgewick. 2) Introduction to Algorithms, 2nd Ed. McGraw-Hill, Boston, 2001 By Thomas H.
Cormen, Charles E. Leiserson, Ron L. Rivest, and Clifford Stein. 3) Algorithm Design by John Wiley & Sons Inc. By Michael T. Goodrich and Roberto Tamassia. 4) http://www.wikipedia.org/wiki/Splay_tree 5) http://ocw.mit.edu/NR/rdonlyres/Electrical-Engineering-and-ComputerScience/6-854JFall-2005/68C23EF1-B915-4D37BBCF-71DBFB58D41A/0/dzhang_splaytree.pdf 6) http://ocw.mit.edu/NR/rdonlyres/Electrical-Engineering-and-ComputerScience/6-854JFall-2005/0BB08801-2680-4296-82DAD60C4C5EB66B/0/n3_splay.pdf 7) http://www.ecf.utoronto.ca/apsc/courses/ece242f/2002/notes.html 8) http://reports-archive.adm.cs.cmu.edu/anon/2004/CMU-CS-04-171.pdf 9) http://www.cs.umd.edu/class/spring2002/cmsc420-0401/ 10)http://www.cs.umd.edu/class/spring2002/cmsc420-0401/psplay.pdf 11)http://www.cse.msu.edu/~pramanik/teaching/courses/cse331/07sp/lectures/ splay/lec.pdf 12)http://reports-archive.adm.cs.cmu.edu/anon/2006/CMU-CS-06-140.pdf

Vous aimerez peut-être aussi