{"id":398,"date":"2018-07-19T06:52:21","date_gmt":"2018-07-19T06:52:21","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=398"},"modified":"2018-12-12T11:27:53","modified_gmt":"2018-12-12T11:27:53","slug":"splay-trees","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/splay-trees\/","title":{"rendered":"Splay Trees"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/md2rAORXodI\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we will discuss another self balbalanced tree \u2013 Splay Trees.<\/p>\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe learning objectives of the module are as follows:\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0 To understand the idea behind Splay Trees\r\n\r\n\u2022\u00a0 To discuss the cases of SplayTrees\r\n\r\n\u2022\u00a0 To describe the operations of Splay Trees\r\n\r\n\u2022\u00a0 To outline the advantages of Splay Trees\r\n\r\n&nbsp;\r\n\r\n<strong>27.1 Basic Idea<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The basic idea of the splay tree is thatevery time a node is accessed, assuming that access to this node will be needed immediately, it is pushed up to the root by a series of <strong><em>tree rotations<\/em><\/strong> knowing assplaying so that the search time for that node is the minimum. The idea is that if the node being splayedis deep, manynodes on the path to that node are alsodeep and by restructuring the tree, wemake access to all of those nodes cheaperin the future, again assuming that nodes nearer a node already accessed will be needed sooner than other nodes.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">We carry out a<strong>\u201c<\/strong>Blind\u201d rebalancing height information is kept. In spite of this the worst-case time per operation is O(<em>n<\/em>) but the worst-case amortized time is O(log <em>n<\/em>). We had already discussed amortized analysis as the worst case analysis of the average cost per operation of a sequence of operations. During Insert\/find operations we always rotate the inserted node or the node that is searched to the <em>root. <\/em>We maintain the property of good locality where we move the commonly accessed keys high up the tree so that they become easier and easier to find.<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-855 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-46.png\" alt=\"\" width=\"518\" height=\"227\" \/>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 27.1 Splay Tree<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">We move the node n (the node just inserted or just searched for) to the root by a series of zig-zag and zig-zig rotations. When we are forced to make a deep access, we ensure that we also fix up a lot of deep nodes towards the root (Figure 27.1).<\/p>\r\n&nbsp;\r\n\r\n<strong>27.2 Splay Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Splay trees are binary search trees (BSTs) that are not perfectly balanced all the time but allow search and insertion operations to try to balance the tree so that future operations can run faster. The splay trees are based on the heuristic that if a node X is accessed once, it is likely to be accessed again.A fter node X is accessed, we perform \u201csplaying\u201d operations to bring X up to the root of the tree. We perform splaying in such a manner that it leaves the tree more or less balanced as a whole.<\/p>\r\n&nbsp;\r\n\r\n<strong>27.2.1 Motivating Example<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us understand the concept of splay trees using a motivating example. In the example shown in Figure 27.2, after we search for 12, splaying with 12 makes the tree <strong>balanced<\/strong>and 12 moves to the root. Subsequent accesses for 12 will take <strong>O(1)<\/strong> time, We perform splay with a<strong>ctive (recently accessed)<\/strong> nodes, they will move towards the root and inactive nodes will slowly move further from the root<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-402 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-248.png\" alt=\"\" width=\"632\" height=\"328\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>27.3 Six Cases of Splaying<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let X be a non-root node, i.e., has at least 1 ancestor. Let P be its parent node and Let G be its grandparent node (if it exists). Now let us consider a path from G to X. Every time we traverse a left subtree, we have \u201czig\u201dtype of splay and correspondingly every time we traverse a right subtree, we have \u201czag\u201d type of splay. Now with this basis let us discuss the six cases of splaying (Figure 27.3). The first case is a \u201czig\u201d, when X is the left child of P. The second case is a \u201czag\u201d, when X is the right child of P. Next is \u201czig-zig\u201d when X is the left child of P and in turn P is left child of G. The fourth case is \u201czig-zag\u201d, when P is left child of G, but X is the right\u00a0<span style=\"font-size: 1em;text-align: initial\">child of P. The fifth case is \u201czag-zig\u201d, when P is right child of G, and X is the left child of P. The fifth case is \u201czag-zig\u201d, when P is right child of G, and X is the left child of P, the final sixth case is \u201czag-zag\u201d, when X is the right child of P and in turn P is right child of G.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-403 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-249.png\" alt=\"\" width=\"418\" height=\"373\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>27.4 Splay Operation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us discuss the splay operation in detail. We traverse the tree from node x we want to splay to root, rotating along the way until xis the root.<\/p>\r\n&nbsp;\r\n\r\n<strong>Rotation<\/strong>\r\n\r\n&nbsp;\r\n\r\nNow at each rotation, if x is the root, do nothing.\r\n\r\nIf x has no grandparent, <strong>rotate x about its parent<\/strong>.\r\n\r\nIf x has a grandparent,\r\n<ul>\r\n \t<li>if x and its parent are both left children or both right children<\/li>\r\n \t<li>rotate the parent about the grandparent, then<\/li>\r\n \t<li>rotate x about its parent<\/li>\r\n \t<li>if x and its parent are opposite type children (one left and the other right)<\/li>\r\n \t<li>rotate x about its parent, then<\/li>\r\n \t<li>Rotate x about its new parent (former grandparent)<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>27.5 Operations of Splay Trees<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">27.5.1 Insertion Operation<\/strong>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">As usual for every balanced tree we first insert as we do into a normal binary search tree. Our next step is to splay the inserted node. if there is a duplicate, the node that holds the duplicate element is splayed.<\/span>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">27.5.2 Find Operation<\/strong>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">We first search for the node, if we find the node we splay to the root. Otherwise we splay the last node that was found of the path we traversed while searching.<\/span>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">27.5.3 Splay Operation<\/strong>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">When node X is accessed either as a result of inserting X or during the find operation, we need to apply one of six rotation operations. In the splay terminology right rotates are called \u201czig\u201d while left rotates are called \u201cZag\u201d.<\/span>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">27.5.3.1 Single Rotations<\/strong>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">There are two cases of single rotations, zig and zag. This happens when X has a parent P but no grandparent G.<\/span>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">27.5.3.1.1 Zig Operation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u201cZig\u201d operation is just a single rotation, as in a tree. Suppose 6 was the node that was accessed (e.g. using Search). Here X (6) is a left child of the parent P (15) but no grandparent. Figure 27.4 shows the example of <\/span><strong style=\"text-align: justify;font-size: 1em\">Zig-Right,<\/strong><span style=\"text-align: justify;font-size: 1em\">a single right rotation of 6 about the pivot 15 whichmoves 6 to the root. This splay operation allows 6 to be accessed faster next time in the O(1).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-404 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-250.png\" alt=\"\" width=\"554\" height=\"208\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u201cZag\u201d is just a single rotation, again as in the AVL tree rotation. Suppose 15 was the node that was accessed (e.g., using Search). Here X (15) is a right child of the parent P (6) but no grandparent. Figure 27.5 shows the example of <strong>Zag-Left,<\/strong> a single left rotation of 15 about the pivot 6 which moves 15 to the root. This splay operation allows 15 to be accessed faster next time in the O(1).<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-405 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-251.png\" alt=\"\" width=\"547\" height=\"189\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>27.5.3.2 Double Rotations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Thereare four cases of double rotations, zig-zig, zig-zag, zag-zig, and zag-zag. This happens when X has a parent P and P has parent G.<\/p>\r\n&nbsp;\r\n\r\n<strong>27.5.3.2.1 Zig-Zig Operation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u201cZig-Zig\u201d consists of two single rotations of the same type. Suppose 3 was the node that was accessed (e.g., using Search). Here X (3) is the left child of parent P (6) and has a grandparent GP (15). P (6) is the left child of grandparent GP (15). Due to \u201czig-zig\u201d splaying, 3 has to be bubbled to the top (Figure 27.6). First we Zig-Right, a single right rotate of P (6) about GP (15). Now P (6) becomes the root whose left child is X (3). Now we again carry out a Zig-Right, a single right rotate of X (3) about P (6). Now X (3) becomes the root and will be accessed faster next time in the O(1). In this case, first P is right rotated about GP and then X is right rotated about P. Note that parent-grandparent is rotated first.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-406 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-252.png\" alt=\"\" width=\"586\" height=\"465\" \/>\r\n<p style=\"text-align: center\"><strong style=\"text-align: initial;font-size: 1em\">Figure 27.7 \u201cZag-Zig\u201d Operation<\/strong><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u201cZag-Zig\u201d consists of two consists of two rotations of the opposite type. Suppose 12 was the node that was accessed (e.g., using Search). Here X (12) is the right child of parent P (6) and has a grandparent GP (15). P (6) is the left child of grandparent GP (15). Due to \u201czag-zig\u201d splaying, 3 bubbles to the top (Figure 27.7). First we Zag-Left, a single left rotate of X (12) about P (6). Now P (6) becomes the left child of X (12). Now we again carry out a Zig-Right, a single right rotate of X (12) about GP (15). Now X (12) becomes the root and will be accessed faster next time in the O(1). In this case first X is left rotated about P and then X is again right rotated about GP. Notice that this is simply an LR imbalance correction in tree terminology (first a left rotation, then a right rotation).<\/p>\r\n&nbsp;\r\n\r\n<strong>27.5.3.2.3 Zig-Zag Operation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u201cZig-Zag\u201d consists of two consists of two rotations of the opposite type. Suppose 17 was the node that was accessed (e.g., using Search). Here X (17) is the left child of parent P (20) and has a grandparent GP (15). P (20) is the right child of grandparent GP (15). Due to \u201czig-zag\u201d splaying, 17 bubbles to the top (Figure 27.8). First we Zig-Right, a single right rotate of X (17) about P (20). Now P (20) becomes the right child of X (17). Now we again carry out a Zag-left, a single left rotate of X (17) about GP (15). Now X (17) becomes the root and will be accessed faster next time in the O(1). In this case first X is right rotated about P and then X is again left rotated about GP. Notice that this is simply an RL imbalance correction in AVL tree terminology (first a right rotation, then a left rotation).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-407 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-253.png\" alt=\"\" width=\"591\" height=\"477\" \/>\r\n\r\n<\/div>\r\n<strong>\u00a0\"<\/strong><span style=\"text-align: justify;font-size: 1em\">Zag-Zag\u201d consists of two single rotations of the same type. Suppose 30 was the node that was accessed (e.g., using Search). Here X (30) is the right child of parent P (20) and has a grandparent GP (15). P (20) is the right child of grandparent GP (15). Due to \u201czag-zag\u201d splaying, 30 has to be bubbled to the top (Figure 27.9). First we Zag-left, a single left rotate of P (20) about GP (15). Now P (20) becomes the root whose rightchild is X (30). Now we again carry out a Zag-Left, a single left rotate of X (30) about P (20). Now X (30) becomes the root and will be accessed faster next time in the O(1). In this case, first P is left rotated about GP and then X is left rotated about P. Note that parent-grandparent is rotated first.<\/span>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">27.6 Splay Trees: Examples<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-408 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-254.png\" alt=\"\" width=\"574\" height=\"304\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The first example we will see is a series of operations of the same type. In the example shown in Figure 27.10, 40 is accessed. 40 is down at the fifth level of the tree. Now we want to keep rotating till 40 bubbles to the top and becomes the root. Now X (40) is the left child of parent P (50) who is itself the left child of grandparent GP (60). We first carry out a Zig, right rotation of 40 about 50. Again we carry out a Zig, right rotation of 40 about 60. Now again we carry out a Zig, right rotation of 40 about 70, and again a Zig, right rotation of 40 about the root of the tree 80. Thus after a series of 4 Zig operations, the node last accessed 40, bubbles up to become the root so that next time it can be accessed fast.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-409 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-255.png\" alt=\"\" width=\"553\" height=\"241\" \/>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 27.11 Example where 60 is Accessed<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The second example we will see is a series of operations of the different types.In the example shown in Figure 27.11, 60 is accessed. 60 is down at the fourth level of the tree. Now we want to keep rotating till 60 bubbles to the top and becomes the root. Now X (60) is the right child of parent P (50) who is itself the left child of grandparent GP (70). We first carry out a Zag, left rotation of 60 about 50. Now we carry out a Zig, right rotation of 60 about 70. Now we carry out a Zag, left rotation of 60 about the root of the tree 40. Thus after a series of 3 operations, Zag, Zig and Zag operations, the node last accessed 60, bubbles up to become the root so that next time it can be accessed fast.<\/p>\r\n&nbsp;\r\n\r\n<strong>27.7 Why Splaying Helps<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we note that the node to be splayed n and its children are always helped that is go up the tree. Except for last step, nodes that are <em>hurt<\/em> by a zig-zag or zig-zig are later <em>helped<\/em> by a rotation higher up the tree. The result is that the shallow nodes may increase depth by one or two but other nodes decrease depth by a large amount. If a node <em>n<\/em> on the access path is at depth <em>d<\/em> before the splay, it\u2019s at about depth <em>d\/2<\/em> after the splay. Exceptions are the root, the child of the root, and the node that is splayed.<\/p>\r\n&nbsp;\r\n\r\n<strong>27.8 Splaying during other operations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Splaying can be done not just after search, but also after other operations such as Insert\/Delete. After an insert operation of node X, which is normally inserting X at a leaf node which is what will happen in a regular BST, we need to splay X up to the root. For deleting a node X, we carry out asearch on X and get X up to the root. We delete X at the root and move the largest item in its left sub-tree, i.e, its predecessor, to the root using splaying.<\/p>\r\n&nbsp;\r\n\r\n<strong>27.9Splaying Operations with Splitting Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In general, for an insert operation, we could do an ordinary BST insert, but that would not fix up the tree. We could do a BST insert followed by a find for splaying. However a better idea is to carry out the splay before the insert operation. For this purpose we need to split the tree T, Split(T, x) which creates two BST\u2019s L and R, such that all elements of T are in either L or R, all elements in L are \u00a3 x and all elements in R are &gt; x. Here x is the node to be inserted. Note that L and R share no elements. Now we insert at the root whose children will be L and R.<\/p>\r\n&nbsp;\r\n\r\n<strong>27.9.1 Splitting a Tree<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now the question is how do we split? We can find x or the <em>parent<\/em> of where x <em>would<\/em> <em>be <\/em>if we were to insert it as an ordinary BST. After this we can splay x or the parent to the root and then break one of the links from the root to a child (Figure 27.12).<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-410 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-256.png\" alt=\"\" width=\"478\" height=\"222\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">27.9.2 Insert Example with Splitting<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The insert example with splitting is shown in Figure 27.13. First we have the original tree. Now suppose we want to insert 5 then we need to split with 5. In other words we split the tree into two components L and R such that all nodes in L are \u00a35 and all nodes in R are &gt;5. In order to do this we need to move a node that is largest in the tree but which is less than 5 up the tree, in our example node 4. For this we carry out the appropriate splay operations. Now we join the tree by inserting 5 as the root with L and R as it\u2019s left and right sub trees respectively.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-411 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-257.png\" alt=\"\" width=\"522\" height=\"303\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>27.9.2 Delete Example with Splitting<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The delete example with splitting is shown in Figure 27.13. First we have the original tree. Now we first find 4, the node to be deleted, and splay 4. Now we have 4 at the root of the tree. We delete 4 and split the tree into two components L and R such that all nodes in L are &lt;4 and all nodes in R are &gt;4. Now we splay on the maximum element in L such that this maximum element is now the root of L. Now we join R as the right sub tree of this root of L. Now we have the tree with the node 4 deleted.<\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-412 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-258.png\" alt=\"\" width=\"579\" height=\"302\" \/>\r\n\r\n<strong>27.10Analysis of Splay Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The examples we have seen suggest that splaying causes the tree to get balanced. By analyzing the splay operation we can prove that any sequence of M operations on a splay tree of size N takes O(M log N) time. So, the amortized running time for one operation is O(log N).This guarantees that even if the depths of some nodes get very large, you cannot get a long sequence of O(N) searches because each search operation causes a rebalance. On the other hand without splaying, total time could be O(MN).<\/p>\r\n&nbsp;\r\n\r\n<strong>27.11 Advantages of Splay Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Splay trees are arguably the most practical kind of self-balancing trees. They do not need any extra information to be stored in the node, such as color, level, etc. These trees are balanced in an amortized sense that is running time on the average isO(mlogn) for m operations. The splay tree can be adapted to the ways in which items are being accessed in a dictionary to achieve faster running times for the frequently accessed items with (O(1)) while trees such as AVL is about O(log n). If number of finds is much larger than n, then locality is crucial, example in applications such as word-counting. Splay trees also support efficient split and join operations. They areand useful for other tasks such as for range queries.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Explained the idea behind Splay Trees<\/li>\r\n \t<li>Discussed the 6 cases of SplayTrees<\/li>\r\n \t<li>Described the operations of Splay Trees<\/li>\r\n \t<li>Outlined some of the advantages of Splay Trees<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Splay Trees<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/md2rAORXodI\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<img class=\"size-full wp-image-413 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-259.png\" alt=\"\" width=\"688\" height=\"538\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/md2rAORXodI\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we will discuss another self balbalanced tree \u2013 Splay Trees.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The learning objectives of the module are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 To understand the idea behind Splay Trees<\/p>\n<p>\u2022\u00a0 To discuss the cases of SplayTrees<\/p>\n<p>\u2022\u00a0 To describe the operations of Splay Trees<\/p>\n<p>\u2022\u00a0 To outline the advantages of Splay Trees<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.1 Basic Idea<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The basic idea of the splay tree is thatevery time a node is accessed, assuming that access to this node will be needed immediately, it is pushed up to the root by a series of <strong><em>tree rotations<\/em><\/strong> knowing assplaying so that the search time for that node is the minimum. The idea is that if the node being splayedis deep, manynodes on the path to that node are alsodeep and by restructuring the tree, wemake access to all of those nodes cheaperin the future, again assuming that nodes nearer a node already accessed will be needed sooner than other nodes.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We carry out a<strong>\u201c<\/strong>Blind\u201d rebalancing height information is kept. In spite of this the worst-case time per operation is O(<em>n<\/em>) but the worst-case amortized time is O(log <em>n<\/em>). We had already discussed amortized analysis as the worst case analysis of the average cost per operation of a sequence of operations. During Insert\/find operations we always rotate the inserted node or the node that is searched to the <em>root. <\/em>We maintain the property of good locality where we move the commonly accessed keys high up the tree so that they become easier and easier to find.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-855 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-46.png\" alt=\"\" width=\"518\" height=\"227\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-46.png 518w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-46-300x131.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-46-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-46-225x99.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-46-350x153.png 350w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 27.1 Splay Tree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We move the node n (the node just inserted or just searched for) to the root by a series of zig-zag and zig-zig rotations. When we are forced to make a deep access, we ensure that we also fix up a lot of deep nodes towards the root (Figure 27.1).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.2 Splay Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Splay trees are binary search trees (BSTs) that are not perfectly balanced all the time but allow search and insertion operations to try to balance the tree so that future operations can run faster. The splay trees are based on the heuristic that if a node X is accessed once, it is likely to be accessed again.A fter node X is accessed, we perform \u201csplaying\u201d operations to bring X up to the root of the tree. We perform splaying in such a manner that it leaves the tree more or less balanced as a whole.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.2.1 Motivating Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us understand the concept of splay trees using a motivating example. In the example shown in Figure 27.2, after we search for 12, splaying with 12 makes the tree <strong>balanced<\/strong>and 12 moves to the root. Subsequent accesses for 12 will take <strong>O(1)<\/strong> time, We perform splay with a<strong>ctive (recently accessed)<\/strong> nodes, they will move towards the root and inactive nodes will slowly move further from the root<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-402 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-248.png\" alt=\"\" width=\"632\" height=\"328\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-248.png 632w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-248-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-248-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-248-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-248-350x182.png 350w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.3 Six Cases of Splaying<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let X be a non-root node, i.e., has at least 1 ancestor. Let P be its parent node and Let G be its grandparent node (if it exists). Now let us consider a path from G to X. Every time we traverse a left subtree, we have \u201czig\u201dtype of splay and correspondingly every time we traverse a right subtree, we have \u201czag\u201d type of splay. Now with this basis let us discuss the six cases of splaying (Figure 27.3). The first case is a \u201czig\u201d, when X is the left child of P. The second case is a \u201czag\u201d, when X is the right child of P. Next is \u201czig-zig\u201d when X is the left child of P and in turn P is left child of G. The fourth case is \u201czig-zag\u201d, when P is left child of G, but X is the right\u00a0<span style=\"font-size: 1em;text-align: initial\">child of P. The fifth case is \u201czag-zig\u201d, when P is right child of G, and X is the left child of P. The fifth case is \u201czag-zig\u201d, when P is right child of G, and X is the left child of P, the final sixth case is \u201czag-zag\u201d, when X is the right child of P and in turn P is right child of G.<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-403 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-249.png\" alt=\"\" width=\"418\" height=\"373\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-249.png 418w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-249-300x268.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-249-65x58.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-249-225x201.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-249-350x312.png 350w\" sizes=\"auto, (max-width: 418px) 100vw, 418px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.4 Splay Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us discuss the splay operation in detail. We traverse the tree from node x we want to splay to root, rotating along the way until xis the root.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Now at each rotation, if x is the root, do nothing.<\/p>\n<p>If x has no grandparent, <strong>rotate x about its parent<\/strong>.<\/p>\n<p>If x has a grandparent,<\/p>\n<ul>\n<li>if x and its parent are both left children or both right children<\/li>\n<li>rotate the parent about the grandparent, then<\/li>\n<li>rotate x about its parent<\/li>\n<li>if x and its parent are opposite type children (one left and the other right)<\/li>\n<li>rotate x about its parent, then<\/li>\n<li>Rotate x about its new parent (former grandparent)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>27.5 Operations of Splay Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">27.5.1 Insertion Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">As usual for every balanced tree we first insert as we do into a normal binary search tree. Our next step is to splay the inserted node. if there is a duplicate, the node that holds the duplicate element is splayed.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">27.5.2 Find Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">We first search for the node, if we find the node we splay to the root. Otherwise we splay the last node that was found of the path we traversed while searching.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">27.5.3 Splay Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">When node X is accessed either as a result of inserting X or during the find operation, we need to apply one of six rotation operations. In the splay terminology right rotates are called \u201czig\u201d while left rotates are called \u201cZag\u201d.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">27.5.3.1 Single Rotations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">There are two cases of single rotations, zig and zag. This happens when X has a parent P but no grandparent G.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">27.5.3.1.1 Zig Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u201cZig\u201d operation is just a single rotation, as in a tree. Suppose 6 was the node that was accessed (e.g. using Search). Here X (6) is a left child of the parent P (15) but no grandparent. Figure 27.4 shows the example of <\/span><strong style=\"text-align: justify;font-size: 1em\">Zig-Right,<\/strong><span style=\"text-align: justify;font-size: 1em\">a single right rotation of 6 about the pivot 15 whichmoves 6 to the root. This splay operation allows 6 to be accessed faster next time in the O(1).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-404 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-250.png\" alt=\"\" width=\"554\" height=\"208\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-250.png 554w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-250-300x113.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-250-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-250-225x84.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-250-350x131.png 350w\" sizes=\"auto, (max-width: 554px) 100vw, 554px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u201cZag\u201d is just a single rotation, again as in the AVL tree rotation. Suppose 15 was the node that was accessed (e.g., using Search). Here X (15) is a right child of the parent P (6) but no grandparent. Figure 27.5 shows the example of <strong>Zag-Left,<\/strong> a single left rotation of 15 about the pivot 6 which moves 15 to the root. This splay operation allows 15 to be accessed faster next time in the O(1).<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-405 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-251.png\" alt=\"\" width=\"547\" height=\"189\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-251.png 547w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-251-300x104.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-251-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-251-225x78.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-251-350x121.png 350w\" sizes=\"auto, (max-width: 547px) 100vw, 547px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.5.3.2 Double Rotations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Thereare four cases of double rotations, zig-zig, zig-zag, zag-zig, and zag-zag. This happens when X has a parent P and P has parent G.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.5.3.2.1 Zig-Zig Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u201cZig-Zig\u201d consists of two single rotations of the same type. Suppose 3 was the node that was accessed (e.g., using Search). Here X (3) is the left child of parent P (6) and has a grandparent GP (15). P (6) is the left child of grandparent GP (15). Due to \u201czig-zig\u201d splaying, 3 has to be bubbled to the top (Figure 27.6). First we Zig-Right, a single right rotate of P (6) about GP (15). Now P (6) becomes the root whose left child is X (3). Now we again carry out a Zig-Right, a single right rotate of X (3) about P (6). Now X (3) becomes the root and will be accessed faster next time in the O(1). In this case, first P is right rotated about GP and then X is right rotated about P. Note that parent-grandparent is rotated first.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-406 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-252.png\" alt=\"\" width=\"586\" height=\"465\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-252.png 586w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-252-300x238.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-252-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-252-225x179.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-252-350x278.png 350w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/p>\n<p style=\"text-align: center\"><strong style=\"text-align: initial;font-size: 1em\">Figure 27.7 \u201cZag-Zig\u201d Operation<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u201cZag-Zig\u201d consists of two consists of two rotations of the opposite type. Suppose 12 was the node that was accessed (e.g., using Search). Here X (12) is the right child of parent P (6) and has a grandparent GP (15). P (6) is the left child of grandparent GP (15). Due to \u201czag-zig\u201d splaying, 3 bubbles to the top (Figure 27.7). First we Zag-Left, a single left rotate of X (12) about P (6). Now P (6) becomes the left child of X (12). Now we again carry out a Zig-Right, a single right rotate of X (12) about GP (15). Now X (12) becomes the root and will be accessed faster next time in the O(1). In this case first X is left rotated about P and then X is again right rotated about GP. Notice that this is simply an LR imbalance correction in tree terminology (first a left rotation, then a right rotation).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.5.3.2.3 Zig-Zag Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u201cZig-Zag\u201d consists of two consists of two rotations of the opposite type. Suppose 17 was the node that was accessed (e.g., using Search). Here X (17) is the left child of parent P (20) and has a grandparent GP (15). P (20) is the right child of grandparent GP (15). Due to \u201czig-zag\u201d splaying, 17 bubbles to the top (Figure 27.8). First we Zig-Right, a single right rotate of X (17) about P (20). Now P (20) becomes the right child of X (17). Now we again carry out a Zag-left, a single left rotate of X (17) about GP (15). Now X (17) becomes the root and will be accessed faster next time in the O(1). In this case first X is right rotated about P and then X is again left rotated about GP. Notice that this is simply an RL imbalance correction in AVL tree terminology (first a right rotation, then a left rotation).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-407 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-253.png\" alt=\"\" width=\"591\" height=\"477\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-253.png 591w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-253-300x242.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-253-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-253-225x182.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-253-350x282.png 350w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/p>\n<\/div>\n<p><strong>\u00a0&#8220;<\/strong><span style=\"text-align: justify;font-size: 1em\">Zag-Zag\u201d consists of two single rotations of the same type. Suppose 30 was the node that was accessed (e.g., using Search). Here X (30) is the right child of parent P (20) and has a grandparent GP (15). P (20) is the right child of grandparent GP (15). Due to \u201czag-zag\u201d splaying, 30 has to be bubbled to the top (Figure 27.9). First we Zag-left, a single left rotate of P (20) about GP (15). Now P (20) becomes the root whose rightchild is X (30). Now we again carry out a Zag-Left, a single left rotate of X (30) about P (20). Now X (30) becomes the root and will be accessed faster next time in the O(1). In this case, first P is left rotated about GP and then X is left rotated about P. Note that parent-grandparent is rotated first.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">27.6 Splay Trees: Examples<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-408 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-254.png\" alt=\"\" width=\"574\" height=\"304\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-254.png 574w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-254-300x159.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-254-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-254-225x119.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-254-350x185.png 350w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The first example we will see is a series of operations of the same type. In the example shown in Figure 27.10, 40 is accessed. 40 is down at the fifth level of the tree. Now we want to keep rotating till 40 bubbles to the top and becomes the root. Now X (40) is the left child of parent P (50) who is itself the left child of grandparent GP (60). We first carry out a Zig, right rotation of 40 about 50. Again we carry out a Zig, right rotation of 40 about 60. Now again we carry out a Zig, right rotation of 40 about 70, and again a Zig, right rotation of 40 about the root of the tree 80. Thus after a series of 4 Zig operations, the node last accessed 40, bubbles up to become the root so that next time it can be accessed fast.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-409 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-255.png\" alt=\"\" width=\"553\" height=\"241\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-255.png 553w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-255-300x131.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-255-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-255-225x98.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-255-350x153.png 350w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 27.11 Example where 60 is Accessed<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The second example we will see is a series of operations of the different types.In the example shown in Figure 27.11, 60 is accessed. 60 is down at the fourth level of the tree. Now we want to keep rotating till 60 bubbles to the top and becomes the root. Now X (60) is the right child of parent P (50) who is itself the left child of grandparent GP (70). We first carry out a Zag, left rotation of 60 about 50. Now we carry out a Zig, right rotation of 60 about 70. Now we carry out a Zag, left rotation of 60 about the root of the tree 40. Thus after a series of 3 operations, Zag, Zig and Zag operations, the node last accessed 60, bubbles up to become the root so that next time it can be accessed fast.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.7 Why Splaying Helps<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we note that the node to be splayed n and its children are always helped that is go up the tree. Except for last step, nodes that are <em>hurt<\/em> by a zig-zag or zig-zig are later <em>helped<\/em> by a rotation higher up the tree. The result is that the shallow nodes may increase depth by one or two but other nodes decrease depth by a large amount. If a node <em>n<\/em> on the access path is at depth <em>d<\/em> before the splay, it\u2019s at about depth <em>d\/2<\/em> after the splay. Exceptions are the root, the child of the root, and the node that is splayed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.8 Splaying during other operations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Splaying can be done not just after search, but also after other operations such as Insert\/Delete. After an insert operation of node X, which is normally inserting X at a leaf node which is what will happen in a regular BST, we need to splay X up to the root. For deleting a node X, we carry out asearch on X and get X up to the root. We delete X at the root and move the largest item in its left sub-tree, i.e, its predecessor, to the root using splaying.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.9Splaying Operations with Splitting Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In general, for an insert operation, we could do an ordinary BST insert, but that would not fix up the tree. We could do a BST insert followed by a find for splaying. However a better idea is to carry out the splay before the insert operation. For this purpose we need to split the tree T, Split(T, x) which creates two BST\u2019s L and R, such that all elements of T are in either L or R, all elements in L are \u00a3 x and all elements in R are &gt; x. Here x is the node to be inserted. Note that L and R share no elements. Now we insert at the root whose children will be L and R.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.9.1 Splitting a Tree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now the question is how do we split? We can find x or the <em>parent<\/em> of where x <em>would<\/em> <em>be <\/em>if we were to insert it as an ordinary BST. After this we can splay x or the parent to the root and then break one of the links from the root to a child (Figure 27.12).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-410 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-256.png\" alt=\"\" width=\"478\" height=\"222\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-256.png 478w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-256-300x139.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-256-65x30.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-256-225x104.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-256-350x163.png 350w\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">27.9.2 Insert Example with Splitting<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The insert example with splitting is shown in Figure 27.13. First we have the original tree. Now suppose we want to insert 5 then we need to split with 5. In other words we split the tree into two components L and R such that all nodes in L are \u00a35 and all nodes in R are &gt;5. In order to do this we need to move a node that is largest in the tree but which is less than 5 up the tree, in our example node 4. For this we carry out the appropriate splay operations. Now we join the tree by inserting 5 as the root with L and R as it\u2019s left and right sub trees respectively.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-411 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-257.png\" alt=\"\" width=\"522\" height=\"303\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-257.png 522w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-257-300x174.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-257-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-257-225x131.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-257-350x203.png 350w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.9.2 Delete Example with Splitting<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The delete example with splitting is shown in Figure 27.13. First we have the original tree. Now we first find 4, the node to be deleted, and splay 4. Now we have 4 at the root of the tree. We delete 4 and split the tree into two components L and R such that all nodes in L are &lt;4 and all nodes in R are &gt;4. Now we splay on the maximum element in L such that this maximum element is now the root of L. Now we join R as the right sub tree of this root of L. Now we have the tree with the node 4 deleted.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-412 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-258.png\" alt=\"\" width=\"579\" height=\"302\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-258.png 579w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-258-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-258-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-258-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-258-350x183.png 350w\" sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><\/p>\n<p><strong>27.10Analysis of Splay Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The examples we have seen suggest that splaying causes the tree to get balanced. By analyzing the splay operation we can prove that any sequence of M operations on a splay tree of size N takes O(M log N) time. So, the amortized running time for one operation is O(log N).This guarantees that even if the depths of some nodes get very large, you cannot get a long sequence of O(N) searches because each search operation causes a rebalance. On the other hand without splaying, total time could be O(MN).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.11 Advantages of Splay Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Splay trees are arguably the most practical kind of self-balancing trees. They do not need any extra information to be stored in the node, such as color, level, etc. These trees are balanced in an amortized sense that is running time on the average isO(mlogn) for m operations. The splay tree can be adapted to the ways in which items are being accessed in a dictionary to achieve faster running times for the frequently accessed items with (O(1)) while trees such as AVL is about O(log n). If number of finds is much larger than n, then locality is crucial, example in applications such as word-counting. Splay trees also support efficient split and join operations. They areand useful for other tasks such as for range queries.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Explained the idea behind Splay Trees<\/li>\n<li>Discussed the 6 cases of SplayTrees<\/li>\n<li>Described the operations of Splay Trees<\/li>\n<li>Outlined some of the advantages of Splay Trees<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Splay Trees<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/md2rAORXodI\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-413 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-259.png\" alt=\"\" width=\"688\" height=\"538\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-259.png 688w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-259-300x235.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-259-65x51.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-259-225x176.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-259-350x274.png 350w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/p>\n","protected":false},"author":3,"menu_order":27,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-t-v-geetha"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-398","chapter","type-chapter","status-publish","hentry","contributor-dr-t-v-geetha"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":11,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/398\/revisions"}],"predecessor-version":[{"id":959,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/398\/revisions\/959"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/398\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=398"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=398"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=398"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}