Java 有没有办法在我的代码中修复我的Max heapify

Java 有没有办法在我的代码中修复我的Max heapify,java,heapsort,max-heap,Java,Heapsort,Max Heap,问题是,我正在尝试修复max heapify,但由于错误不断发生,它无法正常工作。我一直在遵循几本书中的伪代码,但仍然显示错误。我试图用=交换A[I]到A[最大的],但它给出了一个错误 class Heap { // public for JUnit testing purposes public ArrayList<Integer> array; public int heap_size; public Heap(int size) { } public Heap(List<

问题是,我正在尝试修复max heapify,但由于错误不断发生,它无法正常工作。我一直在遵循几本书中的伪代码,但仍然显示错误。我试图用
=
交换
A[I]
A[最大的]
,但它给出了一个错误

class Heap {
// public for JUnit testing purposes
public ArrayList<Integer> array;
public int heap_size;

public Heap(int size) {
}
public Heap(List<Integer> source) {
    this(source, false);
}
public Heap(List<Integer> source, boolean incremental) {
}

public static int parent(int index) {
    return index/2;
}
public static int left(int index) {
    return 2 * index;
}
public static int right(int index) {
    return (2 * index) + 1;
}

public void maxHeapify(int i, int A)
{
  int l = left(i);
  int r = right(i);
  if(l <= A.heap_size && A[l] > A[i])
    largest = l;
  else
    largest = i;
  if(r <= A.heap_size && A[r] > A[largest])
    largest = r;
  if(largest != i)
  {
    A[i] = A[largest];
    maxHeapify(A,largest);
  }
}
public void buildMaxHeap() {
}
public void insert(Integer k) {
}
public Integer maximum() {
    return 0;
}
public Integer extractMax() {
    return 0;
 }
}
类堆{
//用于JUnit测试的公共
公共数组列表数组;
公共整数堆大小;
公共堆(整数大小){
}
公共堆(列表源){
这(来源,假);
}
公共堆(列表源,布尔增量){
}
公共静态int父级(int索引){
回报指数/2;
}
公共静态整型左(整型索引){
返回2*索引;
}
公共静态整数权限(整数索引){
收益率(2*指数)+1;
}
公共无效maxHeapify(整数i,整数A)
{
int l=左(i);
int r=右(i);
if(la[i])
最大=l;
其他的
最大=i;
if(r A[最大])
最大=r;
如果(最大!=i)
{
A[i]=A[最大];
maxHeapify(A,最大);
}
}
公共void buildMaxHeap(){
}
公共void插入(整数k){
}
公共整数最大值(){
返回0;
}
公共整数extractMax(){
返回0;
}
}
我期待它运行,但我得到一个错误

    Heap.java:31: error: int cannot be dereferenced
  if(l <= A.heap_size && A[l] > A[i])
           ^
   Heap.java:31: error: array required, but int found
  if(l <= A.heap_size && A[l] > A[i])
                          ^
  Heap.java:31: error: array required, but int found
  if(l <= A.heap_size && A[l] > A[i])
                                 ^
    Heap.java:32: error: cannot find symbol
    largest = l;
    ^
   symbol:   variable largest
   location: class Heap
Heap.java:31:错误:无法取消对int的引用
if(la[i])
^
java:31:错误:需要数组,但找到int
if(la[i])
^
java:31:错误:需要数组,但找到int
if(la[i])
^
java:32:错误:找不到符号
最大=l;
^
符号:变量最大
位置:类堆

如果可以,请提供帮助。

==
是一个比较运算符。如果要分配值,应使用
=
运算符:

A[i] = A[largest];

=
是一个比较运算符。如果要分配值,应使用
=
运算符:

A[i] = A[largest];

根据错误消息,问题在于maxHeapify的参数A是一个整数值。您需要将参数A作为数组传递

public void maxHeapify(int i, int[] A) {
    int largest = i;
    int l = left(i);
    int r = right(i);

    if(l < A.length && A[l] > A[largest])
        largest = l;
    if(r < A.length && A[r] > A[largest])
        largest = r;

    if(largest != i)
    {
        int tmp = A[i];
        A[i] = A[largest];
        A[largest] = tmp;
        maxHeapify(largest, A);
    }
}
public void maxHeapify(int i,int[]A){
int=i;
int l=左(i);
int r=右(i);
如果(lA[最大])
最大=l;
如果(rA[最大])
最大=r;
如果(最大!=i)
{
int tmp=A[i];
A[i]=A[最大];
A[最大]=tmp;
maxHeapify(最大,A);
}
}
我已经在下面的代码中测试了上面的maxHeapify

public class HeapMain {
    public static void main(String[] args) {
        // Note: ignore the value at index 0
        int[] A = new int[]{-1, 3, 9, 10, 4, 2, 33, 5};
        Heap heap = new Heap(A.length);
        for (int i = heap.parent(A.length - 1); i > 0; i--) {
            heap.maxHeapify(i, A);
        }

        // You will get => 33 9 10 4 2 3 5
        for (int i = 1; i < A.length; i++) {
            System.out.print(A[i]);
            System.out.print(" ");
        }
        System.out.println();
    }
}
公共类HeapMain{
公共静态void main(字符串[]args){
//注意:忽略索引0处的值
int[]A=新的int[]{-1,3,9,10,4,2,33,5};
堆=新堆(A.长度);
对于(inti=heap.parent(A.length-1);i>0;i--){
heap.maxHeapify(i,A);
}
//您将得到=>339104235
for(int i=1;i
注意:parent、left和right方法假定数组中堆的节点是从索引1保存的

以下URL可能会有所帮助


根据错误消息,问题在于maxHeapify的参数A是一个整数值。您需要将参数A作为数组传递

public void maxHeapify(int i, int[] A) {
    int largest = i;
    int l = left(i);
    int r = right(i);

    if(l < A.length && A[l] > A[largest])
        largest = l;
    if(r < A.length && A[r] > A[largest])
        largest = r;

    if(largest != i)
    {
        int tmp = A[i];
        A[i] = A[largest];
        A[largest] = tmp;
        maxHeapify(largest, A);
    }
}
public void maxHeapify(int i,int[]A){
int=i;
int l=左(i);
int r=右(i);
如果(lA[最大])
最大=l;
如果(rA[最大])
最大=r;
如果(最大!=i)
{
int tmp=A[i];
A[i]=A[最大];
A[最大]=tmp;
maxHeapify(最大,A);
}
}
我已经在下面的代码中测试了上面的maxHeapify

public class HeapMain {
    public static void main(String[] args) {
        // Note: ignore the value at index 0
        int[] A = new int[]{-1, 3, 9, 10, 4, 2, 33, 5};
        Heap heap = new Heap(A.length);
        for (int i = heap.parent(A.length - 1); i > 0; i--) {
            heap.maxHeapify(i, A);
        }

        // You will get => 33 9 10 4 2 3 5
        for (int i = 1; i < A.length; i++) {
            System.out.print(A[i]);
            System.out.print(" ");
        }
        System.out.println();
    }
}
公共类HeapMain{
公共静态void main(字符串[]args){
//注意:忽略索引0处的值
int[]A=新的int[]{-1,3,9,10,4,2,33,5};
堆=新堆(A.长度);
对于(inti=heap.parent(A.length-1);i>0;i--){
heap.maxHeapify(i,A);
}
//您将得到=>339104235
for(int i=1;i
注意:parent、left和right方法假定数组中堆的节点是从索引1保存的

以下URL可能会有所帮助