Please use Java to solve the following Solution import java
Solution
import java.util.Arrays;
import java.util.NoSuchElementException;
public class TernaryPQ
{
private int heapSize;
private int[] heap;
public TernaryPQ ()
{
heapSize = 0;
heap = new int[10 + 1];
Arrays.fill(heap, -1);
}
public boolean isEmpty( )
{
return heapSize == 0;
}
public boolean isFull( )
{
return heapSize == heap.length;
}
public void clear( )
{
heapSize = 0;
}
public int parent(int i)
{
return (i - 1)/3;
}
public int kthChild(int i, int k)
{
return 3 * i + k;
}
/** Function to insert element */
public void insert(int x)
{
if (isFull( ) )
throw new NoSuchElementException(\"Overflow Exception\");
heap[heapSize++] = x;
heapifyUp(heapSize - 1);
}
public int findMax( )
{
if (isEmpty() )
throw new NoSuchElementException(\"Underflow Exception\");
return heap[0];
}
public int delete(int ind)
{
if (isEmpty() )
throw new NoSuchElementException(\"Underflow Exception\");
int keyItem = heap[ind];
heap[ind] = heap[heapSize - 1];
heapSize--;
heapifyDown(ind);
return keyItem;
}
/** Function heapifyUp **/
private void heapifyUp(int childInd)
{
int tmp = heap[childInd];
while (childInd > 0 && tmp > heap[parent(childInd)])
{
heap[childInd] = heap[ parent(childInd) ];
childInd = parent(childInd);
}
heap[childInd] = tmp;
}
/** Function heapifyDown **/
private void heapifyDown(int ind)
{
int child;
int tmp = heap[ ind ];
while (kthChild(ind, 1) < heapSize)
{
child = maxChild(ind);
if (heap[child] > tmp)
heap[ind] = heap[child];
else
break;
ind = child;
}
heap[ind] = tmp;
}
public int maxChild(int ind)
{
int bestChild = kthChild(ind, 1);
int k = 2;
int pos = kthChild(ind, k);
while ((k <= 3) && (pos < heapSize))
{
if (heap[pos] > heap[bestChild])
bestChild = pos;
pos = kthChild(ind, k++);
}
return bestChild;
}
public void printHeap()
{
System.out.print(\"\ Heap = \");
for (int i = 0; i < heapSize; i++)
System.out.print(heap[i] +\" \");
System.out.println();
}
public static void main(String[] args)
{
TernaryPQ myheap = new TernaryPQ( );
myheap.insert(1);
myheap.insert(2);
myheap.insert(3);
myheap.printHeap();
System.out.println(\"maximum element is \"+ myheap.findMax());
myheap.insert(4);
myheap.insert(5);
myheap.printHeap();
// extracting the maximam
myheap.delete(0);
myheap.printHeap();
System.out.println(\"maximum element is \"+ myheap.findMax());
}
}


