you will create a simple heap data structure with several methods including maxHeapify and buildMaxHeap. To get started, import the starter file (Heap.java) into the heap package you create in a new Java Project. Please do not change any of the method signatures in the Heap class. Implement the methods described below. You are free to test your code however you prefer. Remember that arrays are indexed starting at 0 in Java. This will change the math in the pseudocode. public int parent(int i) This method should return the index of the parent of the ith element of the data array. If the ith element is the root, then the method should return -1. public int left(int i) This method should return the index of the left child of the ith element of the data array. If the ith element is a leaf, then the index will potentially be greater than the size of the data array. That’s fine. public int right(int i) This method should return the index of the right child of the ith element of the data array. If the ith element is a leaf, then the index will potentially be greater than the size of the data array. That’s fine. public void maxHeapify(int loc) The method converts the tree rooted at the loc element in the array to a maxHeap. It assumes that the left and right children of loc are maxHeaps. public void buildMaxHeap() The method converts the data array into a maxHeap. Note that the entire data array might not be converted into a MaxHeap, just the portion of the array up until HeapSize. this is the method signature: package heap; import java.util.Arrays; public class Heap { private int[] data; private int heapSize; public Heap(int[] data,int heapSize) { this.data = data; this.heapSize = heapSize; } public void setHeapSize(int i) { heapSize = i; } public int getHeapSize() { return heapSize; } //return the parent of ith element in the array. //should return -1 if the ith element is the root of the heap public int parent(int i) { return 0; } //returns the index of the left child of the ith element in the array //for leaves the index will be greater than the heapSize public int left(int i) { return 0; } //returns the index of the right child of the ith element in the array //for leaves the index will be greater than the heapSize public int right(int i) { return 0; } //modifies the data array so that the tree rooted at the loc element //is a max heap. //Assumes that the trees rooted at the left and right children of loc //are max heaps public void maxHeapify(int loc) { } //converts the data array to an array that represents a max heap of size HeapSize public void buildMaxHeap() { } //helper method for debugging and printing public String toString() { return Arrays.toString(Arrays.copyOfRange(data, 0, heapSize)); } }
you will create a simple heap data structure with several methods including
maxHeapify and buildMaxHeap. To get started, import the starter file (Heap.java) into the heap
package you create in a new Java Project. Please do not change any of the method signatures in
the Heap class. Implement the methods described below. You are free to test your code however you
prefer.
Remember that arrays are indexed starting at 0 in Java. This will change the math in the
pseudocode.
public int parent(int i)
This method should return the index of the parent of the ith element of the data array. If the ith
element is the root, then the method should return -1.
public int left(int i)
This method should return the index of the left child of the ith element of the data array. If the ith
element is a leaf, then the index will potentially be greater than the size of the data array. That’s
fine.
public int right(int i)
This method should return the index of the right child of the ith element of the data array. If the ith
element is a leaf, then the index will potentially be greater than the size of the data array. That’s
fine.
public void maxHeapify(int loc)
The method converts the tree rooted at the loc element in the array to a maxHeap. It assumes that
the left and right children of loc are maxHeaps.
public void buildMaxHeap()
The method converts the data array into a maxHeap. Note that the entire data array might not be converted into a MaxHeap, just the portion of the array up until HeapSize.
this is the method signature:
package heap;
import java.util.Arrays;
public class Heap {
private int[] data;
private int heapSize;
public Heap(int[] data,int heapSize) {
this.data = data;
this.heapSize = heapSize;
}
public void setHeapSize(int i) {
heapSize = i;
}
public int getHeapSize() {
return heapSize;
}
//return the parent of ith element in the array.
//should return -1 if the ith element is the root of the heap
public int parent(int i) {
return 0;
}
//returns the index of the left child of the ith element in the array
//for leaves the index will be greater than the heapSize
public int left(int i) {
return 0;
}
//returns the index of the right child of the ith element in the array
//for leaves the index will be greater than the heapSize
public int right(int i) {
return 0;
}
//modifies the data array so that the tree rooted at the loc element
//is a max heap.
//Assumes that the trees rooted at the left and right children of loc
//are max heaps
public void maxHeapify(int loc) {
}
//converts the data array to an array that represents a max heap of size HeapSize
public void buildMaxHeap() {
}
//helper method for debugging and printing
public String toString() {
return Arrays.toString(Arrays.copyOfRange(data, 0, heapSize));
}
}
Step by step
Solved in 3 steps with 1 images