For this assignment, you will be coding a MinHeap that is backed by an array of contiguous elements. Here is a tree and array representation of the same MinHeap: IMPORTANT: • You will be given 5 attempts on this assignment, with a 30 minute cooldown between submissions. • Please run your code before each submission to ensure that there are no formatting errors! If there are formatting errors in your code, your code will not be graded and a submission attempt will be logged. For more information, please review the Vocareum overview below. Properties A MinHeap is a type of binary tree with two main properties: • Shape Property: The tree must be complete. All levels of the tree must be full except the bottom-most level. If the bottom-most level is not full, it must be filled from left to right with no gaps. • Order Property: Each node's data is smaller than the data in its children. There is no explicit relationship between sibling nodes. These properties guarantee that the smallest element in the heap will be at the root of the heap. Implementation Details Although heaps are usually classified as a type of tree, they are commonly implemented using an array due to their completeness. In your implementation, you should leave index 0 empty and begin your heap at index 1. This will make the arithmetic for finding parent and children indices simpler. When resizing your backingArray, double the current capacity. Therefore, if the initial capacity is 13, the first resize should bring the total backingArray capacity to 26. Note that this includes the 0th index! You may assume that your implementation does not need to handle duplicate elements. That is, the add method will never be passed duplicates and the remove method will never have to deal with the heap having duplicates. To be clear, your implementation would most likely work even if we were to test for duplicates; however, this will help remove ambiguity surrounding grading and testing your implementation.
Provided Code:
public class MinHeap {
private int[] heap;
private int size;
private int capacity;
public MinHeap() {
this.capacity = 13;
this.size = 0;
this.heap = new int[capacity];
}
private int parent(int index) {
return index / 2;
}
private int leftChild(int index) {
return index * 2;
}
private int rightChild(int index) {
return index * 2 + 1;
}
private boolean hasParent(int index) {
return index > 1;
}
private boolean hasLeftChild(int index) {
return leftChild(index) <= size;
}private boolean hasRightChild(int index) {
return rightChild(index) <= size;
}
private void swap(int index1, int index2) {
int temp = heap[index1];
heap[index1] = heap[index2];
heap[index2] = temp;
}
private void resize() {
capacity *= 2;
int[] newHeap = new int[capacity];
System.arraycopy(heap, 1, newHeap, 1, size);
heap = newHeap;
}
public void add(int value) {
if (size == capacity - 1) {
resize();
}}
heap[++size] = value;
upHeap();
}
private void upHeap() {
int index = size;
while (hasParent(index) && heap[index] < heap[parent(index)]) {
swap(index, parent(index));
index = parent(index);
}
}
public int remove() {
if (size == 0) {
throw new NoSuchElementException("Error: heap is empty");
}
int min = heap[1];
heap[1] = heap[size--];
downHeap();
return min;
}
private void downHeap() {
int index = 1;
while (hasLeftChild(index)) {
int smallerChildIndex = leftChild(index);(hasRightChild(index) && heap[rightChild(index)] < heap[smallerChildIndex]) {
smallerChildIndex = rightChild(index);
}
if (heap[index] < heap[smallerChildIndex]) {
break;
} else {
swap(index, smallerChildIndex);
}
index = smallerChildIndex;
}
}
public int peek() {
if (size == 0) {
throw new NoSuchElementException("Error: heap is empty");
}
return heap[1];
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public static void main(String[] args) {
MinHeap minHeap = new MinHeap();
minHeap.add(5);
minHeap.add(3);
minHeap.add(8);
minHeap.add(1);
minHeap.add(4);
minHeap.add(7);
minHeap.add(2);
System.out.println("Size of the heap: " + minHeap.size());
System.out.println("Minimum value in the heap: " + minHeap.peek());
System.out.print("Heap elements: ");
while (!minHeap.isEmpty()) {
System.out.print(minHeap.remove() + " ");
}
System.out.println();
}
}


Trending now
This is a popular solution!
Step by step
Solved in 3 steps









