Java 使用minHeapify()对数组排序,使用extractMin()返回元素
我使用minHeapify结构从数组中提取数字,并使用此方法“minHeapify()”对数组进行排序,使用extractMin()返回值最低的元素 它总是首先返回第一个元素,并且在计算负数时遇到问题。这是我的密码Java 使用minHeapify()对数组排序,使用extractMin()返回元素,java,arrays,sorting,heap,Java,Arrays,Sorting,Heap,我使用minHeapify结构从数组中提取数字,并使用此方法“minHeapify()”对数组进行排序,使用extractMin()返回值最低的元素 它总是首先返回第一个元素,并且在计算负数时遇到问题。这是我的密码 public class PQHeap implements PQ { Element[] eList; int size; public PQHeap(int maxElms){ eList= new Element[maxElm
public class PQHeap implements PQ {
Element[] eList;
int size;
public PQHeap(int maxElms){
eList= new Element[maxElms];
}
@Override
public Element extractMin() {
size = heapSize(eList);
if (size <= 0) {
System.out.println("Empty Array");
return null;
}
Element min = eList[0];
eList[0] = eList[size -1];
// sets the last index in the array to null
eList[size -1]= null;
size--;
minHeapify(eList, 0);
return min;
}
public void minHeapify(Element[] array, int num){
int l = left(num);
int r = right(num);
int smallest = num;
if (l < size && array[l].key < array[smallest].key){
smallest = l;
}
if (r < size && array[r].key < array[smallest].key){
smallest = r;
}
if (smallest != num){
swap(array, num, smallest);
minHeapify(array, smallest);
}
}
public void swap(Element[] array,int parent, int smallest){
Element tmp= array[parent];
array[parent] = array[smallest];
array[smallest] = tmp;
}
public int heapSize(Element[] array){
size = 0;
for (int i = 0; i <array.length; i++) {
if(array[i] != null){
size++;
}
}
return size;
}
@Override
public void insert(Element e) {
int i = 0;
for (int j = 0; j <eList.length; j++) {
if(eList[i]== null){
eList[i] = e;
break;
}else{
i++;
}
}
}
public int left(int i){
return 2 * i+1;
}
public int right(int i){
return 2 * i + 2;
}
}
公共类PQHeap实现PQ{
元素[]列表;
整数大小;
公共PQHeap(int-maxElms){
eList=新元素[maxElms];
}
@凌驾
公共元素extractMin(){
大小=heapSize(eList);
如果(size我解决了这个问题,我不知道是应该删除post还是听之任之,但解决方案如下。insert方法没有遵循minHeap结构:
public void insert(Element e) {
size = heapSize(eList);
eList[size] = e;
decreaseKey(eList, size, e.key);
}
public void decreaseKey(Element[] array, int i, int key){
array[i].key = key;
while (i > 0 && array[parent(i)].key > array[i].key){
swap(array,i,parent(i));
i = parent(i);
}
}
System.out.println();
System.out.println("Creating a PQHeap with room for 10 elements");
System.out.println();
PQ pq = new PQHeap(10);
System.out.println("Inserting elements with keys");
System.out.println(" 3, 5, 0, 100, -117, 1, 1, 1, 2, 3");
System.out.println("(and corresponding Integers as data)");
System.out.println();
pq.insert(new Element(3,new Integer(3)));
pq.insert(new Element(5,new Integer(5)));
pq.insert(new Element(0,new Integer(0)));
pq.insert(new Element(100,new Integer(100)));
pq.insert(new Element(-117,new Integer(-117)));
pq.insert(new Element(1,new Integer(1)));
pq.insert(new Element(1,new Integer(1)));
pq.insert(new Element(1,new Integer(1)));
pq.insert(new Element(2,new Integer(2)));
pq.insert(new Element(3,new Integer(3)));
System.out.println("Doing 10 extractMins (showing keys and data)");
System.out.println();
Element e;
for (int i=0; i<10; i++){
e = pq.extractMin();
System.out.println(e.key + " " + e.data);
public void insert(Element e) {
size = heapSize(eList);
eList[size] = e;
decreaseKey(eList, size, e.key);
}
public void decreaseKey(Element[] array, int i, int key){
array[i].key = key;
while (i > 0 && array[parent(i)].key > array[i].key){
swap(array,i,parent(i));
i = parent(i);
}
}