
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if a given Binary Tree is Heap in Python
Suppose we have a binary tree; we have to check whether it is heap or not. The heap has following property: Heap will be a binary tree That tree should be a complete tree (So. all levels except last should be full). Every nodes value of that tree should be greater than or equal to its child node (max-heap).
So, if the input is like
then the output will be true
To solve this, we will follow these steps −
- Define a function number_of_nodes() . This will take root
- if root is null, then
- return 0
- otherwise,
- return(1 + number_of_nodes(root.left) + number_of_nodes(root.right))
- Define a function has_heap_property() . This will take root
- if root.left is null and root.right is null, then
- return True
- if root.right is null, then
- return true when root.val >= root.left.val
- otherwise,
- if (root.val >= root.left.val and root.val >= root.right.val, then
- return(has_heap_property(root.left) and has_heap_property(root.right))
- otherwise,
- return False
- if (root.val >= root.left.val and root.val >= root.right.val, then
- Define a function is_complete_tree() . This will take root,index, node_count
- if root is null, then
- return True
- if index >= node_count, then
- return False
- return(is_complete_tree(root.left, 2 * index + 1, node_count) and is_complete_tree(root.right, 2 * index + 2, node_count))
- From the main method do the following −
- node_count := number_of_nodes()
- if is_complete_tree(root, 0, node_count) and has_heap_property(root) is non-zero, then
- return True
- otherwise,
- return False
Example
Let us see the following implementation to get better understanding −
class TreeNode: def __init__(self, value): self.val = value self.left = None self.right = None def number_of_nodes(self, root): if root is None: return 0 else: return (1 + self.number_of_nodes(root.left) + self.number_of_nodes(root.right)) def has_heap_property(self, root): if (root.left is None and root.right is None): return True if root.right is None: return root.val >= root.left.val else: if (root.val >= root.left.val and root.val >= root.right.val): return (self.has_heap_property(root.left) and self.has_heap_property(root.right)) else: return False def is_complete_tree(self, root,index, node_count): if root is None: return True if index >= node_count: return False return (self.is_complete_tree(root.left, 2 * index + 1, node_count) and self.is_complete_tree(root.right, 2 * index + 2, node_count)) def is_heap(self): node_count = self.number_of_nodes(self) if (self.is_complete_tree(self, 0, node_count) and self.has_heap_property(self)): return True else: return False root = TreeNode(99) root.left = TreeNode(46) root.right = TreeNode(39) root.left.left = TreeNode(14) root.left.right = TreeNode(5) root.right.left = TreeNode(9) root.right.right = TreeNode(33) root.left.left.left = TreeNode(7) root.left.left.right = TreeNode(12) print(root.is_heap())
Input
root = TreeNode(99) root.left = TreeNode(46) root.right = TreeNode(39) root.left.left = TreeNode(14) root.left.right = TreeNode(5) root.right.left = TreeNode(9) root.right.right = TreeNode(33) root.left.left.left = TreeNode(7) root.left.left.right = TreeNode(12)
Output
True
- Related Articles
- Check if a given Binary Tree is Heap in C++
- Check if a given Binary Tree is height balanced like a Red-Black Tree in Python
- Check if a given Binary Tree is SumTree in C++
- Check if a given Binary Tree is height balanced like a Red-Black Tree in C++
- Check if a binary tree is subtree of another binary tree in C++
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- Python - Check if a given string is binary string or not
- C++ Program to Check if a Binary Tree is a BST
- Program to find out if a BST is present in a given binary tree in Python
- Check if a given graph is tree or not
- Check if a binary tree is sorted levelwise or not in C++
- Check if a given array can represent Preorder Traversal of Binary Search Tree in C++
- Program to find out if a linked list is present in a given binary tree in Python

Advertisements