我有一个程序可以从文件中读取国家列表及其GDP,并将其插入MinHeap。堆是Country对象的集合(数组)。国家对象具有two字段。一个名为name的字符串字段,用于保存一个国家的两个字母的代码,以及一个名为gdp的整数字段,用于存储GDP值。 insert方法使用GDP值作为插入键。由于这是MinHeap,因此GDP最低的国家将永远是根源。另外,每次插入和删除之后,堆的所有其他属性都应满足。输入文件中不会有任何重复的GDP值。
我必须完成Heap.java类才能实现以下功能:
removeMinGDP();应该从堆中删除GDP最低的国家,然后将其返回。删除后,应还原堆属性。如果堆为空,则返回null。
removeGivenGDP(int gdp):应找到具有给定GDP值的国家并将其删除。删除后,应还原堆属性。此函数应返回被删除国家的名称。如果找不到具有给定GDP值的国家,则返回一个空字符串。
我需要一些关于removeMinGDP的帮助。我知道我将删除根并将其替换为最大元素。我知道我必须降低负担。我知道一般的想法,但不确定如何执行。因此,我基本上陷入了困境,因为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);
}
}
}
deleteMin
和downheap
不需要Country arr[]
或int n
参数。它们应直接对storage
和size
实例变量进行操作:
private Country deleteMin(){
int last = storage[size-1];
Country temp = storage[0];
storage[0] = last;
...
A类encapsulates状态:您的Heap类中的方法可以直接处理低级实现细节,例如“存储”数组。