Java Programming Implement a minimum (min) heap of integers, and write a driver
ID: 3930986 • Letter: J
Question
Java Programming
Implement a minimum (min) heap of integers, and write a driver to demonstrate its functionality. Min heaps work the same way as maximum (max) heaps except the a parent is always smaller than its children.
The class your min heap should have the following methods
insert: Add a new element to the heap, and bubble up as needed.
delete: Remove the first element from the heap and then reheapify.
heapSort: Make a copy of the heap, then remove and print each element from the clone. Make sure this doesn’t modify the current heap.
print: Prints the elements in the heap in breadth order.
Example Print Out:
Int Min Heap Tester
Populating Heap with values
21
37
49
11
23
1
13
16
33
17
Printing Heap
1
16
11
21
17
49
13
37
33
23
Testing heap sort
1 11 13 16 17 21 23 33 37 49
Removing an element from the heap
The element removed is 1 and the heap now is:
11
16
13
21
17
49
23
37
33
*Include a screenshot of code output*
Explanation / Answer
Binary Heap has to be complete binary tree at all levels except the last level. This is called shape property All nodes are either greater than equal to (Max-Heap) or less than equal to (Min-Heap) to each of its child nodes. This is called heap property
Heap Majorly has 3 operations..insert operation , delete operation , extract operation...
}
Testing heap sort
1 11 13 16 17 21 23 33 37 49
public class minHeap { public int size; public int [] mH; public int position; public minHeap(int size){ this.size=size; mH = new int [size+1]; position = 0; } public void createHeap(int [] arrA){ if(arrA.length>0){ for(int i=0;i<arrA.length;i++){ insert(arrA[i]); } public int extractMin(){ int min = mH[1]; mH[1]=mH[position-1]; mH[position-1]=0; position--; sinkDown(1); return min;}
Testing heap sort
1 11 13 16 17 21 23 33 37 49
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.