Given a binary tree we need to check it has heap property or not, Binary tree need to fulfill following two conditions for being a heap –
- It should be a complete tree (i.e. all levels except last should be full).
- Every node’s value should be greater than or equal to its child node (considering max-heap).
For example this tree contains heap property –
While this doesn’t –
We check each of the above condition separately, for checking completeness isComplete and for checking heap isHeapUtil function are written.
Detail about isComplete function can be found here.
isHeapUtil function is written considering following things –
- Every Node can have 2 children, 0 child (last level nodes) or 1 child (there can be at most one such node).
- If Node has No child then it’s a leaf node and return true (Base case)
- If Node has one child (it must be left child because it is a complete tree) then we need to compare this node with its single child only.
- If Node has both child then check heap property at Node at recur for both subtrees.
Complete code.
Implementation
C/C++
/* C program to checks if a binary tree is max heap ot not */ #include<stdio.h> #include<stdlib.h> #include<stdbool.h> /* Tree node structure */ struct Node { int key; struct Node *left; struct Node *right; }; /* Helper function that allocates a new node */ struct Node *newNode( int k) { struct Node *node = ( struct Node*) malloc ( sizeof ( struct Node)); node->key = k; node->right = node->left = NULL; return node; } /* This function counts the number of nodes in a binary tree */ unsigned int countNodes( struct Node* root) { if (root == NULL) return (0); return (1 + countNodes(root->left) + countNodes(root->right)); } /* This function checks if the binary tree is complete or not */ bool isCompleteUtil ( struct Node* root, unsigned int index, unsigned int number_nodes) { // An empty tree is complete if (root == NULL) return ( true ); // If index assigned to current node is more than // number of nodes in tree, then tree is not complete if (index >= number_nodes) return ( false ); // Recur for left and right subtrees return (isCompleteUtil(root->left, 2*index + 1, number_nodes) && isCompleteUtil(root->right, 2*index + 2, number_nodes)); } // This Function checks the heap property in the tree. bool isHeapUtil( struct Node* root) { // Base case : single node satisfies property if (root->left == NULL && root->right == NULL) return ( true ); // node will be in second last level if (root->right == NULL) { // check heap property at Node // No recursive call , because no need to check last level return (root->key >= root->left->key); } else { // Check heap property at Node and // Recursive check heap property at left and right subtree if (root->key >= root->left->key && root->key >= root->right->key) return ((isHeapUtil(root->left)) && (isHeapUtil(root->right))); else return ( false ); } } // Function to check binary tree is a Heap or Not. bool isHeap( struct Node* root) { // These two are used in isCompleteUtil() unsigned int node_count = countNodes(root); unsigned int index = 0; if (isCompleteUtil(root, index, node_count) && isHeapUtil(root)) return true ; return false ; } // Driver program int main() { struct 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)) printf ( "Given binary tree is a Heap
" ); else printf ( "Given binary tree is not a Heap
" ); return 0; } |
Java
/* Java program to checks if a binary tree is max heap ot not */ // A Binary Tree node class Node { int key; Node left, right; Node( int k) { key = k; left = right = null ; } } class Is_BinaryTree_MaxHeap { /* This function counts the number of nodes in a binary tree */ int countNodes(Node root) { if (root== null ) return 0 ; return ( 1 + countNodes(root.left) + countNodes(root.right)); } /* This function checks if the binary tree is complete or not */ boolean isCompleteUtil(Node root, int index, int number_nodes) { // An empty tree is complete if (root == null ) return true ; // If index assigned to current node is more than // number of nodes in tree, then tree is not complete if (index >= number_nodes) return false ; // Recur for left and right subtrees return isCompleteUtil(root.left, 2 *index+ 1 , number_nodes) && isCompleteUtil(root.right, 2 *index+ 2 , number_nodes); } // This Function checks the heap property in the tree. boolean isHeapUtil(Node root) { // Base case : single node satisfies property if (root.left == null && root.right== null ) return true ; // node will be in second last level if (root.right == null ) { // check heap property at Node // No recursive call , because no need to check last level return root.key >= root.left.key; } else { // Check heap property at Node and // Recursive check heap property at left and right subtree if (root.key >= root.left.key && root.key >= root.right.key) return isHeapUtil(root.left) && isHeapUtil(root.right); else return false ; } } // Function to check binary tree is a Heap or Not. boolean isHeap(Node root) { if (root == null ) return true ; // These two are used in isCompleteUtil() int node_count = countNodes(root); if (isCompleteUtil(root, 0 , node_count)== true && isHeapUtil(root)== true ) return true ; return false ; } // driver function to test the above functions public static void main(String args[]) { Is_BinaryTree_MaxHeap bt = new Is_BinaryTree_MaxHeap(); Node root = new Node( 10 ); root.left = new Node( 9 ); root.right = new Node( 8 ); root.left.left = new Node( 7 ); root.left.right = new Node( 6 ); root.right.left = new Node( 5 ); root.right.right = new Node( 4 ); root.left.left.left = new Node( 3 ); root.left.left.right = new Node( 2 ); root.left.right.left = new Node( 1 ); if (bt.isHeap(root) == true ) System.out.println( "Given binary tree is a Heap" ); else System.out.println( "Given binary tree is not a Heap" ); } } // This code has been contributed by Amit Khandelwal |
Python
# To check if a binary tree # is a MAX Heap or not class GFG: def __init__( self , value): self .key = value self .left = None self .right = None def count_nodes( self , root): if root is None : return 0 else : return ( 1 + self .count_nodes(root.left) + self .count_nodes(root.right)) def heap_propert_util( self , root): if (root.left is None and root.right is None ): return True if root.right is None : return root.key > = root.left.key else : if (root.key > = root.left.key and root.key > = root.right.key): return ( self .heap_propert_util(root.left) and self .heap_propert_util(root.right)) else : return False def complete_tree_util( self , root, index, node_count): if root is None : return True if index > = node_count: return False return ( self .complete_tree_util(root.left, 2 * index + 1 , node_count) and self .complete_tree_util(root.right, 2 * index + 2 , node_count)) def check_if_heap( self ): node_count = self .count_nodes( self ) if ( self .complete_tree_util( self , 0 , node_count) and self .heap_propert_util( self )): return True else : return False # Driver Code root = GFG( 5 ) root.left = GFG( 2 ) root.right = GFG( 3 ) root.left.left = GFG( 1 ) if root.check_if_heap(): print ( "Given binary tree is a heap" ) else : print ( "Given binary tree is not a Heap" ) # This code has been # contributed by Yash Agrawal |
Output:
Given binary tree is a Heap
leave a comment
0 Comments