Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006.
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
Jim Anderson Comp 750, Fall 2009 Splay Trees - 1 Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there.
Amortized Analysis In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed Aggregate.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
Amortized Analysis.
Lecture 23 Red Black Tree Chapter 10 of textbook
Part-D1 Binary Search Trees
Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000
Andreas Klappenecker [partially based on the slides of Prof. Welch]
COMP9024: Data Structures and Algorithms
BCA-II Data Structure Using C
Red-Black Trees v z Red-Black Trees Red-Black Trees
Data Structures – LECTURE Balanced trees
Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Binary Search Tree (BST)
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
Splay Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Summary of General Binary search tree
Lecture 7 Algorithm Analysis
Lecture 25 Splay Tree Chapter 10 of textbook
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees Motivations
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Multi-Way Search Trees
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Lecture 9 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
SPLAY TREES.
Lecture 7 Algorithm Analysis
Trees CMSC 202, Version 5/02.
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
Binary Search Trees < > =
CMSC 202 Trees.
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
Topics covered (since exam 1, excluding PQ):
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
CMSC 341 Splay Trees.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees.
Red-Black Trees.
AVL-Trees (Part 1).
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Richard Anderson Spring 2016
CMSC 341 Splay Trees.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CMSC 341 Splay Trees.
1 Lecture 13 CS2013.
Chapter 12&13: Binary Search Trees (BSTs)
326 Lecture 9 Henry Kautz Winter Quarter 2002
Splay Trees Binary search trees.
Presentation transcript:

Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete operations are like in binary search trees, except at the end of each operation a special step called splaying is done. Splaying ensures that all operations take O(lg n) amortized time. First, a quick review of BST operations… Comp 750, Fall 2009

BST: Search Search(25) Search(76) Note: In the handout, sentinel leaf nodes are assumed.  tree with n keys has 2n+1 nodes. 44 17 88 32 65 97 28 54 82 29 76 80 Comp 750, Fall 2009

BST: Insert Insert(78) 44 17 88 32 65 97 28 54 82 29 76 80 78 Comp 750, Fall 2009

BST: Delete 44 Delete(32) 17 88 32 65 97 Has only one child: just splice out 32. 28 54 82 29 76 80 78 Comp 750, Fall 2009

BST: Delete 44 Delete(32) 17 88 28 65 97 29 54 82 76 80 78 Comp 750, Fall 2009

BST: Delete 44 Delete(65) 17 88 32 65 97 Has two children: Replace 65 by successor, 76, and splice out successor. Note: Successor can have at most one child. (Why?) 28 54 82 29 76 80 78 Comp 750, Fall 2009

BST: Delete 44 Delete(65) 17 88 32 76 97 28 54 82 29 80 78 Comp 750, Fall 2009

Splaying In splay trees, after performing an ordinary BST Search, Insert, or Delete, a splay operation is performed on some node x (as described later). The splay operation moves x to the root of the tree. The splay operation consists of sub-operations called zig-zig, zig-zag, and zig. Comp 750, Fall 2009

Note: x’s depth decreases by two. Zig-Zig z x x has a grandparent 10 30 y y 20 20 T1 T4 x z 30 10 T2 T3 T3 T4 T1 T2 (Symmetric case too) Note: x’s depth decreases by two. Comp 750, Fall 2009

Note: x’s depth decreases by two. Zig-Zag z x x has a grandparent 10 20 y 30 T1 x z y 20 10 30 T4 T2 T3 T1 T2 T3 T4 (Symmetric case too) Note: x’s depth decreases by two. Comp 750, Fall 2009

Zig x has no grandparent (so, y is the root) Note: w could be NIL y x 10 20 x 20 T1 w y w 30 10 30 T2 T3 T4 T1 T2 T3 T4 (Symmetric case too) Note: x’s depth decreases by one. Comp 750, Fall 2009

Complete Example 44 50 Splay(78) 17 88 32 65 97 zig-zag 28 54 82 z 29 76 y 80 x 78 Comp 750, Fall 2009

Complete Example 44 50 Splay(78) 17 88 32 65 97 zig-zag 28 54 82 x 29 76 80 Comp 750, Fall 2009

Complete Example 44 50 Splay(78) 17 88 zig-zag z 32 65 97 y 28 54 82 x 29 78 76 80 Comp 750, Fall 2009

Complete Example 44 50 Splay(78) 17 88 x 32 78 97 zig-zag 28 z 65 y 82 29 54 76 80 Comp 750, Fall 2009

Complete Example 44 z zig-zag 50 Splay(78) 17 88 y x 32 78 97 28 65 82 29 54 76 80 Comp 750, Fall 2009

Complete Example 44 x 78 Splay(78) 17 88 y 50 z 82 32 97 65 zig-zag 80 28 54 76 29 Comp 750, Fall 2009

Complete Example zig y 44 x 78 Splay(78) w 17 88 50 82 32 97 65 80 28 54 76 29 Comp 750, Fall 2009

Complete Example x 78 y 44 Splay(78) w 17 88 50 82 32 97 65 zig 80 28 54 76 29 Comp 750, Fall 2009

Result of splaying The result is a binary tree, with the left subtree having all keys less than the root, and the right subtree having keys greater than the root. Also, the final tree is “more balanced” than the original. However, if an operation near the root is done, the tree can become less balanced. Comp 750, Fall 2009

When to Splay Search: Insert: Delete: Successful: Splay node where key was found. Unsuccessful: Splay last-visited internal node (i.e., last node with a key). Insert: Splay newly added node. Delete: Splay parent of removed node (which is either the node with the deleted key or its successor). Note: All operations run in O(h) time, for a tree of height h. Comp 750, Fall 2009

Amortized Analysis Based on the Accounting Method (see Sec. 17.2 of CLRS). Idea: When an operation’s amortized cost exceeds it actual cost, the difference is assigned to certain tree nodes as credit. Credit is used to pay for subsequent operations whose amortized cost is less than their actual cost. Most of our analysis will focus on splaying. The BST operations will be easily dealt with at the end. Comp 750, Fall 2009

Review: Accounting Method Stack Example: Operations: Push(S, x). Pop(S). Multipop(S, k): if stack has s items, pop off min(s, k) items. Can implement in O(1) time. s ≥ k items Multipop(S, k) s – k items Multipop(S, k) s  k items 0 items Comp 750, Fall 2009

Accounting Method (Continued) We charge each operation an amortized cost. Charge may be more or less than actual cost. If more, then we have credit. This credit can be used to pay for future operations whose amortized cost is less than their actual cost. Require: For any sequence of operations, amortized cost upper bounds worst-case cost. That is, we always have nonnegative credit. Comp 750, Fall 2009

Accounting Method (Continued) Stack Example: Actual Costs: Push: 1 Pop: 1 Multipop: min(s, k) Amortized Costs: Push: 2 Pop: 0 Multipop: 0 For a sequence of n operations, does total amortized cost upper bound total worst-case cost, as required? What is the total worst- case cost of the sequence? All O(1). Pays for the push and a future pop. Comp 750, Fall 2009

Ranks T is a splay tree with n keys. Definition: The size of node v in T, denoted n(v), is the number of nodes in the subtree rooted at v. Note: The root is of size 2n+1. Definition: The rank of v, denoted r(v), is lg(n(v)). Note: The root has rank lg(2n+1). Definition: r(T) = vT r(v). Comp 750, Fall 2009

Meaning of Ranks The rank of a tree is a measure of how well balanced it is. A well balanced tree has a low rank. A badly balanced tree has a high rank. The splaying operations tend to make the rank smaller, which balances the tree and makes other operations faster. Some operations near the root may make the rank larger and slightly unbalance the tree. Amortized analysis is used on splay trees, with the rank of the tree being the potential Comp 750, Fall 2009

Credit Invariant We will define amortized costs so that the following invariant is maintained. So, each operation’s amortized cost = its real cost + the total change in r(T) it causes (positive or negative). Let Ri = op. i’s real cost and i = change in r(T) it causes. Total am. cost = i=1,…,n (Ri + i). Initial tree has rank 0 & final tree has non-neg. rank. So, i=1,…n i  0, which implies total am. cost  total real cost. Each node v of T has r(v) credits in its account. Comp 750, Fall 2009

What’s Left? We want to show that the per-operation amortized cost is logarithmic. To do this, we need to look at how BST operations and splay operations affect r(T). We spend most of our time on splaying, and consider the specific BST operations later. To analyze splaying, we first look at how r(T) changes as a result of a single substep, i.e., zig, zig-zig, or zig-zag. Notation: Ranks before and after a substep are denoted r(v) and r(v), respectively. Comp 750, Fall 2009

Proposition 13.6 Proposition 13.6: Let  be the change in r(T) caused by a single substep. Let x be the “x” in our descriptions of these substeps. Then,   3(r(x)  r(x))  2 if the substep is a zig-zig or a zig-zag;   3(r(x)  r(x)) if the substep is a zig. Proof: Three cases, one for each kind of substep… Comp 750, Fall 2009

Case 1: zig-zig Only the ranks of x, y, and 10 30 y Only the ranks of x, y, and z change. Also, r(x) = r(z), r(y)  r(x), and r(y)  r(x). Thus,  = r(x) + r(y) + r(z) – r(x) – r(y) – r(z) = r(y) + r(z) – r(x) – r(y)  r(x) + r(z) – 2r(x). (*) Also, n(x) + n(z)  n(x), which (by property of lg), implies r(x) + r(z)  2r(x) – 2, i.e., r(z)  2r(x) – r(x) – 2. (**) By (*) and (**),   r(x) + (2r(x) – r(x) – 2) – 2r(x) = 3(r(x) – r(x)) – 2. y 20 20 T1 T4 x z 30 10 T2 T3 T3 T4 T1 T2 If a > 0, b > 0, and c  a + b, then lg a + lg b  2 lg c – 2. Comp 750, Fall 2009

Case 2: zig-zag Only the ranks of x, y, and 10 30 20 T3 T4 T2 T1 z y x Only the ranks of x, y, and z change. Also, r(x) = r(z) and r(x)  r(y). Thus,  = r(x) + r(y) + r(z) – r(x) – r(y) – r(z) = r(y) + r(z) – r(x) – r(y)  r(y) + r(z) – 2r(x). (*) Also, n(y) + n(z)  n(x), which (by property of lg), implies r(y) + r(z)  2r(x) – 2. (**) By (*) and (**),   2r(x) – 2 – 2r(x)  3(r(x) – r(x)) – 2. Comp 750, Fall 2009

Case 3: zig Only the ranks of x and y change. 20 10 T1 T2 T3 T4 x y 30 w Only the ranks of x and y change. Also, r(y)  r(y) and r(x)  r(x). Thus,  = r(x) + r(y) – r(x) – r(y)  r(x) – r(x)  3(r(x) – r(x)). Comp 750, Fall 2009

Proposition 13.7 Proposition 13.7: Let T be a splay tree with root t, and let  be the total variation of r(T) caused by splaying a node x at depth d. Then,   3(r(t)  r(x))  d + 2. Proof: Splay(x) consists of p = d/2 substeps, each of which is a zig-zig or zig-zag, except possibly the last one, which is a zig if d is odd. Let r0(x) = x’s initial rank, ri(x) = x’s rank after the ith substep, and i = the variation of r(T) caused by the ith substep, where 1  i  p. By Proposition 13.6, Comp 750, Fall 2009

Meaning of Proposition If d is small (less than 3(r(t)  r(x)) + 2) then the splay operation can increase r(t) and thus make the tree less balanced. If d is larger than this, then the splay operation decreases r(t) and thus makes the tree better balanced. Note that r(t)  3lg(2n + 1) Comp 750, Fall 2009

Amortized Costs As stated before, each operation’s amortized cost = its real cost + the total change in r(T) it causes, i.e., . This ensures the Credit Invariant isn’t violated. Real cost is d, so amortized cost is d + . The real cost of d even includes the cost of binary tree operations such as searching. Note:  can be positive or negative (or zero). If it’s positive, we’re overcharging. If it’s negative, we’re undercharging. Comp 750, Fall 2009

Another Look at   = the total change in r(T). Consider this example: splay(b) n(a) b a 4 4  < 0 a b 3 1 c 2 splay(a) c 2 d 1 d 1  > 0 r(T) = lg(4  3  2  1) = lg(24) r(T) = lg(4  2  1  1) = lg(8) Comp 750, Fall 2009

Unbalancing the Tree In fact, a sequence of zig operations can result in a completely unbalanced linear tree. Then a search operation can take O(n) time, but this is OK because at least n operations have been performed up to this point. Comp 750, Fall 2009

A Bound on Amortized Cost We have: Amortized Cost of Splaying = d +   d + (3(r(t) – r(x)) – d + 2) {Prop. 13.7} = 3(r(t) – r(x)) + 2 < 3r(t) + 2 = 3lg(2n + 1) + 2 {Recall t is the root} = O(lg n) Comp 750, Fall 2009

Finishing Up Until now, we’ve just focused on splaying costs. We also need to ensure that BST operations can be charged in a way that maintains the Credit Invariant. Three Cases: Search: Not a problem — doesn’t change the tree. Delete: Not a problem — removing a node can only decrease ranks, so existing credits are still fine. Insert: As shown next, an Insert can cause r(T) to increase by up to lg(2n+3) + lg 3. Thus, the Credit Invariant can be maintained if Insert is assessed an O(lg n) charge. Comp 750, Fall 2009

Insert k Insert(78) 44 vd 44 88 88 65 65 Insert(k) 82 82 76 v2 76 80 v = v0 78 Comp 750, Fall 2009

Insert Lots of typos in book! For i = 1, …, d, let n(vi) and n'(vi) be sizes before and after insertion, and r(vi) and r'(vi) be ranks before and after insertion. We have: n'(vi) = n(vi) + 2. For i = 1, …, d – 1, n(vi) + 2  n(vi+1), and r'(vi) = lg(n'(vi)) = lg(n(vi) + 2)  lg(n(vi+1)) = r(vi+1). Thus, i=1..d (r'(vi) – r(vi))  r'(vd) – r(vd) + i=1..d-1(r(vi+1) – r(vi)) = r'(vd) – r(vd) + r(vd) – r(v1)  lg(2n + 3). Thus, the Credit Invariant can be maintained if Insert is assessed a charge of at most lg(2n + 3) + lg 3. Leaf gets replaced by “real” node and two leaves. Subtree at vi doesn’t include vi+1 and its “other” child. Note: v0 is excluded here – it doesn’t have an old rank! It’s new rank is lg 3. Comp 750, Fall 2009

Proposition 13.8 We’ve established the following: Proposition 13.8: Consider a sequence of m operations on a splay tree, each a search, insertion, or deletion, starting from a splay tree with zero keys. Also, let ni be the number of keys in the tree after operation i, and n be the total number of insertions. The total running time for performing the sequence of operations is O(m + i=1..m lg ni) which is O(m lg n). Comp 750, Fall 2009

Proposition 13.9 The following can also be shown: Proposition 13.9: Consider a sequence of m operations on a splay tree, each a search, insertion, or deletion, starting from a splay tree with zero keys. Also, let f(i) denote the number of times item i is accessed, and let n be the total number of insertions. Assuming each item is accessed at least once, the total running time for performing the sequence of operations is O(m + i=1..n f(i)lg(m/f(i))). In other words, splay trees adapt to the accesses being performed: the more an item is accessed, the lower the associated amortized cost!!! Comp 750, Fall 2009