320 likes | 546 Views
CSE 326: Data Structures Lecture #18 Fistful of Data Structures. Steve Wolfman Winter Quarter 2000. Today’s Outline. What Steve Didn’t Get To On Monday Warm-up: augmenting leftist heaps Binomial Queues Treaps Randomized Skip Lists What Steve Won’t Get To (Ever?).
E N D
CSE 326: Data StructuresLecture #18Fistful of Data Structures Steve Wolfman Winter Quarter 2000
Today’s Outline • What Steve Didn’t Get To On Monday • Warm-up: augmenting leftist heaps • Binomial Queues • Treaps • Randomized Skip Lists • What Steve Won’t Get To (Ever?)
Thinking about DecreaseKey in Leftist Heaps Why not just percolate up? decreaseKey( , 3) node 7 3 12 8 7 8 3 15 12 17 9 30 17 9 30 20 22 18 20 22 18
DecreaseKey in Leftist Heaps decreaseKey(15, 3) 7 7 12 8 12 8 3 15 3 17 9 30 17 9 30 20 22 18 20 22 18 Now just merge the two?
Fixing DecreaseKey in Leftist Heaps decreaseKey(15, 3) This may not be leftist 7 7 So, fix it! 12 8 8 12 3 17 9 30 9 30 17 20 22 18 18 This is still leftist Now, merge!
DecreaseKey runtime How many nodes could possibly have the wrong Null Path Length up the line? runtime:
Delete in Leftist Heaps decreaseKey(15, -) deleteMin() runtime:
Binomial Trees A binomial tree of rank 0 is a one node tree. A binomial tree of rank k is a binomial tree of rank k-1 with another binomial tree of rank k-1 hanging from its root. rank 0 rank 1
First Five Binomial Trees rank 0 rank 1 rank 2 rank 3 rank 4 How many nodes does a binomial tree of rank k have?
Binomial Queue Heap Data Structure rank 3 rank 2 rank 1 rank 0 • Composed of a forest of binomial trees, no two of the same rank • Heap-order enforced within each tree • Ranks present can be computed using the binary representation of the tree’s size size = 10 = 10102 rank 1 rank 3 5 3 4 9 7 13 6 10 15 21
Insertion in Binomial Queues insert(10) rank 1 rank 2 rank 0 rank 1 rank 2 10 10 5 3 5 3 9 7 13 9 7 13 15 15 If there’s no rank 0 tree, just put the new node in as a rank 0 tree.
Insertion in Binomial Queues insert(10) rank 0 rank 1 rank 0 rank 1 rank 2 10 5 3 5 3 3 7 10 7 7 5 10 It’s like addition of binary numbers! 1+1 = 0 plus a carry tree 1+1+1 = 1 plus a carry tree runtime:
Merge in Binomial Queues rank 1 rank 2 rank 0 rank 1 5 3 11 4 9 7 13 16 15 rank 0 rank 3 11 3 0110 + 0011 = 1001 4 7 13 15 16 5 runtime: 9
DeleteMin in Binomial Queues These are one Binomial Queue 11 10 1 3 4 14 8 25 7 13 27 15 16 5 9 These are another 8 10 3 Just merge the two: 4 11 14 25 7 13 27 15 16 5 runtime: 9
Binomial Queue Summary • Implements priority queue ADT • Insert in amortized O(1) time • FindMin (with some tricks) in O(1) time • DeleteMin in O(log n) time • Merge in O(log n) time • Memory use • O(1) per node • about the cost of skew heaps • Complexity?
Treap Dictionary Data Structure heap in yellow; search tree in blue • Treaps have the binary search tree • binary tree property • search tree property • Treaps also have the heap-order property! • randomly assigned priorities 2 9 6 7 4 18 7 8 9 15 10 30 Legend: priority key 15 12
insert(7) insert(8) insert(9) insert(12) 6 7 6 7 2 9 2 9 7 8 6 7 6 7 15 12 7 8 7 8 Tree + Heap… Why Bother? Insert data in sorted order into a treap; what shape tree comes out? Legend: priority key
Treap Insert • Choose a random priority • Insert as in normal BST • Rotate up until heap order is restored insert(15) 2 9 2 9 2 9 6 7 15 12 6 7 15 12 6 7 9 15 7 8 7 8 9 15 7 8 15 12
Treap Delete delete(9) 2 9 6 7 • Find the key • Increase its value to • Rotate it to the fringe • Snip it off 7 8 9 6 7 9 15 9 15 7 8 15 12 6 7 6 7 15 12 6 7 7 8 7 8 9 15 9 15 7 8 9 15 9 15 12 15 12 15 12 9
Treap Summary • Implements Dictionary ADT • insert in expected O(log n) time • delete in expected O(log n) time • find in expected O(log n) time • Memory use • O(1) per node • about the cost of AVL trees • Complexity?
Perfect Binary Skip List • Sorted linked list • # of links of a node is its height • The height i link of each node (that has one) links to the next node of height i or greater 22 11 8 19 29 2 10 13 20 23
Find in a Perfect Binary Skip List • Start i at the maximum height • Until the node is found or i is one and the next node is too large: • If the next node along the i link is less than the target, traverse to the next node • Otherwise, decrease i by one runtime:
Randomized Skip List Intuition It’s far too hard to insert into a perfect skip list, but is perfection necessary? What matters in a skip list?
13 8 22 10 20 29 2 11 19 23 Randomized Skip List • Sorted linked list • # of links of a node is its height • The height i link of each node (that has one) links to the next node of height i or greater • There should be about 1/2 as many nodes of height i+1 as there are of height i
Find in a RSL • Start i at the maximum height • Until the node is found or i is one and the next node is too large: • If the next node along the i link is less than the target, traverse to the next node • Otherwise, decrease i by one Same as for a perfect skip list! runtime:
Insertion in a RSL • Flip a coin until it comes up heads; that takes i flips. Make the new node’s height i. • Do a find, remembering nodes where we go down • Put the node at the spot where the find ends • Point all the nodes where we went down (up to the new node’s height) at the new node • Point the new node’s links where those redirected pointers were pointing
insert(22) with 3 flips 13 20 23 13 8 8 10 10 20 29 29 2 2 11 11 19 19 23 Insertion Example in RSL 22 runtime:
Range Queries and Iteration • Range query: search for everything that falls between two values • Iteration: successively return (in order) each element in the structure How do we do them? How fast are they?
Randomized Skip List Summary • Implements Dictionary ADT • insert in expected O(log n) • find in expected O(log n) • delete? • Memory use • expected constant memory per node • about double a linked list • Complexity?
What We Won’t Discuss Pairing heaps - practically, the fastest and best implementation of heaps for decreaseKey and merge; they use the leftist cut and merge technique Red-Black Trees - a balanced tree that uses just a one-bit color flag and some invariants to maintain balance: see www/homes/sds/rb.html AA-Trees - a cross between Red-Black trees and B-Trees that is relatively simple to code and gives worst case O(log n) running time Deterministic skip lists - a version of skip lists that gives worst case O(log n) running time
To Do • Finish Project III • Browse chapters 10 & 12 in the book • Form Project IV teams! • groups of 4-6 • 2 1/2 week project • demos at the end
Coming Up • Quad Trees • k-D Trees • Quiz (February 17th) • Project III due (February 17th by 5PM!) • Project IV distributed (February 18th)