Implemenetation of binary Heap given beloiw:
* Binary Heap
*/
import java.util.Scanner;
import java.util.Arrays;
import java.util.NoSuchElementException;
/* Class BinaryHeap */
class BinaryHeap
{
/* The number of children each node has */
private static final int d = 2;
private int heapSize;
private int[] heap;
/* Constructor */
public BinaryHeap(int capacity)
{
heapSize = 0;
heap = new int[capacity + 1];
Arrays.fill(heap, -1);
}
/* Function to check if heap is empty */
public boolean isEmpty( )
{
return heapSize == 0;
}
/* Check if heap is full */
public boolean isFull( )
{
return heapSize == heap.length;
}
/* Clear heap /
public void makeEmpty( )
{
heapSize = 0;
}
/* Function to get index parent of i */
private int parent(int i)
{
return (i - 1)/d;
}
/* Function to get index of k th child of i */
private int kthChild(int i, int k)
{
return d * i + k;
}
/* Function to insert element /
public void insert(int x)
{
if (isFull( ) )
throw new NoSuchElementException("Overflow Exception");
/* Percolate up */
heap[heapSize++] = x;
heapifyUp(heapSize - 1);
}
/* Function to find least element */
public int findMin( )
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return heap[0];
}
/* Function to delete min element */
public int deleteMin()
{
int keyItem = heap[0];
delete(0);
return keyItem;
}
/* Function to delete element at an index */
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 = minChild(ind);
if (heap[child] < tmp)
heap[ind] = heap[child];
else
break;
ind = child;
}
heap[ind] = tmp;
}
/* Function to get smallest child */
private int minChild(int ind)
{
int bestChild = kthChild(ind, 1);
int k = 2;
int pos = kthChild(ind, k);
while ((k <= d) && (pos < heapSize))
{
if (heap[pos] < heap[bestChild])
bestChild = pos;
pos = kthChild(ind, k++);
}
return bestChild;
}
/* Function to print heap */
public void printHeap()
{
System.out.print("\nHeap = ");
for (int i = 0; i < heapSize; i++)
System.out.print(heap[i] +" ");
System.out.println();
}
}
/* Class BinaryHeapTest */
public class BinaryHeapTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Binary Heap Test\n\n");
System.out.println("Enter size of Binary heap");
/* Make object of BinaryHeap */
BinaryHeap bh = new BinaryHeap(scan.nextInt() );
char ch;
/* Perform Binary Heap operations */
do
{
System.out.println("\nBinary Heap Operations\n");
System.out.println("1. insert ");
System.out.println("2. delete min");
System.out.println("3. check full");
System.out.println("4. check empty");
System.out.println("5. clear");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
try
{
System.out.println("Enter integer element to insert");
bh.insert( scan.nextInt() );
}
catch (Exception e)
{
System.out.println(e.getMessage() );
}
break;
case 2 :
try
{
System.out.println("Min Element : "+ bh.deleteMin());
}
catch (Exception e)
{
System.out.println(e.getMessage() );
}
break;
case 3 :
System.out.println("Full status = "+ bh.isFull());
break;
case 4 :
System.out.println("Empty status = "+ bh.isEmpty());
break;
case 5 :
bh.makeEmpty();
System.out.println("Heap Cleared\n");
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* Display heap */
bh.printHeap();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Must be in JAVA 1. Design and implement a Binary Heap class that must support "insert"...
5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...
Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...
In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...
Min heap class implementation in Python.
Implement a min-using an array. Your min-heap class will have one private attribute, an array of integers. Implement the following methods for the min-heap class You may not use the built-in min function. init - Constructor makes an empty heap str - Prints the heap out in any way you want for debugging only) makenull(self) - Makes the heap empty insert(self,x) - Insert element x into the heap parent(self,i) - Returns the index of...
Write a program in Java to implement the min-priority queue using min-heap data structure. Implement the min-heap data structure using a String array of 5 cells. (Do not use Java in-built PriorityQueue class.) [In a min-heap, the root node and the intermediate node vales are always smaller than their children.] First, take 5 names from the user and insert them in the min-priority queue. Then print the elements of the queue. After that, delete two elements from the queue and...
Please use Java Design a class called supportTicket that handles information about reported IT support issues. A ticket includes an id number, the location of the equipment (e.g., building and room number), a short description of the issue, and a priority level. A valid priority level is a positive integer value with 0 being the highest priority level Tickets are serviced based on their priority level, from highest to lowest. Assume each ticket is assigned a unique priority level. When...
PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....
in JAVA please thanks. (need answer in a few hours !!)
Exercise #1: Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search algorithm discussed in the lecture slides. Define method LinearSearch() to implement linear search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Define method BinarySearch() to implement binary search of an array of...
Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. Note that as discussed in the video lecture, the index range of the array implementation of a heap is 1:n, NOT 0:n-1 • parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None •...
1. In Lab 4, you developed a program to build a Max Heap, and then Heap Sort. Update the program by adding two additional functions: (a) AddData(A, N, V) where V is the new value added. (b) Delete a data Delete by giving the index of the data position Make sure to display the array after calling each of the function. 2. Write a program to implement Binary Search Algorithm, which will return the index of the data searched (V)....