-1

For a project, am i working with heaps. In this project i have to "investigate" whether an array is a max heap.

These 2 rules apply for a max heap:

  1. The parent shall be bigger or equal to its child / children
  2. Each node, in a heap, shall contain an element

Therefore have i created 2 for loops checking whether these rules apply. Unfortunately for me, my code doesn't work.

I have 2 for loops:

 //Checks if there's a 0 value in the array. If so: Return false

    for (int i = 1; i <= A.length; i++) {
        System.out.println("A.length: " + A[i]);
        if (A[i] == 0) {
            System.out.println("weweh: "+ A[i]);
            return false;
        }

//Checks if either left or right child have a bigger value than itself

    for (int i = 1; i <= (A.length - 2) / 2; i++) {
        System.out.println("A: " + i);
        if (A[i] < A[2 * i] || A[i] < A[2 * i + 1]) {
            return false;
        }
    }


 //the array


 int A[] = {0, 40, 35, 30, 25, 15, 10, 5};

The last for loop works, but for some reason i get a mistake in the first for loop. The loop can find a number. Lets say that i picked 15 to be equal with A[i], then it would work and return false, but when the selected number 0 isn't there, then it sends me the error, and it wont go further for the second loop.

 //Error:

 A.length: 40
A.length: 35
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
A.length: 30
A.length: 25
A.length: 15
A.length: 10
A.length: 5
    at ismaxheap.IsMaxHeap.MaxHeap(IsMaxHeap.java:24)
    at ismaxheap.IsMaxHeap.main(IsMaxHeap.java:15)
/Users/yusuf/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

THE WHOLE CODE:

public class IsMaxHeap {

public static void main(String[] args) {
    MaxHeap();
    System.out.println("Max Heap:  " + MaxHeap());
}

public static boolean MaxHeap() {

    int A[] = {0, 40, 35, 30, 25, 15, 10, 5};

    for (int i = 1; i <= A.length; i++) {
        System.out.println("A.length: " + A[i]);
        if (A[i] == 0) {
            System.out.println("weweh: "+ A[i]);
            return false;
        }
    } 

    for (int i = 1; i <= (A.length - 2) / 2; i++) {
        System.out.println("A: " + i);
        if (A[i] < A[2 * i] || A[i] < A[2 * i + 1]) {
            return false;
        }
    }
    return true;
}

}
Buster3650
  • 470
  • 2
  • 8
  • 19
  • Are you 1-indexing your arrays? 0_0 – Easton Bornemeier Jul 31 '18 at 19:34
  • Yes, to make a long story short, it's because of the heap. To find the children you have to check index of the array. To find left child: 2 * i -- to find right child: 2 * i + 1. if you start at index zero the rule changes – Buster3650 Jul 31 '18 at 19:44
  • There's a much easier way to check to see if an array is a valid max heap. See https://stackoverflow.com/a/51594791/56778 – Jim Mischel Aug 01 '18 at 13:41

1 Answers1

0

In your first loop you are looping one more than possible by using <=

for (int i = 1; i <= A.length; i++) 

Since you are trying to access A[i] and if i == A.length then you are out of bounds.

Should be:

for (int i = 1; i < A.length; i++) 

Now I don't know why are you starting at index 1 since indexing in Java start with 0.

Dealing with special case of one child

This is the case when you array length is 3 (Since the zero index is ignored)

All you have to check is if A[2] < A[1] to be a Max heap, otherwise return false immediately.

Otherwise do the normal loop.

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • 1
    it worked, but i just found out that the second loop doesn't work, when there's only 1 child. To find children in an array, you look at the index numbers. Left child: 2 * i Right child: 2 * i + 1 For the following array: int E[] = {0, 15, 12, 9, 32}; I receive true instead of false. By debugging i found out that it's because the second loop ONLY look works if there are 2 children. When there's only 1, for instance the 32, then it wont even bother to check it. Do you know the answer to this? – Buster3650 Jul 31 '18 at 20:11
  • @Buster3650 Have a special case when you only have 1 child (before looping the second time) – gtgaxiola Jul 31 '18 at 20:12