Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000."— Presentation transcript:

1 CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000

2 Today’s Outline Turn in Homework #2 Things Steve Didn’t Finish on Monday (d-Heaps) Leftist Heaps Skew Heaps Comparing Heaps Hand Out Project #2

3 New Operation: Merge Given two heaps, merge them into one heap –first attempt: insert each element of the smaller heap into the larger. runtime: –second attempt: concatenate heaps’ arrays and run buildHeap. runtime: How about O(log n) time?

4 Idea: Hang a New Tree 1213106 115 2 + 1014 49 1 = 1213106 49115 12 ? 10 Now, just percolate down!

5 Idea: Hang a New Tree 1213106 115 2 + = 1213106 1213106 115 2 1213106 Problem?

6 Leftist Heaps Idea: make it so that all the work you have to do in maintaining a heap is in one small part Leftist heap: –almost all nodes are on the left –all the merging work is on the right

7 the null path length (npl) of a node is the number of nodes between it and a null in the tree Random Definition: Null Path Length npl(null) = -1 npl(leaf) = 0 npl(single-child node) = 0 000 001 11 2 another way of looking at it: npl is the height of complete subtree rooted at this node 0

8 Leftist Heap Properties Heap-order property –parent’s priority value is  to childrens’ priority values –result: minimum element is at the root Leftist property –null path length of left subtree is  npl of right subtree –result: tree is at least as “heavy” on the left as the right Are leftist trees complete? Balanced?

9 Leftist tree examples NOT leftistleftist 00 001 11 2 0 0 000 11 2 1 000 0 0 0 0 0 1 0 0 every subtree of a leftist tree is leftist, comrade!

10 Right Path in a Leftist Tree is Short If the right path has length at least r, the tree has at least 2 r - 1 nodes Proof by induction Basis: r = 1. Tree has at least one node: 2 1 - 1 = 1 Inductive step: assume true for r’ < r. The right subtree has a right path of at least r - 1 nodes, so it has at least 2 r - 1 - 1 nodes. The left subtree must also have a right path of at least r - 1 (otherwise, there is a null path of r - 3, less than the right subtree). Again, the left has 2 r - 1 - 1 nodes. All told then, there are at least: 2 r - 1 - 1 + 2 r - 1 - 1 + 1 = 2 r - 1 So, a leftist tree with at least n nodes has a right path of at most log n nodes 0 000 11 2 1 00

11 Whew!

12 Merging Two Leftist Heaps merge(T 1,T 2 ) returns one leftist heap containing all elements of the two (distinct) leftist heaps T 1 and T 2 a L1L1 R1R1 b L2L2 R2R2 merge T1T1 T2T2 a < b a L1L1 merge b L2L2 R2R2 R1R1

13 Merge Continued a L1L1 R’R’ R ’ = Merge(R 1, T 2 ) a R’R’ L1L1 npl(R ’ ) > npl(L 1 ) runtime:

14 Operations on Leftist Heaps merge with two trees of total size n: O(log n) insert with heap size n: O(log n) –pretend node is a size 1 leftist heap –insert by merging original heap with one node heap deleteMin with heap size n: O(log n) –remove and return root –merge left and right subtrees merge

15 Example 1210 5 87 3 14 1 00 1 00 0 merge 7 3 14 ? 0 0 1210 5 8 1 00 0 merge 10 5 ? 0 merge 12 8 0 0 8 0 0

16 Sewing Up the Example 8 12 0 0 10 5 ? 0 7 3 14 ? 0 0 8 12 0 0 10 5 1 0 7 3 14 ? 0 0 8 12 0 0 10 5 1 0 7 3 14 2 0 0 Done?

17 Finally… 8 12 0 0 10 5 1 0 7 3 14 2 0 0 7 3 2 0 0 8 12 0 0 10 5 1 0

18 Iterative Leftist Merging 1210 5 87 3 14 1 00 1 00 0 merge downward pass: merge right paths 8 12 0 0 10 5 1 0 7 3 14 2 0 0

19 Iterative Leftist Merging upward pass: fix right path 8 12 0 0 10 5 1 0 7 3 14 2 0 0 8 12 0 0 10 5 1 0 7 3 14 2 0 0 8 12 0 0 10 5 1 0 7 3 14 2 0 0 7 3 2 0 0 8 12 0 0 10 5 1 0 What do we need to do this iteratively?

20 Random Definition: Amortized Time am·or·tize To write off an expenditure for (office equipment, for example) by prorating over a certain period. time A nonspatial continuum in which events occur in apparently irreversible succession from the past through the present to the future. am·or·tized time Running time limit resulting from writing off expensive runs of an algorithm over multiple cheap runs of the algorithm, usually resulting in a lower overall running time than indicated by the worst possible case. If M operations take total O(M log N) time, amortized time per operation is O(log N)

21 Skew Heaps Problems with leftist heaps –extra storage for npl –two pass merge (with stack!) –extra complexity/logic to maintain and check npl Solution: skew heaps –blind adjusting version of leftist heaps –amortized time for merge, insert, and deleteMin is O(log n) –worst case time for all three is O(n) –merge always switches children when fixing right path –iterative method has only one pass

22 Merging Two Skew Heaps a L1L1 R1R1 b L2L2 R2R2 merge T1T1 T2T2 a < b a L1L1 merge b L2L2 R2R2 R1R1

23 Example 1210 5 87 3 14 merge 7 3 14 1210 5 8 merge 7 3 14 10 5 8 merge 12 7 3 14 10 8 5 12

24 Skew Heap Code void merge(heap1, heap2) { case { heap1 == NULL: return heap2; heap2 == NULL: return heap1; heap1.findMin() < heap2.findMin(): temp = heap1.right; heap1.right = heap1.left; heap1.left = merge(heap2, temp); return heap1; otherwise: return merge(heap2, heap1); }

25 Comparing Heaps Binary Heaps d-Heaps Binomial Queues Leftist Heaps Skew Heaps

26 To Do Start looking at Project II Find a group for Project II Read chapter 4 in the book

27 Coming Up Dictionary ADT Self-Balancing Trees Quiz #3 (January 20 th )


Download ppt "CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000."

Similar presentations


Ads by Google