JAVA
Modify the existing (methods already in the file may be modified but not deleted) the code to a MaxHeap and write a driver class to do the following:
1. Insert item into a Heap
2. Delete Max from the Heap
3. Display the Heap array
4. Convert an array to Heap
1,2 & 3 must all work on the same heap array.
Option 4 is independent and must take in input from the user of an integer array and convert that array to a heap array.
The driver class must be written inside the Heap class.
________________________________________________________________________________________________________________
import java.util.*;
public class Heap {
private int[] a;
private int n;
public Heap()
{
a=new int[1];
n=0;
a[0]=-100;
}
public Heap(int maxSize)
{
a= new int[maxSize];
n=0;
a[0]=-100;
}
public void insertItem(int value)
{
n++;
a[n]=value;
upHeap(n);
}
private void upHeap(int i)
{
int k=a[i];
int iparent=i/2;
while(a[iparent]>k)
{
a[i]=a[iparent];
i=iparent;
iparent=i/2;
}
a[i]=k;
}
public int removeMin()
{
if(n==0)
throw new
NoSuchElementException("Heap is Empty!!");
int minValue=a[1];
a[1]=a[n];
n--;
downHeap(1);
return minValue;
}
public void downHeap(int i)
{
int k=a[i];
int left=2*i;
int right=left+1;
while (right<=n)
{
if(k<=a[left]&&k<=a[right])
{
a[i]=k;
return;
}
else
if(a[left]>a[right])
{
a[i]=a[right];
i=right;
}
else
{
a[i]=a[left];
i=left;
}
left=2*i;
right=left+1;
}
/* If number of nodes is even there
is one node without right child */
if(left==n&&k>a[left])
{
a[i]=a[left];
i=left;
}
a[i]=k;
}
public void min_heapify (int Arr[ ] , int i, int
N)
{
int left = 2*i;
int right = 2*i+1;
int smallest;
if(left <= N && Arr[left] < Arr[ i ]
)
smallest = left;
else
smallest = i;
if(right <= N && Arr[right] <
Arr[smallest] )
smallest = right;
if(smallest != i)
{
int temp=Arr[i];
Arr[i]=Arr[smallest];
Arr[smallest]=temp;
min_heapify (Arr, smallest,N);
}
}
}
____________________________________________________________________________________________________________________
public class Node<E>{
public int priority;
public E data;
public Node<E> nextNode;
public Node(E i, int pr)
{
data=i;
priority=pr;
nextNode=null;
}
}
____________________________________________________________________________________________________________________
public interface PriorityQueueInterface<E> {
public void insertItem(E data,int priority);
public E removeMin();
public boolean isEmpty();
public E minElement(); /* Equal to peek*/
public int minPriority();
public void display();
}
____________________________________________________________________________________________________________________
import java.util.*;
/* This is an implementation of a sorted List.
* insertItem() always inserts the element according to
priority
*/
public class PriorityQueueLL<E> implements PriorityQueueInterface<E>{
private Node<E> head;
public PriorityQueueLL()
{
head=null;
}
public void insertItem(E element, int elementpr)
{
Node<E> temp,p;
temp=new
Node<E>(element,elementpr);
if(isEmpty()||elementpr<head.priority)
{
temp.nextNode=head;
head=temp;
}
else
{
p=head;
while(p.nextNode!=null &&
p.nextNode.priority<=elementpr)
p=p.nextNode;
temp.nextNode=p.nextNode;
p.nextNode=temp;
}
}
public E removeMin()
{
E delData;
if(isEmpty())
{
System.out.println("Queue Underflow");
throw new
NoSuchElementException();
}
else
{
delData=head.data;
head=head.nextNode;
}
return delData;
}
public E minElement()
{
if(isEmpty())
{
System.out.println("Queue is Empty!!");
throw new
NoSuchElementException();
}
else
return
head.data;
}
public int minPriority()
{
if(isEmpty())
{
System.out.println("Queue is Empty!!");
throw new
NoSuchElementException();
}
else
return
head.priority;
}
public boolean isEmpty()
{
return (head==null);
}
public void display()
{
Node<E> p=head;
if(isEmpty())
System.out.println("Queue is Empty!");
else
{
System.out.println("Queue is :");
System.out.println("Element priority");
while(p!=null)
{
System.out.println(p.data+" "+p.priority);
p=p.nextNode;
}
}
System.out.println("");
}
}
____________________________________________________________________________________________________________________
// Heap.java
import java.util.NoSuchElementException;
public class Heap {
private int[] a;
private int n;
public Heap()
{
a=new int[1];
n=0;
a[0]=-100;
}
public Heap(int maxSize)
{
a= new int[maxSize];
n=0;
a[0]=-100;
}
// method to insert value into the heap
public void insertItem(int value)
{
// if heap is not empty
if(n < a.length)
{
// insert the
value at the end of the heap
n++;
a[n]=value;
upHeap(n); //
move the value up the heap until it is placed in its correct
position
}else
System.out.println("Heap is full");
}
// method to to move the value at i so as to maintain
the heap
private void upHeap(int i)
{
int k=a[i];
int iparent=i/2;
// loop to move the element up so
that it occupies its correct position to maintain a Heap
while(iparent>=1 &&
a[iparent] < k)
{
// if value at
iparent < k, move value at iaprent to i
a[i]=a[iparent];
i=iparent;
iparent=i/2;
}
a[i]=k; // place k at i
}
// method to remove and return the maximum value of
the heap
public int removeMax()
{
// empty heap
if(n==0)
throw new
NoSuchElementException("Heap is Empty!!");
int maxValue=a[1]; // get the
maximum element
a[1]=a[n]; // set the last element
of array as first element
n--; // decrement n
downHeap(1); // process the array
to restore back the heap
return maxValue;
}
// helper method to restore the Heap
private void downHeap(int i)
{
int k=a[i]; // get the element at
i
int left=2*i; // get its left child
index
int right=left+1; // get its right
child index
// loop that continues till right
child exists
while (right<=n)
{
// if k is in
its correct position, set ith element to k and return
if(k >=
a[left] && k >=a [right])
{
a[i]=k;
return;
}
else if(a[left]
< a[right]) // if left child < right child
{
// set i to right child
a[i]=a[right];
i=right;
}
else // if left
child > right child
{
// set i to left child
a[i]=a[left];
i=left;
}
left=2*i;
right=left+1;
}
/* If number of nodes is even there
is one node without right child */
if(left==n && k <
a[left])
{
a[i]=a[left];
i=left;
}
a[i]=k;
}
// method to display the heap array
public void display()
{
for(int i=1;i<=n;i++)
System.out.print(a[i]+" ");
System.out.println();
}
public void max_heapify (int Arr[ ] , int i, int
N)
{
int left = 2*i;
int right = 2*i+1;
int largest;
if(left <= N &&
Arr[left] > Arr[ i ] )
largest =
left;
else
largest =
i;
if(right <= N &&
Arr[right] > Arr[largest] )
largest =
right;
if(largest != i)
{
int
temp=Arr[i];
Arr[i]=Arr[largest];
Arr[largest]=temp;
max_heapify
(Arr, largest,N);
}
}
// method to return a Heap from the input array
public static Heap convertToHeap(int Arr[])
{
// create a Heap of one more than
the input array's length
Heap heap = new
Heap(Arr.length+1);
// loop to insert elements from Arr
to heap
for(int
i=0;i<Arr.length;i++)
heap.insertItem(Arr[i]);
return heap; // return the
heap
}
public static void main(String[] args) {
// test the Max Heap
int arr[] =
{23,12,77,10,5,1,89,19,20,165,188,76,23};
Heap heap =
convertToHeap(arr);
System.out.println("Heap array:
");
heap.display();
System.out.println("Removing max
from heap: ");
for(int
i=0;i<arr.length;i++)
{
System.out.println(heap.removeMax());
}
}
}
//end of Heap.java
Output:

JAVA Modify the existing (methods already in the file may be modified but not deleted) the...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...
import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial package declaration that might be added to your file in * case you edit it in eclipse. * * The goal of the homework is to create two ArrayList based implementations of * a Priority Queue as explained in Section 9.2 (in 9.2.4 and 9.2.5) of the * textbook. * * These are to be made by completing the classes PQunsorted and PQsorted as...
JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...
Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....
// CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap { private Comparable a heap; // array of heap entries private static final int DEFAULT MAX SIZE = 100; private int lastIndex; // index of last entry public MinHeap() { heap = new Comparable (DEFAULT_MAX_SIZE]; lastIndex = 0; } // end default constructor public MinHeap (int maxSize) { heap = new Comparable (maxSize); lastIndex = 0; } // end constructor public MinHeap (Comparable[] entries)...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...
*JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> { private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10; public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0;...
Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...