0

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");
        }
    }
}
krlzlx
  • 5,752
  • 14
  • 47
  • 55
rahulchawla
  • 170
  • 1
  • 3
  • 20
  • It's difficult to understand what you're trying to do, and where you're having trouble. Your code is poorly formatted, and your explanation is too terse. Are you really trying to create three heaps in a single array? Why would you want to do such a thing? How do you determine which items go into which heap? Where, exactly, are you having trouble? – Jim Mischel Nov 04 '16 at 21:17
  • I am putting the values in a single array but yes I have to create 3 heaps,, which point to each other, the first 4 values will be in each array which are transformed to a heap, I need some guidance as I'm completely lost, I've managed to read the file data into a single array – rahulchawla Nov 05 '16 at 18:50
  • This question is a duplicate of http://stackoverflow.com/questions/40413572/reading-from-textfile-creating-array-creating-heap. Please do not double-post. Delete one of them. – Jim Mischel Nov 06 '16 at 12:32

0 Answers0