How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; If a leaf node ; If an internal node and is greater than its children , and same is recursively true for the children ; Driver Code
< ? php function isHeap ( $ arr , $ i , $ n ) { if ( $ i >= ( $ n - 2 ) / 2 ) return true ; 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 ; } $ arr = array ( 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ) ; $ n = sizeof ( $ arr ) ; if ( isHeap ( $ arr , 0 , $ n ) ) echo " Yes " ; else echo " No " ; ? >