CS210- Lecture 18 July 14, 2005 Agenda Skip Lists Update operations on Skip Lists Binary Search Trees 6/30/2016 CS210-Summer 2005, Lecture 18 1 What is a Skip List A skip list for a set S of distinct (key, element) items is a series of lists S0, S1 , … , Sh such that Each list Si contains the special keys + and - List S0 contains the keys of S in nondecreasing order Each list is a subsequence of the previous one, i.e., S0 S1 … Sh List Sh contains only the two special keys We show how to use a skip list to implement the dictionary ADT S3 - S2 - S1 - S0 - 6/30/2016 + + 31 23 12 23 26 31 34 31 34 + 64 44 56 CS210-Summer 2005, Lecture 18 64 78 + 2 Search We search for a key x in a a skip list as follows: We start at the first position of the top list At the current position p, we compare x with y key(next(p)) x = y: we return element(next(p)) x > y: we “scan forward” x < y: we “drop down” If we try to drop down past the bottom list, we return null Example: search for 78 S3 - S2 - S1 - S0 - 6/30/2016 + + 31 23 12 23 26 31 34 31 34 + 64 44 56 CS210-Summer 2005, Lecture 18 64 78 + 3 Randomized Algorithms A randomized algorithm performs coin tosses (i.e., uses random bits) to control its execution It contains statements of the type b random() if b = 0 do A … else { b = 1} do B … We analyze the expected running time of a randomized algorithm under the following assumptions Its running time depends on the outcomes of the coin tosses 6/30/2016 the coins are unbiased, and the coin tosses are independent The worst-case running time of a randomized algorithm is often large but has very low probability (e.g., it occurs when all the coin tosses give “heads”) We use a randomized algorithm to insert items into a skip list CS210-Summer 2005, Lecture 18 4 Insertion To insert an entry (x, o) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails, and we denote with i the number of times the coin came up heads If i h, we add to the skip list new lists Sh+1, … , Si +1, each containing only the two special keys We search for x in the skip list and find the positions p0, p1 , …, pi of the items with largest key less than x in each list S0, S1, … , Si For j 0, …, i, we insert item (x, o) into list Sj after position pj Example: insert key 15, with i = 2 p2 S2 - p1 S1 - S0 - 23 p0 10 6/30/2016 23 36 S3 - + + S2 - 15 + S1 - 15 23 + S0 - 15 23 10 CS210-Summer 2005, Lecture 18 + + 36 + 5 Deletion To remove an entry with key x from a skip list, we proceed as follows: We search for x in the skip list and find the positions p0, p1 , …, pi of the items with key x, where position pj is in list Sj We remove positions p0, p1 , …, pi from the lists S0, S1, … , Si We remove all but one list containing only the two special keys Example: remove key 34 + S3 - p2 S2 - 34 p1 S1 - S0 - 23 34 p0 12 6/30/2016 23 34 45 + S2 - + S1 - + S0 - CS210-Summer 2005, Lecture 18 + + 23 12 23 45 + 6 Implementation We can implement a skip list with quad-nodes A quad-node stores: entry link to link to link to link to the the the the node node node node prev next below above quad-node x Also, we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them 6/30/2016 CS210-Summer 2005, Lecture 18 7 Summary A skip list is a data structure for dictionaries that uses a randomized insertion algorithm In a skip list with n entries The expected space used is O(n) The expected search, insertion and deletion time is O(log n) 6/30/2016 Using a more complex probabilistic analysis, one can show that these performance bounds also hold with high probability Skip lists are fast and simple to implement in practice CS210-Summer 2005, Lecture 18 8 Binary Search Trees A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w) An inorder traversal of a binary search trees visits the keys in increasing order 6 2 1 9 4 8 External nodes do not store items 6/30/2016 CS210-Summer 2005, Lecture 18 9 Search Algorithm TreeSearch(k) To search for a key k, TreeNode v = root; we trace a downward path starting at the root while v.isInternal() v.key != k The next node visited if k < v.key depends on the v = v.left outcome of the else comparison of k with v = v.right the key of the current return v node If we reach a leaf, the 6 key is not found and we < return null 2 Example: find(4): > Call TreeSearch(4,root) 6/30/2016 1 4 = CS210-Summer 2005, Lecture 18 9 8 10 Insertion To perform operation insert(k, o), we search for key k (using TreeSearch) Assume k is not already in the tree, and let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert 5 6/30/2016 6 < 2 9 > 1 4 8 > w 6 2 1 9 4 CS210-Summer 2005, Lecture 18 8 w 5 11 Deletion To perform operation remove(k), we search for key k Assume key k is in the tree, and let let v be the node storing k If node v has one child w, we remove v and w from the tree with operation removeExternal(w), which removes w and its parent Example: remove 4 6/30/2016 6 < 2 9 > 4 v 1 8 w 5 6 2 1 9 5 CS210-Summer 2005, Lecture 18 8 12 Deletion (cont.) We consider the case where the key k to be removed is stored at a node v whose children are both internal we find the internal node w that follows v in an inorder traversal we copy key(w) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeExternal(z) 1 v 3 2 w z 5 9 6 1 v 5 2 Example: remove 3 6/30/2016 8 8 6 CS210-Summer 2005, Lecture 18 9 13 Performance Consider a dictionary with n items implemented by means of a binary search tree of height h the space used is O(n) methods find, insert and remove take O(h) time The height h is O(n) in the worst case and O(log n) in the best case 6/30/2016 CS210-Summer 2005, Lecture 18 14