I have read a file of integers into an array and I have to turn that one array into 3 separate heaps. Here is my current code but I am having trouble with a percolating down method. I have managed to create an array with the first integer n
and create a single array.
For now I have only implemented the program to read from the file and based on the first value (n) read in the n
values listed in the file into an array - I'm not quite sure how to make a min heap from that single array as I need 3 heaps. Example of the file is:
12 4 2 10 3 10 2 3 4 2 11 1 4 10
So 12 is n number of values in the array and 4 is # of values in each heap therefore 3 heaps.
public class realheap{
private static final int DEFAULT_CAPACITY = 10;
private int[] heap1;
private int size;
private int maxSize;
private static final int START = 1;
private static int numofval;
private static int valinheap;
public realheap(int[] array){
heap1 = new int[valinheap+1];
heap1[0] = Integer.MIN_VALUE;
size = array.length;
buildHeap();
}
public void buildHeap(){
for(int k = size/2; k > 0; k--)
{
percolatingDown(k);
}
}
public void percolatingDown(int k){
int temp = heap1[k];
int child;
for(; 2*k <= size; k = child)
{
child = 2*k;
if(child != size &&
heap1[child].compareTo(heap1[child + 1]) > 0) child++;
if(tmp.compareTo(heap[child]) > 0) heap[k] = heap[child];
else
break;
}
heap[k] = tmp;
}
/*
public static void loadFile(String file) {
try {
Scanner sc = new Scanner(new File(file));
numofval = sc.nextInt();
valinheap = sc.nextInt();
int[] ar1 = new int[numofval+1];
ar1[0] = 3;
// 3 arrays to load textfile data into arrays will be later transformed to heaps
while(sc.hasNextInt()){
// for (int i=1;i<valinheap+1;i++){
//ar1[i] = sc.nextInt(); }
for (int i=1;i<numofval+1;i++){
ar1[i] = sc.nextInt();
}
}
sc.close();
}
catch (FileNotFoundException e){
System.out.println("File not found");
}
}
*/
//locates parent of index
private int getParent(int index){
return index/2;
}
//locates index of left
private int getLeftChild(int index){
return 2*index;
}
//locates the index
private int getRightChild(int index){
return (2*index)+1;
}
private void swap(int index1,int index2){
int temp = heap1[index1];
heap1[index1] = heap1[index2];
heap1[index2] = temp;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String files;
Scanner input= new Scanner(System.in);
System.out.println("Please enter the name of the file");
files=input.next();
input.close();
try {
Scanner sc = new Scanner(new File(files));
numofval = sc.nextInt();
valinheap = sc.nextInt();
int k = 1;
int[] ar1 = new int[numofval+1];
// 3 arrays to load textfile data into arrays will be later transformed to heaps
while(sc.hasNext()){
// for (int i=1;i<valinheap+1;i++){
//ar1[i] = sc.nextInt(); }
ar1[k] = sc.nextInt();
k++;
}
for (int i=1;i<numofval+1;i++){
System.out.println(ar1[i]);
}
sc.close();
}
catch (FileNotFoundException e){
System.out.println("File not found");
}
}
}