Search function in a array-based MaxHeap java

97 views Asked by At

I wrote this code, but it gives arrayoutofboundexception at the last index, but it is working for other indexes. please help :) In main, im passing the value of i = 0;

public int recursiveSearch(int key, int i)
    {
       int left = ((2*i) + 1);
       int right = ((2*i) + 2);
       if(isEmpty())
       {
           return -1;
       }  
       if(a[i] == key)
       {
           return i; 
       }
       else if(key < a[i])
       {
           int found = recursiveSearch(key, left);
           if(found == -1)
           {
               return recursiveSearch(key, right);
           }
           return found;
       }
       else
       {
           return -1;
       }
       
    }
0

There are 0 answers