# Is Binary Heap Tree

Posted: 24 Nov, 2020
Difficulty: Easy

## PROBLEM STATEMENT

#### Note:

``````A binary tree is a tree in which each parent node has at most two children.

A binary heap tree has the following properties.
1. It must be a complete binary tree. In the complete binary tree every level, except the last level, is completely filled and the last level is as far left as possible.

2. Every parent must be greater than its all children nodes.
``````
##### For example:
``````Consider this binary tree:
`````` ``````Figure 1 is a complete binary tree because every level, except the last level, is completely filled and the last nodes are as far left as possible, and the level has 2 nodes both on the left side.

Figure 2 is not a complete binary tree because level 2 (level is 0 based) is not completely filled means the right child of the node (36) is missing.
There is another reason, in the last level, there can be one another node in between node (1) and node (14) to make the binary tree as far left as possible.
``````
##### Note:
``````1. In the world of programming two types of binary heap tree possible:
a. Min-heap - if all parents nodes are lesser than its children nodes.
b. Max-heap - if all parents nodes greater than its children nodes, explained in the above figure-1.

2. In this problem binary heap tree is a binary max-heap tree.
``````
##### Input Format:
``````The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.

The only line of input contains the elements of the tree in the level order form separated by a single space.
If any node does not have a left or right child, take -1 in its place. Refer to the example below.
``````

#### Example:

``````Elements are in the level order form. The input consists of values of nodes separated by a single space in a single line. In case a node is null, we take -1 in its place.

For example, the input for the tree depicted in the below image would be :
`````` `````` 1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation :

Level 1 :
The root node of the tree is 1

Level 2 :
Left child of 1 = 2
Right child of 1 = 3

Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6

Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)

Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.

The input ends when all nodes at the last level are null (-1).
``````
##### Note :
``````The above format was just to provide clarity on how the input is formed for a given tree.

The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
``````
##### Output Format:
``````For every test case, print a single containing either "True" if the binary tree is a heap else print "False".
``````

#### Note:

``````You do not need to print anything; it has already been taken care of. Just implement the given function.
``````
##### Constraint :
``````1 <= 'T' <= 100
1 <= 'N' <= 3000
0 <= 'DATA' <= 10 ^ 9

Where ‘N’ is the number of nodes in the tree, and ‘DATA’ denotes data contained in the node of a binary tree.

Time Limit: 1 sec.
`````` Approach 1

The idea is that we keep a check of both the properties of a binary heap one by one. Firstly, we check if the given tree is a complete binary tree and secondly, we check if all the parent nodes are greater than their child nodes. The above figure (3,8) represents that the level of the node with data (1) is 3 and the index is 8.

• Firstly, we check given binary tree is a complete binary tree or not
• In the binary heap representation of a binary tree, if the parent node is assigned an index of ‘I’, the left child gets assigned an index of ‘2 * ‘I’ while the right child gets assigned an index of ‘2 * ‘I’ + 1’. In the above figure node with value (10) has an assigned index of ‘3’ so its left node with data (11) has to assign index ‘6’ and its right node with data (5) has been assigned index of ‘7’. 4
• If we represent the above binary tree as an array with the respective indices assigned to the different nodes of the tree above from top to down and left to right. The algorithm of the whole process goes like this:
• Count the number of nodes in the binary tree.
• Start with the root node, assign index ‘1’ to root node and recursively check for a left child with index ‘2 * ‘I’ and right child with index ‘2 * ‘I’ + 1’.
• If at any point in time, the examined node’s assigned index is greater than the total number of nodes then it is not a complete binary tree and returns false
• In the end, if all node’s assigned indices are less than the number of nodes in the tree, then, it is ensured that the given tree is a complete binary tree.
• Given a binary tree is a complete binary tree then we check for the second heap property that is:- every parent node must be greater than its child nodes.
• Start from the root and check for every node
• If: at any point in time, the parent node is less than its children node then return false because it is not holding binary max-heap property.
• Else: check for its left child and right child recursively.
• In the end, if the binary tree holds the second property of the binary heap tree then we can ensure that the given tree is a binary heap tree, return true.
• Else return false.