This is for my object class in Java If anyone can help I wou
This is for my object class in Java, If anyone can help I would be grateful, the problem is from the textbook, and this is all the requirments and the entire problem as it is stated in the textbook. Anyone that can help me, please do, I will be greateful. Thank you in advanced for any help!
Problem 7 of Chapter 10.
The heap in this chapter is referred to as a maxheap because the highest priority value is also the maximum value. Implement the heap as a minheap, in which the entry of the node with the highest priority has the minimum value in the heap, and an entry of a node is never more than the entries of the node\'s children.
Deliverbale
1) Java code of the minheap object -- you must use ARRAY as the supporting datas structure
2) Your tester code
3) Output showing that your minheap is operational
4) Output showing a preorder print of your minheap.
Solution
MinimumHeap.java
-----------------------------
public class MinimumHeap{
   
    //Instance Variables
 private int[] heap;
 private int size;
 private int maxsize;
 
 private static final int FRONT = 1;
 
 public MinimumHeap(int maxsize) //Constructor with Max size of heap
 {
 this.maxsize = maxsize;
 this.size = 0;
 heap = new int[this.maxsize + 1];
 heap[0] = Integer.MIN_VALUE; //Initialize root with minimum value
 }
 
 private int parent(int pos) //Get parent index of a node
 {
 return pos / 2;
 }
 
 private int leftChild(int pos) //Get left Child index of a node
 {
 return (2 * pos);
 }
 
 private int rightChild(int pos) //Get right Child of a node
 {
 return (2 * pos) + 1;
 }
 
 private boolean isLeaf(int pos)
 {
 if (pos >= (size / 2) && pos <= size)
 {
 return true;
 }
 return false;
 }
 
 private void swap(int fpos, int spos)
 {
 int tmp;
 tmp = heap[fpos];
 heap[fpos] = heap[spos];
 heap[spos] = tmp;
 }
 
 private void heapify(int pos)
 {
 if (!isLeaf(pos))
 {
 if ( heap[pos] > heap[leftChild(pos)] || heap[pos] > heap[rightChild(pos)])
 {
 if (heap[leftChild(pos)] < heap[rightChild(pos)])
 {
 swap(pos, leftChild(pos));
 heapify(leftChild(pos));
 }else
 {
 swap(pos, rightChild(pos));
 heapify(rightChild(pos));
 }
 }
 }
 }
 
 public void insert(int element)
 {
 heap[++size] = element;
 int current = size;
 
 while (heap[current] < heap[parent(current)])
 {
 swap(current,parent(current));
 current = parent(current);
 }  
 }
 
 public void print()
 {
 for (int i = 1; i <= size / 2; i++ )
 {
 System.out.print(\" PARENT : \" + heap[i] + \" LEFT CHILD : \" + heap[2*i]
 + \" RIGHT CHILD :\" + heap[2 * i + 1]);
 System.out.println();
 }
 }
 
 public void createHeap()
 {
 for (int pos = (size / 2); pos >= 1 ; pos--)
 {
 heapify(pos);
 }
 }
 
 public int remove() //To remove a node
 {
 int popped = heap[FRONT];
 heap[FRONT] = heap[size--];
 heapify(FRONT);
 return popped;
 }
 }
----------------------
TestMinHeap.java
----------------------
public class TestMinHeap {
    public static void main(String...arg)
 {
 System.out.println(\"Heap is being created with values 4,2,48,12,26,28,9 \");
   
 MinimumHeap minHeap = new MinimumHeap(15);
   
 //Inserting values in heap
 minHeap.insert(4);
 minHeap.insert(2);
 minHeap.insert(48);
 minHeap.insert(12);
 minHeap.insert(26);
 minHeap.insert(28);
 minHeap.insert(9);
 minHeap.createHeap();
 
 System.out.println(\"\  Heap Created.. Printing...\");
 minHeap.print();
 System.out.println(\"\ The Min val is \" + minHeap.remove());
 }
 }
OUTPUT:
Heap is being created with values 4,2,48,12,26,28,9
Heap Created.. Printing...
 PARENT : 2 LEFT CHILD : 4 RIGHT CHILD :9
 PARENT : 4 LEFT CHILD : 12 RIGHT CHILD :26
 PARENT : 9 LEFT CHILD : 48 RIGHT CHILD :28
The Min val is 2



