You are given an array arr
of length n
. The array is represented in the form of a complete binary tree. A representation of an array as a binary tree is said to be heap compatible if it follows the following features:
The first element is made the root of the tree. Every subsequent element is made the left-child. If left pointer is non-empty, it is made the right-child. If both children exist, then the same procedure is followed for the left-child and then right child.
Consider the array below:
arr[] = {5,6,89,9,45,7,1}
The heap compatible representation of arr[]
is as follows:
5
/\
6 89
/\ /\
9 45 7 1
Task
You are given an array representation of a heap compatible binary tree. You have to construct a max-heap data structure using the given array. Return the BFS traversal of the data. Build the heap using iterated insertions.
Note: You may want to use the given “queue.h” file to return the BFS traversal
Sample I/O
Sample Input 1
5
4 10 3 2 1
Sample Output 1
10 4 3 2 1