CSIS 10B Assignment 13 Heaps and Priority Queues Due: next Wed In class Demos (0 points) We develop a simple (non-generic) StrHeap class: 1) basic mapping of tree heirarchy into a vector 2) size, empty and clear methods 3) adding to a heap – percolateUp Assignment (10 points) Do either Basic or Advanced Basic -- do all these exercises in lab13basic folder inside mod13 download 1) Implement the following algorithm in the StrHeap class, which performs the percolateDown operation on a heap, where data = the structure (vector or array) containing the elements in the heap, r = the index of a "root" (which could be THE root or any subtree root in the heap), and c= the index of a child (either left or right). Note that this is a general algorithm. Use the appropriate access methods for the vector and the appropriate comparison method for String. public void percolateDown(int r) pre: r = a valid index in heap, the root index of a subtree post: move the value at r into its proper min heap position within a path descending to a leaf node. While r < size do following Set c = left(r) // start by examining the left child If c < size if right(r) < size and data[c+1] < data[c] // if the right child is smaller Increment c by 1 // we'll compare right instead of left if data[c] < data[r] // the child is smaller than its parent i. Swap data[r] and data[c] // exchange the parent with its child ii. set r = c // get ready to repeat this operation else // items are in order so done break; else // we are at the end of heap so done break; End while If you follow the steps carefully, this operation will be O(logN). When swapping make sure you do not use the add method since that will give O(N log N) behavior due to the shifting of data needed. Note: after this method works, change it from PUBLIC to PRIVATE—it is only a helper. 2) Code and test the above algorithm. Then, write the remove method for our heap: public String remove() get the first item (index 0) of the heap and store in variable minVal remove the last item in the heap and store in variable lastVal if there are more than 1 items remaining in the heap, set the root of the heap to lastVal start percolateDown at index 0 ( the root location) finally, return the minVal item If you follow the steps carefully, this algorithm will be O(logN) as well. 3) Having completed the remove operation, we can now use our StrHeap to sort a vector of String. Simply make a loop that adds each item in the vector to the heap. Then, make another loop that retrieves all of the items from the heap (in increasing order) and places them in a new sorted vector. (There are more efficient ways to implement heap sort on an array or Vector "in place" but this illustrates the main idea and is still O(N log N), which is generally the best one can do in sorting.) 4) Complete the project shell given in OsSim.java using the following instructions: Note1: you will need to define a compareTo method for the Task class that compares priorities of two tasks. Note2: since javas PriorityQueue uses a min-heap, a task with priority 0 will be the “highest” priority. 5. Add your answers to the following problems to the bottom of your HeapDemoApp file: Advanced For a more open-ended priority queue simulation you may do Lab 13.7 on p242 of the text.
© Copyright 2026 Paperzz