text: Check if a given Binary Tree is Heap | Java program to checks if a binary tree is max heap or not ; Tree node structure ; To add a new node ; Driver code ; Function call
code: import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int k ) { Node node = new Node ( ) ; node . data = k ; node . right = node . left = null ; return node ; } static boolean isHeap ( Node root ) { Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; boolean nullish = false ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left != null ) { if ( nullish temp . left . data >= temp . data ) { return false ; } q . add ( temp . left ) ; } else { nullish = true ; } if ( temp . right != null ) { if ( nullish temp . right . data >= temp . data ) { return false ; } q . add ( temp . right ) ; } else { nullish = true ; } } return true ; } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 10 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 8 ) ; root . left . left = newNode ( 7 ) ; root . left . right = newNode ( 6 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 4 ) ; root . left . left . left = newNode ( 3 ) ; root . left . left . right = newNode ( 2 ) ; root . left . right . left = newNode ( 1 ) ; if ( isHeap ( root ) ) System . out . print ( "Given binary tree is a HeapNEW_LINE"); else System . out . print ( "Given binary tree is not a HeapNEW_LINE"); } }