How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; Start from root and go till the last internal node ; If left child is greater , return false ; If right child is greater , return false ; Driver Code
< ? php function isHeap ( $ arr , $ i , $ n ) { for ( $ i = 0 ; $ i < ( ( $ n - 2 ) / 2 ) + 1 ; $ i ++ ) { if ( $ arr [ 2 * $ i + 1 ] > $ arr [ $ i ] ) return False ; if ( 2 * $ i + 2 < $ n && $ arr [ 2 * $ i + 2 ] > $ arr [ $ i ] ) return False ; return True ; } } $ arr = array ( 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ) ; $ n = sizeof ( $ arr ) ; if ( isHeap ( $ arr , 0 , $ n ) ) echo " Yes " ; else echo " No " ; ? >