1

The issue is that I am trying to fix the max heapify, it is not working as the error keeps occurring. I have been following the pseudocode from several books, yet errors are still shown. I am trying to exchange the A[i] to A[largest] by using = to exchange but it is giving an error instead

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

I am expecting it to run but i get an error

    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

If you can, please help.

ryan boss
  • 31
  • 1
  • 7

2 Answers2

0

== is a comparison operator. If you want to assign a value you should use the = operator:

A[i] = A[largest];
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

According to the error messages, the problem is that the argument A of maxHeapify is an integer value. You need to pass the argument A as an array.

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

I have tested the above maxHeapify in the following code.

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

Note: parent, left and right method assume that the nodes of the heap in an array are saved from index 1.

The following URL might be helpful.

https://www.youtube.com/watch?v=ixdWTKWSz7s

satojkovic
  • 689
  • 1
  • 4
  • 15