0

Hello I need help for my homework. How can i check if a list is minimum binary heap? Please tell me if i did it right:

public boolean check(int [] arr){
  if(arr[0]==null){
    return false;
  }
  for(int i=0 ; i<arr.length ;i++){
    if(x[i]<x[2i+1] && x[i]<x[2i+2]){
      return true;
    }
   return false;
}
amit
  • 175,853
  • 27
  • 231
  • 333
  • an array is an array. Depending on the indices you can interpret anything you want into that array. Using simply an array without enclosing class containing the heap logic the answer is basically a *no*, an array is not a minimum heap. – luk2302 Jun 23 '17 at 12:38
  • It'll give NPE because you are running loop till `arr.length` and also accessing `2*i+2` – Kaushal28 Jun 23 '17 at 12:38
  • 2
    `int == null` is impossible in java. – M. Prokhorov Jun 23 '17 at 12:43
  • @luk2302 all min-heaps are array, so min-heaps are in a subset of the set of arrays. The OP wants an [indicator function](https://en.wikipedia.org/wiki/Indicator_function) of the min-heaps. See: http://en.cppreference.com/w/cpp/algorithm/is_heap – fjardon Jun 23 '17 at 12:45
  • @luk2302: usually one implements a binary heap [with an array](https://en.wikipedia.org/wiki/Binary_heap#Heap_implementation) since then one can save memory on references to the children of a node and nodes itself. – Willem Van Onsem Jun 23 '17 at 12:46
  • 1
    @luk2302 You don't need a class for everything. See: [implicit data structure](https://en.wikipedia.org/wiki/Implicit_data_structure) – fjardon Jun 23 '17 at 12:47
  • Stack Overflow is not a compiler or debugging service. Please, at the very least, test your code yourself and explain the problem, if there is one, clearly in the question. Also post your code as a [mcve] that clearly shows the problematic behaviour. But you really should just learn to debug - us telling you what's wrong doesn't help anyone, especially not you - you'll get further and further behind in your class if you get other people to fix your problems instead of figuring them out on your own or getting (appropriate) help from your teacher. – Bernhard Barker Jun 23 '17 at 16:36

2 Answers2

1

Short answer: the code is incorrect.

You cannot simply return true from the moment one of the checks succeeds. A minheap is a structure where for every node this check must hold.

Furthermore it is more safe to check whether the parent of a node is indees less than (or equal) to the node itself. Since the last layer of the heap can be incomplete.

public boolean check(int [] arr){
    if(arr == null){ // check whether the arr is null itself, not the element
        return false; // here we assume null is not a heap (open for discussion)
    }
    for(int i=1 ; i < arr.length ;i++){
        if(x[i] < x[(i-1)/2]){ // the node is less than its parent
            return false; // we know there is an error, so return false
        }
   }
   return true; // all checks succeeded, return true
}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

You're having it the other way around.

You need ALL elements fit the criteria, while your code checks if ANY element fit the criteria.

You should return false in case of violation of this criteria, and if you are done without finding any - return true.
Also: Don't forget to make sure you do not access x[2*i+1] when i > x.length()/2

public boolean check(int [] arr){
  if(arr==null){
    return false;
  }
  for(int i=0 ; i<arr.length ;i++){
    // check if the min heap property is violated
    if((2*i + 1 < arr.length && x[i] > x[2*i+1]) || (2*i + 2 < arr.length && x[i]>x[2i+2])){
      return false;
    }
  }
  return true;
}
amit
  • 175,853
  • 27
  • 231
  • 333