渗透堆的方法

时间:2016-11-04 17:18:40

标签: c# methods heap heapsort

我已经将一个整数文件读入一个数组,我必须把这个数组变成3个独立的堆。这是我当前的代码,但我遇到了渗透方法的问题。我设法用第一个整数n创建一个数组并创建一个数组。

现在我只实现了从文件中读取的程序,并根据文件中列出的n值中读取的第一个值(n)变成了数组 - 我不太清楚如何从那个单个数组中创建一个最小堆,因为我需要3个堆。该文件的示例是:

12 4 2 10 3 10 2 3 4 2 11 1 4 10

所以12是数组中的n个值,4是每个堆中的值#,因此有3个堆。

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");
        }
    }
}

0 个答案:

没有答案