Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Lecture #5 Political Heaps

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #5 Political Heaps"— Presentation transcript:

1 CSE 326: Data Structures Lecture #5 Political Heaps
Bart Niswonger Summer Quarter 2001

2 Today’s Outline Project comments & questions
Things Bart Didn’t Finish on Monday (Leftist Heaps) Skew Heaps Comparing Heaps Today we’ll cover everything I missed on Monday Recall the problem at hand – merging heaps Today we’ll cover leftist heaps and skew heaps today; those are two kinds of mergeable heaps. Then, if we have time, we’ll compare all those heap implementations.

3 Merging Heaps How can we make it fast?
Array-based implementation: Pointer-based implementation: To make it fast we need to identify the bottleneck…. Seems like you have to copy which would be theta(n) for equal sized heaps, just to copy the array What if rather than using an array, we use a linked structure (linked list/tree)? We will see we can make it faster, but what about the other operations?

4 Leftist Heaps Idea: Leftist heap: almost all nodes are on the left all the merging work is on the right make it so that all the work you have to do in maintaining a heap is in one small part Leftist heaps are a solution to that. They put all the work on the right side of the tree and most of the nodes on the left (or at least not on the right)

5 Not-so Random Definition: Null Path Length
the null path length (npl) of a node is the number of nodes between it and a null in the tree npl(null) = -1 npl(leaf) = 0 npl(single-child node) = 0 2 1 1 Before I make this more concrete, let’s get a totally unrelated random definition that we probably will never use. Note that I’m assuming a binary tree here. Null path length is the number of nodes between a given node and a null. Npl of null is -1 npl of a leaf or a node with just one child is 0 Notice that I’m putting the null path length, NOT the values in the nodes. 1 another way of looking at it: npl is the height of complete subtree rooted at this node

6 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 So, here are the formal properties of a leftist heap. Heap-order, of course. And, the leftist property; this means that the npl of the right child is never greater than that of the left child. Are leftist trees necessarily complete or balanced? NO. In fact, a _great_ leftist tree is a string of nodes all on the left! Are leftist trees complete? Balanced?

7 Leftist tree examples NOT leftist leftist leftist 2 2 1 1 1 1 1 1 1
1 1 1 1 1 1 1 Here are some training examples to help you learn. The green nodes break the leftist property for the first tree. The other two trees are leftist. every subtree of a leftist tree is leftist, comrade!

8 Right Path in a Leftist Tree is Short
If the right path has length at least r, the tree has at least 2r-1 nodes Proof by induction Basis: r = 1. Tree has at least one node: = 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 2r 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 2r nodes. All told then, there are at least: 2r r = 2r - 1 So, a leftist tree with at least n nodes has a right path of at most log n nodes 2 1 1 1 Why do we have this leftist property? Because it guarantees that the right path is really short compared to the number of nodes in the tree without being as stodgy as the heap structure property. Here’s a proof which boils down to the fact that if the left path is r long, the null path length must be at least r (otherwise somewhere along there, there’s a node with a larger right npl than left). Since the tree is complete at least to depth r, there are at least 2r - 1 nodes in the tree.

9 Merging Two Leftist Heaps
merge(T1,T2) returns one leftist heap containing all elements of the two (distinct) leftist heaps T1 and T2 merge T1 a a merge L1 R1 L1 R1 How do we merge two leftist heaps? We put the smaller root as the new root, hang its left subtree on the left. Recursively merge its right subtree and the other tree. Are we done? No. Because we haven’t ensured that the leftist property is intact. a < b T2 b b L2 R2 L2 R2

10 Merge Continued a a npl(R’) > npl(L1) L1 R’ R’ L1 runtime:
So, we check if the leftist property is intact. If it isn’t we switch the right and left children. How long does this take? How much work do we do at each step? CONSTANT And, we recursively traverse the whole right path of both trees. How long are those paths? At most log n. So how much work? O(log n) Pretty cool, huh? R’ = Merge(R1, T2) runtime:

11 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 Now that we have that wicked merge op. Let’s just use it to implement all the other ops. Is one node a size one leftist heap? merge

12 Example merge 3 5 merge 7 10 12 5 5 merge 14 3 10 10 12 12 7 8 8 8 14
? 1 3 5 merge ? 7 10 12 1 5 5 merge 1 14 3 10 10 12 12 Alright, let’s try an example. I’m unwinding the recursion here. I pulled a fast one merging the last two nodes. I could have reduced that to merging a node with null (which is clearly the original node) but I didn’t for convenience. Don’t _you_ do that on tests! 7 8 8 8 14 8 12

13 Sewing Up the Example ? ? 2 3 3 3 7 ? 7 1 7 5 1 5 5 14 14 14 10 8 10 10 8 8 No, we need to put the example back together, fixing the leftist property as we go. Are we done here? NO! The root’s right child has a higher npl than its left child! 12 12 12 Done?

14 Finally… 2 2 3 3 7 5 1 5 1 7 14 10 8 10 8 14 So, we swap the root’s children. Is the final tree a leftist heap? 12 12

15 Iterative Leftist Merging
downward pass: merge right paths merge 1 5 2 3 10 12 7 5 1 It turns out that we can do an iterative version of this. First, we traverse down the right side, picking up the lesser node of each tree as we go. 1 3 14 10 8 7 8 12 14

16 Iterative Leftist Merging
upward pass: fix right path 2 2 2 2 3 3 3 3 1 1 1 1 7 5 7 5 7 5 5 7 14 10 8 14 10 8 10 8 14 14 10 8 Then, we go back up and fix the leftist property from bottom to top. What do we need to do this iteratively? A STACK of the nodes on the right path! 12 12 12 12 What do we need to do this iteratively?

17 Random Definition: Amortized Time
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. Now, we’re done with leftist heaps for the moment, and we’re going to move on to another type of heap. But, before we do, here’s a totally random definition. If M operations take total O(M log N) time, amortized time per operation is O(log N)

18 Skew Heaps Problems with leftist heaps Solution: skew 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 There are some problems with leftist heaps. We’ll use skew heaps to fix those problems. Motivation? Remember that we “often” get heavy right sides when we merge. So, why not just assume the right side is heavy and move it over automatically?

19 Merging Two Skew Heaps merge T1 a a merge L1 R1 L1 R1 a < b T2 b b
This merge should look very similar but simpler than the previous algorithm. L2 R2 L2 R2

20 Example merge 3 3 5 merge 5 7 7 10 12 5 merge 10 14 3 14 12 10 12 8 3
This happens to end up with a leftist tree and start out with leftist trees. Is that guaranteed? No – an example? 8 8 5 7 14 8 10 14 12

21 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); } Here’s the code. I won’t go into it except to say that it is _easy_. The iterative version is a touch more intricate but also fairly easy and requires NO STACK!

22 Comparing Heaps Leftist Heaps Skew Heaps Binary Heaps d-Heaps
Binomial Queues Leftist Heaps Skew Heaps OK, I said you were being cheated unless you got a comparison of data structures. Who can tell me some good things about each of these? Binary: simple, memory efficient, fast, can run buildHeap. cannot merge quickly. d: Faster than binary heaps. Possibly much faster in some applications. Slightly more complicated. Otherwise, all binary advantages. Leftist: supports merge quickly. Supports all other operations asymptotically quickly. Practically, extra storage, link traversals and recursion make it bigger and slower. Skew: slightly less storage, somewhat faster and simpler than leftist. NOT guaranteed fast on EVERY operation (but guaranteed amortized speed. Binomial queues? Never heard of ‘em. Read up on them in your book!

23 To Do Project I due tonight @ 10pm
Please me if you are going to be late Groups were due Monday Homework due START of section

24 Coming Up Section Project II Asymptotic Complexity!
tomorrow: The Ashish Experience Project II Asymptotic Complexity! Ashish is back from an intensive Karate training – maybe he’ll show you some moves, esp if your homework is late


Download ppt "CSE 326: Data Structures Lecture #5 Political Heaps"

Similar presentations


Ads by Google