You are going to build in Java a Binary Heap ADT (BHEAP). You are also going to use your BHEAP to implement and exercise a priority queue.
Instead of a single integer as heap element (as we have done in class) your heap will allow manipulation of more information. We represent this as a integer (for priority queue position) and a string (associated information). As elements move around in the heap, keep the associated information associated. You design how to do this.
Example: [4:goheels] / \ / \ / \ [9:virginia] [12:wake] / \ / / \ / / \ / [17:duke] [11:state] [23:pitt]
Your main method will provide 2 modes of operation: interactive, and command line. Details of each below.
Interface: The BHEAP will provide this collection of operations:
insert in: the integer priority and associated string information return: void, boolean, whatever works for you delMin in: nothing return: void, boolean, whatever works for you getMin in: nothing return: both the integer priority and the associated string size in: nothing return: integer 0 or greater build in: array of elements that need to be in the heap return: void, or boolean, or whatever (assume for a build that the bheap will start empty)
In addition, your program at the driver level will need to do these:
print in: nothing side effect: prints, not really a part of the ADT behavior it might be in the BHEAP ADT (thats ok), or you might wish to return the heap data with some getStuff internal op and print at the driver level sort in: nothing side effect: does like print, but it generated the elements in sorted order by calling getMIn and delMin as many times as there are elements in the heap new in: nothing fill in: how many randomly generated items to insert, generate them, call insert for each build in: collect information from user and construct array(s), then call heap build and pass array information
Your main class/method will provide two modes of operation:
How will we know which mode to operate in?
-- If your program is executed with command line args, then operate in mode 2.
-- It is is run with no command line args, then operate interactively in mode 1.
This will be like the BST assignment (parts 1,2). You will present the user with a menu of single character operations to select and apply each operation to your BHEAP as directed. Mode 1 will help you debug your code and get the BHEAP working correctly.
Print a single line showing the user the single letter commands, and wait for keyboard input:
command (q,n,i,d,g,s,b,f,p,o)?Apply the selected operation, produce any output needed, and loop back for another command. Here "q" is quit, "n" is make a new empty BHEAP, "i" is insert, "d" is delete minimum, "g" is get minimum, "s" is for size, "b" is for build, "f" is fill, "p" is print the heap, and "o" is order (sorted) print.
Make the output look like this:
3:miller, 7:smith, 9:wilson, 21:adams, 13:davis, 34:carson, 17:jonesThis is simply the elements in the heap in array subscript order, from 1 (don't print the 0 element).
3:miller, 7:smith, 9:wilson, 13:davis, 17:jones, 21:adams, 34:carsonThis is like print but the elements are not in heap/array order, they are in sorted order.
3:miller
7 elements
Ignore the string for ordering. In the test data we use, the integer priorities will be unique.
For this implementation, handle duplications this way. If you get an element inserted that has the same priority as one already there (same integer) just insert it as the algorithm allows.
Consider this question: lets think of two elements being added, both with priority 3 (let's say). Let's say element E is added first (with integer priority 3) and then later element E' is added (also with priority 3). Also assume we are just construcing the heap in this interval.. no deletes are being done. If we then do a sort/print (that is, begin removing all the elements in priority order with repeated delMin calls) will E thcome out first and E' second, guaranteed? In other words, does our heap algorithm maintain insertion order among elements at the same priority level?
Like with the BST code, the fill command will help debug the code without having to type a lot of input. For fill, ask the user how many items to generate. Then generate an integer 0 or greater at random, and also generate a random string (like we did for BST). This will give you two random pieces of data to store in the heap together as one priority queue element. Do the insert command on the each pair as you generate them. Produce no printed output.
In interactive mode, let's do the build command this way. Go into a loop where you ask the user to give an integer then a string. Store the user input into array(s). Stop when the user gives a "-1" for integer. Then do the build operation with the array(s) created.
The intent is to "sort" by repeatdly doing (getMin,delMin) pairs of operations. This is necessarily destructive to the heap, and when this operation is complete the heap will be empty. This is ok. Implementing the "o" operation this way is fine, and we will live with it leaving up with an empty heap.
If you wish to rebuild the heap so the we see the sorted order but the heap remains when done, that is ok too. One this you can do it create a second heap, and as you do getMin you do an insert in the new heap. You grow the new heap as you diminish the one you are sort/printing. When done, set the new heap to being the "real" heap that the driver is manipulating. Another thing you could do is save the elements as they come out of the heap (in array(s)) and then do a build using the array(s) when the sort/print is finished.
Mode 2 will read data from the command line -- meaning from the (String[ ] args) parameter to "public static void main". It will not be interactive and will only perform some of the BHEAP operations.
The input will consist of pairs of values, alternating integer and string. Example:
17 jones 21 adams 3 miller 7 smith 13 davis 34 carson 9 wilson
Do the following things in the main class:
3:miller, 7:smith, 9:wilson, 21:adams, 13:davis, 34:carson, 17:jones
3:miller, 7:smith, 9:wilson, 21:adams, 13:davis, 34:carson, 17:jones
3:miller, 7:smith, 9:wilson, 13:davis, 17:jones, 21:adams, 34:carson
Hand in your code as per the instructions under
Assignment 5 "Binary Heap" in the WorkBase.
Submit a single text file with the entire Java source...
main class, any supporting classes, as well as your BHEAP class(es).
We should be able to cut and paste this single Java source
into a Java IDE and have it compile, execute, and
produce your output for grading.
Please check this. Copy your submission once submitted and
paste it back into your Java IDE and see if it compiles as a single
file. Many of yours are not, due to small things... like
multiple includes in wrong places, or "public" being before
"class" for the classes after the main in the one file.
They are also not compiling due to "package" designations
that may not work properly in the environment we use.