//-------------------------------------------------------------------------------------------------- // Returns true if arr[i..n-1] represents a // max-heap bool isHeap(int arr[], int index, int size) { if (index > (size - 2)/2) // If a leaf node return true; // If an internal node and is greater than its children, and same is recursively true for the // children if ( arr[index] >= arr[2 * index + 1] && arr[index] >= arr[2 * index + 2] && isHeap(arr, 2 * index + 1, size) && isHeap(arr, 2*index + 2, size) ) return true; return false; }
// Returns true if arr[i..n-1] represents a // max-heap bool isHeap(int arr[], int i, int n) { // If a leaf node if (i > (n - 2)/2) return true; // If an internal node and is greater than its children, and // same is recursively true for the children if (arr[i] >= arr[2*i + 1] && arr[i] >= arr[2*i + 2] && isHeap(arr, 2*i + 1, n) && isHeap(arr, 2*i + 2, n)) return true; return false; }
// Driver program int main() { int arr[] = {90, 15, 10, 7, 12, 2, 7, 3}; int size = sizeof(arr) / sizeof(int); isHeap(arr, 0, size)? printf("Yes"): printf("No"); return 0; }