I'm trying to write a program that checks if a vector is a min heap. I've been looking at code from here. I understand why they use 2*i+2 to compare to n, since there's a tipping point with the index where values in the vector/array (mine uses a vector) become leaf nodes. What I don't understand is why they keep using 2*i + 1 and 2*i + 2 as the index when they call the function recursively. Shouldn't they be using i+1 to access the left node and i+2 to access the right? But I tried this and I get a segmentation fault.
bool checkMinHeap(int A[], int i, int n)
{
// if i is a leaf node, return true as every leaf node is a heap
if (2*i + 2 > n)
return true;
// if i is an internal node
// recursively check if left child is heap
bool left = (A[i] <= A[2*i + 1]) && checkMinHeap(A, 2*i + 1, n);
// recursively check if right child is heap (to avoid array out
// of bound, we first check if right child exists or not)
bool right = (2*i + 2 == n) ||
(A[i] <= A[2*i + 2] && checkMinHeap(A, 2*i + 2, n));
// return true if both left and right child are heap
return left && right;
}
Their test code:
int main()
{
int A[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(A) / sizeof(int);
// start with index 0 (root of the heap)
int index = 0;
if (checkMinHeap(A, index, n))
cout << "Given array is a min heap";
else
cout << "Given array is not a min heap";
return 0;
}
My test code (returns 0, when it should return 1):
int main (void)
{
vector <int> test;
test.push_back(1);
test.push_back(2);
test.push_back(3);
test.push_back(4);
test.push_back(5);
test.push_back(9);
test.push_back(3);
test.push_back(19);
cout << isMinHeap(test,0) << endl;
}