2つのフィールド... removeMinとのminheapと指定されたキーを削除します

newprogrammer:

私は、ファイルから国と自分のGDPのリストを読み込むとのminheapにそれらを挿入するためのプログラムを持っています。ヒープは、国オブジェクトのコレクション(配列)です。国オブジェクトは、持っている2つのフィールドを。GDP値を保存するために、国の2文字のコードとGDPと呼ばれる整数フィールドを保持している名前と呼ばれる文字列フィールド、。挿入方法は、挿入のためのキーとしてGDP値を使用します。これはのminheapあるので、最低限のGDPを持つ国は常にルートであることを行っています。また、ヒープの他のすべての特性は、各挿入および除去後に満足されなければなりません。入力ファイル内の重複したGDP値があってはなりません。

私は、以下の機能を実装するためにHeap.javaクラスを完了する必要があります。

removeMinGDP() ; ヒープから最小GDPと国を削除し、それを返す必要があります。除去した後、ヒーププロパティを復元する必要があります。ヒープが空だった場合はnullを返します。

removeGivenGDP(int型GDP) 与えられたGDP値を持つ国を見つけて、それを削除する必要があります。除去した後、ヒーププロパティを復元する必要があります。この機能は削除された国の名前を返す必要があります。与えられたGDP値を持つ国が見つからない場合は、空の文字列を返します。

私はremoveMinGDP上のいくつかの助けを必要としています。私は根を除去して、最大の要素と交換することでしょうね。私はdownheapする必要があります知っています。私は一般的な考えを知っているが、それを実行する方法がわかりません。したがって、基本的に私は、私は2つのプライベートメソッドを作成する必要がありましたので、removeMinGDPはパラメータを受け付けませんので立ち往生んだけど、私はdeleteMinに何を渡すことはありません。

私は、私はまだremoveGivenGDP上で開始されていない(ただし、ヘルプは喜んで受け付けます)知っています。そして、私は非常に少ない経験をコーディングしているので、生きている私を食べないでください。


  private Country[] storage; //the actual heap storing elements
  private int size; //number of elements currently in the heap
  private int cap; //capacity of the heap

  Heap (int c) {
    size = 0;
    cap = c;
    storage = new Country[cap];
  }

  public void insert(Country c) {
    if (size >= cap) {
      throw new IllegalStateException("Heap is full");
    }
    int curr = size++;
    storage[curr] = c;
    while ((curr != 0) && (storage[curr].gdp < storage[parent(curr)].gdp)) {
      exch(storage, curr, parent(curr));
      curr = parent(curr);
    }

  }

  private void exch(Country[] arr, int i, int j) {
    Country tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  private int parent(int i) {
    if (i <= 0) return -1;
    return (i-1)/2;
  }

  public int numCountries () {
    return size;
  }

  public boolean isEmpty() {
    return size == 0;
  }

  private boolean isLeaf(int pos)
  { 
    return (pos >= size/2) && (pos < size); 

  }

  private int leftchild(int pos) {
    if (pos >= size/2) return -1;
    return 2*pos + 1;
  }


  private int rightchild(int pos) {
    if (pos >= (size-1)/2) return -1;
    return 2*pos + 2;
  }

//***************************************************************************

  public Country removeMinGDP() {
    /**************************
     * remove the country with minimum  GDP
     * from the heap and return it. restore
     * the heap properties.
     * return null if heap is empty.
    ***************************/
    if(isEmpty()) return null;
    else{
      return deleteMin(arr,size);
  }
  }

  private Country deleteMin(Country arr[], int n){
    int last = arr[n-1];
    Country temp = arr[0];
    arr[0] = last;
    n = n-1;
    downheap(arr,n,0);
    return temp.name;
  }

  private void downheap(Country arr[], int n, int i){
    int biggest = i;
    int l = 2 * i + 1;
    int r = 2 * i + 2;

    if(l < n && arr[l].gdp > arr[biggest].gdp) biggest = l;
    if(r < n && arr[r].gdp > arr[biggest].gdp) biggest = r;
    if(biggest != i){
      int swap = arr[i];
      arr[i] = arr[biggest];
      arr[biggest] = swap;

      downheap(arr, n, biggest);
    }
  }


  public String removeGivenGDP(int gdp) {
    /**************************
     * TODO: find the country with the given GDP 
     * value and remove it. return the name of 
     * the country and restore the heap property
     * if needed. If no countries with the given GDP
     * were found, return empty string ""
    ***************************/
    return "";
  }
}

ここでは主な機能はあります。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

class Main {
  public static void main(String[] args) throws FileNotFoundException {
    File input = new File("countries.txt");

    //Creating Scanner instance to read File
    Scanner sc = new Scanner(input);

    //Create an instance of MinHeap
    Heap h = new Heap(10);

    //Reading each line of file using Scanner class
    while(sc.hasNextLine()){
        String[] tokens = sc.nextLine().split(",");
        System.out.println("Inserted: " + tokens[0] + " -> " + tokens[1]);
        h.insert(new Country(tokens[0], Integer.parseInt(tokens[1].trim())));
    }

    System.out.println("\n\nTotal number of countries inserted is: " + h.numCountries());
    System.out.println("The name of the country with a GDP value of 2314 is " + h.removeGivenGDP(2314));
    System.out.println("Total number of countries now is: " + h.numCountries());

    System.out.println("\n\nCountries in ascending order of GDP values: ");
    while (!h.isEmpty()) {
      Country tmp = h.removeMinGDP();
      System.out.println(tmp.name + " -> " + tmp.gdp);
    } 

  }
}
ジムMischel:

与えられたGDPを削除するには、する必要があります。

  1. 検索storage要求された値が含まれているエントリの配列を。
  2. その場所に、ヒープ内の最後のノードをコピーします。
  3. 1でサイズを小さくしてください。
  4. 新しい値(あなたが最後のノードからコピー1)がその親よりも小さい場合は、ヒープに、そのノードを取捨選択する必要があります。そうしないと、その適切な場所に、ヒープをそれを取捨選択する必要があります。

はい、その最後のノードは確かにすることができますが、削除したノードの親よりも小さくします。参照https://stackoverflow.com/a/8706363/56778を例えば。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=365043&siteId=1