Spla Trees 3/20/14 Presentation for use with the tetbook Data Structures and Algorithms in Java, 6 th edition, b M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wile, 2014 Spla Trees v 6 3 8 4 2013 Goodrich, Tamassia, Goldwasser Spla Trees 1 Spla Trees are Binar Search Trees! BST Rules: n entries stored onl at internal nodes n kes stored at nodes in the left subtree of v are less than or equal to the ke stored at v n kes stored at nodes in the right subtree of v are greater than or equal to the ke stored at v! An inorder traversal will return the kes in order note that two kes of equal value ma be wellseparated Slide b Matt Dickerson all the kes in the blue region are 20 all the kes in the ellow region are 20 2013 Goodrich, Tamassia, Goldwasser Spla Trees 2 1
Spla Trees 3/20/14 Searching in a Spla Tree: Starts the Same as in a BST! Search proceeds down the tree to found item or an eternal node.! Eample: Search for time with ke 11. Slide b Matt Dickerson 2013 Goodrich, Tamassia, Goldwasser Spla Trees 3 Eample Searching in a BST, continued! search for ke 8, ends at an internal node. Slide b Matt Dickerson 2013 Goodrich, Tamassia, Goldwasser Spla Trees 4 2
Spla Trees 3/20/14 Spla Trees do Rotations after Ever Operation (Even Search)! new operation: spla n splaing moves a node to the root using rotations n right rotation n left rotation n makes the left child of a node n makes the right child of a node into s parent; becomes the into s parent; becomes the left right child of child of a right rotation about a left rotation about T 2 T 2 (structure of tree above is not modified) T 2 (structure of tree above is not modified) T 2 2013 Goodrich, Tamassia, Goldwasser Spla Trees 5 start with node Splaing: is the root? no is a child of the root? es is the left child of the root? es ig es no no ig stop n is a left-left grandchild means is a left child of its parent, which is itself a left child of its parent n p is s parent; g is p s parent is a left-left grandchild? is a right-right grandchild? is a right-left grandchild? is a left-right grandchild? right-rotate about g, right-rotate about p left-rotate about g, left-rotate about p left-rotate about p, right-rotate about g right-rotate left-rotate about es right-rotate about p, about the root the root left-rotate about g 2013 Goodrich, Tamassia, Goldwasser Spla Trees Slide b Matt Dickerson 6 es es es ig-ig ig-ig ig-ag ig-ag 3
Spla Trees 3/20/14 Visualiing the Splaing Cases ig-ag T 2 ig-ig T 2 T 2 ig w w T 2 T 2 T 2 2013 Goodrich, Tamassia, Goldwasser Spla Trees 7 Splaing Eample! let = n is the right child of its parent, which is the left child of the grandparent n left-rotate around p, then rightrotate around g p g 1. (before rotating) Slide b Matt Dickerson g p p g 2. (after first rotation) 3. is not et the root, so 2013 Goodrich, Tamassia, Goldwasser Spla Trees we spla again 8 (after second rotation) 4
Spla Trees 3/20/14 Splaing Eample, Continued! now is the left child of the root n right-rotate around root 1. (before appling rotation) 2. (after rotation) is the root, so stop 2013 Goodrich, Tamassia, Goldwasser Spla Trees Slide b Matt Dickerson 9 Slide b Matt Dickerson Eample Result of Splaing! tree might not be more balanced! e.g. spla n before, the depth of the shallowest leaf is 3 and the deepest is 7 n after, the depth of shallowest leaf is 1 and deepest is 8 before after first spla 2013 Goodrich, Tamassia, Goldwasser Spla Trees 10 after second spla 5
Spla Trees 3/20/14 Spla Tree Definition! a spla tree is a binar search tree where a node is splaed after it is accessed (for a search or update) n deepest internal node accessed is splaed n splaing costs O(h), where h is height of the tree which is still O(n) worst-case w O(h) rotations, each of which is O(1) 2013 Goodrich, Tamassia, Goldwasser Spla Trees 11 Spla Trees & Ordered Dictionaries! which nodes are splaed after each operation? method Search for k spla node if ke found, use that node if ke not found, use parent of ending eternal node Insert (k,v) Remove item with ke k use the new node containing the entr inserted use the parent of the internal node that was actuall removed from the tree (the parent of the node that the removed item was swapped with) 2013 Goodrich, Tamassia, Goldwasser Spla Trees 12 6
Spla Trees 3/20/14 Amortied Analsis of Spla Trees! Running time of each operation is proportional to time for splaing.! Define rank(v) as the logarithm (base 2) of the number of nodes in subtree rooted at v.! Costs: ig = $1, ig-ig = $2, ig-ag = $2.! Thus, cost for plaing a node at depth d = $d.! Imagine that we store rank(v) cber-dollars at each node v of the spla tree (just for the sake of analsis). 2013 Goodrich, Tamassia, Goldwasser Spla Trees 13 Cost per ig ig w w T 2 T 2! Doing a ig at costs at most rank () - rank(): n cost = rank () + rank () - rank() - rank() < rank () - rank(). 2013 Goodrich, Tamassia, Goldwasser Spla Trees 14 7
Spla Trees 3/20/14 Cost per ig-ig and ig-ag ig-ig T 2 T 2! Doing a ig-ig or ig-ag at costs at most 3(rank () - rank()) - 2 ig-ag T 2 T 2 2013 Goodrich, Tamassia, Goldwasser Spla Trees 15 Cost of Splaing! Cost of splaing a node at depth d of a tree rooted at r: n at most 3(rank(r) - rank()) - d + 2: n Proof: Splaing takes d/2 splaing substeps: cost d / 2 i= 1 d / 2 i= 1 cost i (3(rank ( ) rank = 3(rank( r) rank ( )) 2( d / d ) + 2 3(rank( r) rank( )) d + 2. ( )) 2) + 2 2013 Goodrich, Tamassia, Goldwasser Spla Trees 16 i 0 i 1 8
Spla Trees 3/20/14 Performance of Spla Trees! Recall: rank of a node is logarithm of its sie.! Thus, amortied cost of an spla operation is O(log n)! In fact, the analsis goes through for an reasonable definition of rank()! This implies that spla trees can actuall adapt to perform searches on frequentlrequested items much faster than O(log n) in some cases 2013 Goodrich, Tamassia, Goldwasser Spla Trees 17 Java Implementation 2013 Goodrich, Tamassia, Goldwasser Spla Trees 18 9
Spla Trees 3/20/14 Java Implementation 2013 Goodrich, Tamassia, Goldwasser Spla Trees 19 10