Download presentation
Presentation is loading. Please wait.
Published byKristina Waters Modified over 9 years ago
1
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue
2
Announcements I uploaded wrong jar for PA #2; its now fixed My only excuse is that I am an absolute moron Portfolio grade is largely about process Document changes from your UML file Run and record results from JUnit tests Keep copies of changes resulting from tests Please consider developing outlines Easily changed into good documentation Shows preconditions, postconditions, & linkages Debugging outline easier than fixing code
3
Today’s Goal Discuss Heap-based implementation What a heap is & properties of a heap How a heap works Effects on big-Oh notation Discuss new method of sorting data Still uses PriorityQueue, but is now Heap- based Our first good sorting algorithm
4
Priority Queue ADT Orders Entrys by their keys For equal priorities, order is not required Access only item with lowest priority Returns Entry using min() or removeMin() Inserts element based upon priority Do not need to maintain order within a priority Positions are imagined – only smallest is important
5
Heap Binary tree storing an Entry in each Node But remember Node still implements only Position Binary tree satisfies 2 additional properties: For each node, v, but the root: v.getKey() ≥ v.getParent().getKey() Binary Tree must be complete 2 65 79
6
Heap Binary tree storing an Entry in each Node But remember Node still implements only Position Binary tree satisfies 2 additional properties: For each node, v, but the root: v.getKey() ≥ v.getParent().getKey() Binary Tree must be complete 2 65 79 Always add to lowest level and add from left to right
7
Why the Added Complexity? Minimum priority Entry stored at tree’s root min() implementation easy & takes O(1) time True for any implementation of binary tree Must insert new elements at: Left-most empty Node on bottom level –or– Left-most Node on new lowest level Easy with array-based tree implementation Node inserted at first empty location in array Check book for proof of this useful property
8
1 1 6 6 2 2 6 1 Upheap Insertion may violate heap-order property Upheap starts after Node is inserted Travels up tree restoring heap-order property Swaps smaller child Node’s Entry with parent’s Restarts upheap-ing with parent’s Node Stop upheap-ing at root or legal Node 2 5 79 2 1 1 3 3
9
Removing Smallest Node Finds and removes root Node Complete tree needs last added Node gone In linked-tree, stored this Node in a field So must find the previous last added Node Starting at the last Node in tree Loop with parent until Node is right child or root If right child is reached, go to the left child Keep following right children until hitting a leaf
10
Removing an Entry Move last added Node’s Entry to root Downheap starts after Node is inserted Go down tree restoring heap-order property Swaps Node’s Entry with smallest child’s Entry Restarts downheap-ing with Node used in swap Stop downheap-ing at leaf or legal Node 2 5 79 6 1 3 32 33 2
11
Downheap private void downHeap(Node node) { if (isInternal(node)) { Node lesser = null; Node left = getLeft(node); Node right = getRight(node); if ( (right == null) || (c.compare(left.element(),right.element())<0 ) { lesser = left; } else { lesser = right; } if (c.compare(lesser.element(),node.element())< 0)){ // Swap elements in left & node downHeap(lesser); } }
12
Implementation Excuses upheap & downheap travel height of tree Running time of each of these is O(log n ) Therefore insertion and removal is O(log n ) Where would this be used? Both Sequence-based priority queue can be faster Assumes will only be inserting & removing If performing both insertion & removal, however heap-based priority queue is fastest! What does this mean for sorting?
13
Heap-Sort Uses heap-based priority queue Otherwise like other priority queue-based sorts Insert values into heap, then remove them Running time of Heap-sort: n insert calls * O(log n ) per call = O( n log n ) time n removeMin calls * O(log n ) each = O( n log n ) time Total time for this sort is in O( n log n ) Our first faster sort!
14
In the Next Lecture Have fun playing with priority queues Try writing out ideas in code See how well ideas actually understood Last in-class opportunity to ask me questions Determine which student is Head of the Class
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.