Program a minmax heap java I have written out all the methods for the class minM
ID: 3569220 • Letter: P
Question
Program a minmax heap java
I have written out all the methods for the class minMaxHeap but I can't get the main to work properly. Here are the assignment requirements:
CS146 Programming Project II:
Build a program that implements a min-max heap (of integers). Your ADT must support the following operations:
buildMinMaxHeap(int array) given an array of integers.
Int peekMin()
Int peekMax()
Int deleteMin()
Int deleteMax()
Insert(int element)
printMinMaxHeap()
Your program should accept a file containing commands on what to do, i.e. one command per line. As an example an input file containing:
buildMinMaxHeap : 1, 4, 2, 3, 7, 6, 10
peekMin
peekMax
insert 25
insert 107
printMinMaxHeap
will direct your program to create a min-max heap with the given elements, will print the minimum, will print the maximum, will insert 25 and then 107, and finally print the resulting heap as one level per line starting with the root the standard output.
Here is what I've written so far:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class minMaxHeap {
private static int currentSize;
private static int[] arr;
private static int max;
public minMaxHeap(int size)
{
currentSize = 0;
arr = new int[size + 1];
}
public static void buildMinMaxHeap(int[] array)
{
for(int i = 0; i < array.length ; i++)
{
for(int j = currentSize / 2; j > 0; j--)
{
percolate(j);
}
}
}
public static int left(int hole){return (2*hole);}
public static int right(int hole){return (2*hole) + 1;}
public static boolean leaf(int index)
{
if(index >= currentSize /2 && index <= currentSize)
{
return true;
}
return false;
}
public static void swap(int first, int second)
{
int temp;
temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
public static void percolate(int index)
{
if(leaf(index) == false)
{
if(arr[index]%2 == 0)
{
percolateDown(index);
}
else
{
percolateUp(index);
}
}
}
public static void percolateDown(int index)
{
if(!leaf(index))
{
if(arr[index] > arr[left(index)] || arr[index] > arr[right(index)])
{
if(arr[left(index)] < arr[right(index)])
{
swap(index, left(index));
percolate(left(index));
}
else
{
swap(index, right(index));
percolate(right(index));
}
}
}
}
public static void percolateUp(int index)
{
if(!leaf(index))
{
if(arr[index] < arr[left(index)] || arr[index] < arr[right(index)])
{
if(arr[left(index)] > arr[right(index)])
{
swap(index, left(index));
percolate(left(index));
}
else
{
swap(index, right(index));
percolate(right(index));
}
}
}
}
public static void insert(int value)
{
arr[currentSize] = value;
currentSize++;
}
public static int deleteMin()
{
int first = arr[0];
arr[0] = arr[currentSize--];
percolate(arr[0]);
return first;
}
public static int peekMin()
{
if(currentSize == 0)
{
return 0;
}
else
{
return arr[0];
}
}
public static int peekMax()
{
if(currentSize == 0)
{
return 0;
}
else
{
max = arr[0];
for(int i = 0; i < arr.length; i++)
{
if(max < arr[i])
{
max = arr[i];
}
}
return max;
}
}
public static int deleteMax()
{
int popped = arr[0];
int value = 0;
for(int i = 0; i < arr.length; i++)
{
if(popped < arr[i])
{
popped = arr[i];
value = i;
}
}
arr[value] = arr[currentSize--];
percolate(value);
return popped;
}
public static void printMinMaxHeap()
{
for (int i = 1; i <= currentSize / 2; i++ )
{
System.out.print( arr[i] + " " + arr[2*i]
+ " " + arr[2 * i + 1] + " ");
}
}
Explanation / Answer
public class MaxHeap { private int[] Heap; private int size; private int maxsize; private static final int FRONT = 1; public MaxHeap(int maxsize) { this.maxsize = maxsize; this.size = 0; Heap = new int[this.maxsize + 1]; Heap[0] = Integer.MAX_VALUE; } private int parent(int pos) { return pos / 2; } private int leftChild(int pos) { return (2 * pos); } private int rightChild(int pos) { return (2 * pos) + 1; } private boolean isLeaf(int pos) { if (pos >= (size / 2) && posRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.